2015-02-04 23:39:41 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/api/atom_api_web_contents.h"
|
2015-09-05 02:43:30 +00:00
|
|
|
#include "atom/browser/web_contents_preferences.h"
|
2015-02-04 23:39:41 +00:00
|
|
|
#include "atom/browser/web_view_manager.h"
|
2015-09-05 02:43:30 +00:00
|
|
|
#include "atom/common/native_mate_converters/value_converter.h"
|
|
|
|
#include "atom/common/node_includes.h"
|
2015-02-05 08:17:45 +00:00
|
|
|
#include "content/public/browser/browser_context.h"
|
2015-02-04 23:39:41 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Converter<content::WebContents*> {
|
2015-05-22 11:11:22 +00:00
|
|
|
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
|
2015-02-04 23:39:41 +00:00
|
|
|
content::WebContents** out) {
|
|
|
|
atom::api::WebContents* contents;
|
|
|
|
if (!Converter<atom::api::WebContents*>::FromV8(isolate, val, &contents))
|
|
|
|
return false;
|
|
|
|
*out = contents->web_contents();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mate
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-02-04 23:52:59 +00:00
|
|
|
atom::WebViewManager* GetWebViewManager(content::WebContents* web_contents) {
|
|
|
|
auto context = web_contents->GetBrowserContext();
|
|
|
|
if (context) {
|
|
|
|
auto manager = context->GetGuestManager();
|
|
|
|
return static_cast<atom::WebViewManager*>(manager);
|
2015-02-05 08:17:45 +00:00
|
|
|
} else {
|
|
|
|
return nullptr;
|
2015-02-04 23:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddGuest(int guest_instance_id,
|
|
|
|
int element_instance_id,
|
|
|
|
content::WebContents* embedder,
|
|
|
|
content::WebContents* guest_web_contents,
|
2015-09-05 02:43:30 +00:00
|
|
|
const base::DictionaryValue& options) {
|
2015-02-04 23:52:59 +00:00
|
|
|
auto manager = GetWebViewManager(embedder);
|
2015-09-03 00:47:58 +00:00
|
|
|
if (manager)
|
2015-02-04 23:52:59 +00:00
|
|
|
manager->AddGuest(guest_instance_id, element_instance_id, embedder,
|
2015-09-03 00:47:58 +00:00
|
|
|
guest_web_contents);
|
|
|
|
|
2015-09-05 02:43:30 +00:00
|
|
|
atom::WebContentsPreferences::From(guest_web_contents)->Merge(options);
|
2015-02-04 23:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveGuest(content::WebContents* embedder, int guest_instance_id) {
|
|
|
|
auto manager = GetWebViewManager(embedder);
|
|
|
|
if (manager)
|
|
|
|
manager->RemoveGuest(guest_instance_id);
|
|
|
|
}
|
|
|
|
|
2015-05-22 11:11:22 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context, void* priv) {
|
2015-02-04 23:39:41 +00:00
|
|
|
mate::Dictionary dict(context->GetIsolate(), exports);
|
2015-02-04 23:52:59 +00:00
|
|
|
dict.SetMethod("addGuest", &AddGuest);
|
|
|
|
dict.SetMethod("removeGuest", &RemoveGuest);
|
2015-02-04 23:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_web_view_manager, Initialize)
|