electron/atom/browser/web_view/web_view_manager.cc

154 lines
5 KiB
C++
Raw Normal View History

// Copyright (c) 2014 GitHub, Inc.
2014-10-22 14:55:13 +00:00
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/web_view/web_view_manager.h"
2014-10-23 15:08:48 +00:00
#include "atom/browser/api/atom_api_web_contents.h"
#include "atom/browser/atom_browser_context.h"
2014-11-06 07:13:37 +00:00
#include "atom/common/native_mate_converters/gurl_converter.h"
2014-10-23 15:08:48 +00:00
#include "base/bind.h"
#include "base/stl_util.h"
#include "content/public/browser/render_process_host.h"
2014-10-23 15:08:48 +00:00
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
2014-11-06 07:13:37 +00:00
#include "net/base/filename_util.h"
2014-10-23 15:08:48 +00:00
#include "atom/common/node_includes.h"
namespace mate {
template<>
struct Converter<content::WebContents*> {
static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
content::WebContents** out) {
atom::api::WebContents* contents;
if (!Converter<atom::api::WebContents*>::FromV8(isolate, val, &contents))
return false;
*out = contents->web_contents();
return true;
}
};
template<>
2015-02-04 22:58:03 +00:00
struct Converter<atom::WebViewManager::WebViewInfo> {
static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
2015-02-04 22:58:03 +00:00
atom::WebViewManager::WebViewInfo* out) {
Dictionary options;
if (!ConvertFromV8(isolate, val, &options))
return false;
2015-02-04 22:58:03 +00:00
GURL preload_url;
if (!options.Get("preloadUrl", &preload_url))
return false;
return net::FileURLToFilePath(preload_url, &(out->preload_script)) &&
options.Get("nodeIntegration", &(out->node_integration)) &&
options.Get("plugins", &(out->plugins)) &&
options.Get("disableWebSecurity", &(out->disable_web_security));
}
};
2014-10-23 15:08:48 +00:00
} // namespace mate
2014-10-22 14:55:13 +00:00
namespace atom {
WebViewManager::WebViewManager(content::BrowserContext* context) {
}
WebViewManager::~WebViewManager() {
}
2014-10-23 15:08:48 +00:00
void WebViewManager::AddGuest(int guest_instance_id,
int element_instance_id,
2014-10-23 15:08:48 +00:00
content::WebContents* embedder,
content::WebContents* web_contents,
2015-02-04 22:58:03 +00:00
WebViewInfo info) {
base::AutoLock auto_lock(lock_);
web_contents_map_[guest_instance_id] = { web_contents, embedder };
2015-02-04 22:58:03 +00:00
int guest_process_id = web_contents->GetRenderProcessHost()->GetID();
info.guest_instance_id = guest_instance_id;
info.embedder = embedder;
webview_info_map_[guest_process_id] = info;
// Map the element in embedder to guest.
ElementInstanceKey key(embedder, element_instance_id);
element_instance_id_to_guest_map_[key] = guest_instance_id;
2014-10-23 15:08:48 +00:00
}
void WebViewManager::RemoveGuest(int guest_instance_id) {
2015-02-04 22:58:03 +00:00
base::AutoLock auto_lock(lock_);
if (!ContainsKey(web_contents_map_, guest_instance_id)) {
return;
}
auto web_contents = web_contents_map_[guest_instance_id].web_contents;
2014-10-23 15:08:48 +00:00
web_contents_map_.erase(guest_instance_id);
2015-02-04 22:58:03 +00:00
int guest_process_id = web_contents->GetRenderProcessHost()->GetID();
webview_info_map_.erase(guest_process_id);
// Remove the record of element in embedder too.
for (const auto& element : element_instance_id_to_guest_map_)
if (element.second == guest_instance_id) {
element_instance_id_to_guest_map_.erase(element.first);
break;
}
2014-10-23 15:08:48 +00:00
}
2015-02-04 22:58:03 +00:00
bool WebViewManager::GetInfo(int guest_process_id, WebViewInfo* webview_info) {
base::AutoLock auto_lock(lock_);
WebViewInfoMap::iterator iter = webview_info_map_.find(guest_process_id);
if (iter != webview_info_map_.end()) {
*webview_info = iter->second;
return true;
}
return false;
}
content::WebContents* WebViewManager::GetGuestByInstanceID(
content::WebContents* embedder,
int element_instance_id) {
ElementInstanceKey key(embedder, element_instance_id);
if (!ContainsKey(element_instance_id_to_guest_map_, key))
return nullptr;
int guest_instance_id = element_instance_id_to_guest_map_[key];
2014-10-23 15:08:48 +00:00
if (ContainsKey(web_contents_map_, guest_instance_id))
return web_contents_map_[guest_instance_id].web_contents;
2014-10-23 15:08:48 +00:00
else
return nullptr;
2014-10-22 14:55:13 +00:00
}
bool WebViewManager::ForEachGuest(content::WebContents* embedder_web_contents,
const GuestCallback& callback) {
2014-10-23 15:08:48 +00:00
for (auto& item : web_contents_map_)
if (item.second.embedder == embedder_web_contents &&
callback.Run(item.second.web_contents))
return true;
2014-10-22 14:55:13 +00:00
return false;
}
} // namespace atom
2014-10-23 15:08:48 +00:00
namespace {
void Initialize(v8::Handle<v8::Object> exports, v8::Handle<v8::Value> unused,
v8::Handle<v8::Context> context, void* priv) {
using atom::WebViewManager;
auto manager = static_cast<WebViewManager*>(
atom::AtomBrowserContext::Get()->GetGuestManager());
mate::Dictionary dict(context->GetIsolate(), exports);
dict.SetMethod("addGuest",
base::Bind(&WebViewManager::AddGuest,
base::Unretained(manager)));
dict.SetMethod("removeGuest",
base::Bind(&WebViewManager::RemoveGuest,
base::Unretained(manager)));
}
} // namespace
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_web_view_manager, Initialize)