2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2014-04-24 08:45:25 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/api/atom_api_web_contents.h"
|
|
|
|
|
2014-10-23 06:04:13 +00:00
|
|
|
#include "atom/browser/atom_browser_context.h"
|
2014-04-25 05:10:16 +00:00
|
|
|
#include "atom/common/api/api_messages.h"
|
2014-10-24 06:37:14 +00:00
|
|
|
#include "atom/common/native_mate_converters/gfx_converter.h"
|
2014-04-24 09:00:41 +00:00
|
|
|
#include "atom/common/native_mate_converters/gurl_converter.h"
|
|
|
|
#include "atom/common/native_mate_converters/string16_converter.h"
|
2014-04-25 05:10:16 +00:00
|
|
|
#include "atom/common/native_mate_converters/value_converter.h"
|
2014-04-25 08:13:16 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2014-11-03 14:12:56 +00:00
|
|
|
#include "brightray/browser/inspectable_web_contents.h"
|
2014-12-09 22:38:43 +00:00
|
|
|
#include "content/public/browser/navigation_details.h"
|
2014-07-28 07:29:51 +00:00
|
|
|
#include "content/public/browser/render_frame_host.h"
|
2014-04-24 09:00:41 +00:00
|
|
|
#include "content/public/browser/render_process_host.h"
|
2014-04-25 02:34:40 +00:00
|
|
|
#include "content/public/browser/render_view_host.h"
|
2014-10-24 08:09:18 +00:00
|
|
|
#include "content/public/browser/render_widget_host_view.h"
|
2014-10-24 15:05:25 +00:00
|
|
|
#include "content/public/browser/resource_request_details.h"
|
2014-10-24 12:20:56 +00:00
|
|
|
#include "content/public/browser/site_instance.h"
|
2014-04-24 08:45:25 +00:00
|
|
|
#include "content/public/browser/web_contents.h"
|
2014-10-23 06:04:13 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
2014-04-24 08:45:25 +00:00
|
|
|
#include "native_mate/object_template_builder.h"
|
|
|
|
|
2014-10-23 06:04:13 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
|
|
|
|
2014-04-24 08:45:25 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2014-10-23 05:31:10 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
v8::Persistent<v8::ObjectTemplate> template_;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-04-24 08:45:25 +00:00
|
|
|
WebContents::WebContents(content::WebContents* web_contents)
|
2014-10-23 09:54:52 +00:00
|
|
|
: content::WebContentsObserver(web_contents),
|
2014-10-24 06:37:14 +00:00
|
|
|
guest_instance_id_(-1),
|
2014-12-08 16:05:34 +00:00
|
|
|
element_instance_id_(-1),
|
2014-10-24 08:48:21 +00:00
|
|
|
guest_opaque_(true),
|
2014-11-02 15:32:33 +00:00
|
|
|
auto_size_enabled_(false) {
|
2014-10-23 06:04:13 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 09:54:52 +00:00
|
|
|
WebContents::WebContents(const mate::Dictionary& options)
|
2014-10-24 06:37:14 +00:00
|
|
|
: guest_instance_id_(-1),
|
2014-12-08 16:05:34 +00:00
|
|
|
element_instance_id_(-1),
|
2014-10-24 08:48:21 +00:00
|
|
|
guest_opaque_(true),
|
2014-10-24 06:37:14 +00:00
|
|
|
auto_size_enabled_(false) {
|
2014-10-24 08:09:18 +00:00
|
|
|
options.Get("guestInstanceId", &guest_instance_id_);
|
2014-10-23 09:54:52 +00:00
|
|
|
|
2014-10-24 12:20:56 +00:00
|
|
|
auto browser_context = AtomBrowserContext::Get();
|
|
|
|
content::SiteInstance* site_instance = content::SiteInstance::CreateForURL(
|
|
|
|
browser_context, GURL("chrome-guest://fake-host"));
|
|
|
|
|
|
|
|
content::WebContents::CreateParams params(browser_context, site_instance);
|
2014-10-23 06:04:13 +00:00
|
|
|
bool is_guest;
|
|
|
|
if (options.Get("isGuest", &is_guest) && is_guest)
|
|
|
|
params.guest_delegate = this;
|
|
|
|
|
2014-11-03 11:45:09 +00:00
|
|
|
storage_.reset(brightray::InspectableWebContents::Create(params));
|
|
|
|
Observe(storage_->GetWebContents());
|
|
|
|
web_contents()->SetDelegate(this);
|
2014-10-23 06:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WebContents::~WebContents() {
|
2014-10-23 09:54:52 +00:00
|
|
|
Destroy();
|
2014-04-24 08:45:25 +00:00
|
|
|
}
|
|
|
|
|
2014-10-25 02:01:19 +00:00
|
|
|
bool WebContents::AddMessageToConsole(content::WebContents* source,
|
|
|
|
int32 level,
|
|
|
|
const base::string16& message,
|
|
|
|
int32 line_no,
|
|
|
|
const base::string16& source_id) {
|
|
|
|
base::ListValue args;
|
|
|
|
args.AppendInteger(level);
|
|
|
|
args.AppendString(message);
|
|
|
|
args.AppendInteger(line_no);
|
|
|
|
args.AppendString(source_id);
|
|
|
|
Emit("console-message", args);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-25 05:21:09 +00:00
|
|
|
bool WebContents::ShouldCreateWebContents(
|
|
|
|
content::WebContents* web_contents,
|
|
|
|
int route_id,
|
|
|
|
WindowContainerType window_container_type,
|
|
|
|
const base::string16& frame_name,
|
|
|
|
const GURL& target_url,
|
|
|
|
const std::string& partition_id,
|
|
|
|
content::SessionStorageNamespace* session_storage_namespace) {
|
|
|
|
base::ListValue args;
|
|
|
|
args.AppendString(target_url.spec());
|
2014-10-27 11:14:17 +00:00
|
|
|
args.AppendString(frame_name);
|
2014-11-04 10:49:57 +00:00
|
|
|
args.AppendInteger(NEW_FOREGROUND_TAB);
|
2014-11-04 10:12:57 +00:00
|
|
|
Emit("-new-window", args);
|
2014-10-25 05:21:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-25 02:01:19 +00:00
|
|
|
void WebContents::CloseContents(content::WebContents* source) {
|
|
|
|
Emit("close");
|
|
|
|
}
|
|
|
|
|
2014-10-24 10:06:32 +00:00
|
|
|
content::WebContents* WebContents::OpenURLFromTab(
|
|
|
|
content::WebContents* source,
|
|
|
|
const content::OpenURLParams& params) {
|
2014-11-04 09:59:15 +00:00
|
|
|
if (params.disposition != CURRENT_TAB) {
|
|
|
|
base::ListValue args;
|
|
|
|
args.AppendString(params.url.spec());
|
|
|
|
args.AppendString("");
|
|
|
|
args.AppendInteger(params.disposition);
|
2014-11-04 10:12:57 +00:00
|
|
|
Emit("-new-window", args);
|
2014-12-17 22:55:22 +00:00
|
|
|
return nullptr;
|
2014-11-04 09:59:15 +00:00
|
|
|
}
|
2014-10-24 10:06:32 +00:00
|
|
|
|
2014-12-17 22:55:22 +00:00
|
|
|
// Give user a chance to cancel navigation.
|
|
|
|
base::ListValue args;
|
|
|
|
args.AppendString(params.url.spec());
|
|
|
|
if (Emit("will-navigate", args))
|
|
|
|
return nullptr;
|
|
|
|
|
2014-10-24 10:06:32 +00:00
|
|
|
content::NavigationController::LoadURLParams load_url_params(params.url);
|
|
|
|
load_url_params.referrer = params.referrer;
|
|
|
|
load_url_params.transition_type = params.transition;
|
|
|
|
load_url_params.extra_headers = params.extra_headers;
|
|
|
|
load_url_params.should_replace_current_entry =
|
|
|
|
params.should_replace_current_entry;
|
|
|
|
load_url_params.is_renderer_initiated = params.is_renderer_initiated;
|
|
|
|
load_url_params.transferred_global_request_id =
|
|
|
|
params.transferred_global_request_id;
|
|
|
|
|
|
|
|
web_contents()->GetController().LoadURLWithParams(load_url_params);
|
|
|
|
return web_contents();
|
|
|
|
}
|
|
|
|
|
2014-10-24 13:46:47 +00:00
|
|
|
void WebContents::HandleKeyboardEvent(
|
|
|
|
content::WebContents* source,
|
|
|
|
const content::NativeWebKeyboardEvent& event) {
|
|
|
|
if (!attached())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Send the unhandled keyboard events back to the embedder to reprocess them.
|
|
|
|
embedder_web_contents_->GetDelegate()->HandleKeyboardEvent(
|
|
|
|
web_contents(), event);
|
|
|
|
}
|
|
|
|
|
2014-04-25 04:40:04 +00:00
|
|
|
void WebContents::RenderViewDeleted(content::RenderViewHost* render_view_host) {
|
2014-04-25 03:51:05 +00:00
|
|
|
base::ListValue args;
|
2014-04-25 04:40:04 +00:00
|
|
|
args.AppendInteger(render_view_host->GetProcess()->GetID());
|
|
|
|
args.AppendInteger(render_view_host->GetRoutingID());
|
2014-04-25 03:51:05 +00:00
|
|
|
Emit("render-view-deleted", args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContents::RenderProcessGone(base::TerminationStatus status) {
|
|
|
|
Emit("crashed");
|
|
|
|
}
|
|
|
|
|
2014-10-11 11:11:34 +00:00
|
|
|
void WebContents::DidFinishLoad(content::RenderFrameHost* render_frame_host,
|
|
|
|
const GURL& validated_url) {
|
|
|
|
bool is_main_frame = !render_frame_host->GetParent();
|
|
|
|
|
2014-06-09 04:03:19 +00:00
|
|
|
base::ListValue args;
|
|
|
|
args.AppendBoolean(is_main_frame);
|
|
|
|
Emit("did-frame-finish-load", args);
|
|
|
|
|
|
|
|
if (is_main_frame)
|
|
|
|
Emit("did-finish-load");
|
2014-04-25 04:22:16 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 15:05:25 +00:00
|
|
|
void WebContents::DidFailLoad(content::RenderFrameHost* render_frame_host,
|
|
|
|
const GURL& validated_url,
|
|
|
|
int error_code,
|
|
|
|
const base::string16& error_description) {
|
|
|
|
base::ListValue args;
|
|
|
|
args.AppendInteger(error_code);
|
|
|
|
args.AppendString(error_description);
|
|
|
|
Emit("did-fail-load", args);
|
|
|
|
}
|
|
|
|
|
2014-04-25 04:22:16 +00:00
|
|
|
void WebContents::DidStartLoading(content::RenderViewHost* render_view_host) {
|
|
|
|
Emit("did-start-loading");
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContents::DidStopLoading(content::RenderViewHost* render_view_host) {
|
|
|
|
Emit("did-stop-loading");
|
|
|
|
}
|
|
|
|
|
2014-10-24 15:05:25 +00:00
|
|
|
void WebContents::DidGetRedirectForResourceRequest(
|
|
|
|
content::RenderViewHost* render_view_host,
|
|
|
|
const content::ResourceRedirectDetails& details) {
|
|
|
|
base::ListValue args;
|
|
|
|
args.AppendString(details.url.spec());
|
|
|
|
args.AppendString(details.new_url.spec());
|
|
|
|
args.AppendBoolean(
|
|
|
|
details.resource_type == content::RESOURCE_TYPE_MAIN_FRAME);
|
|
|
|
Emit("did-get-redirect-request", args);
|
|
|
|
}
|
|
|
|
|
2014-12-09 22:38:43 +00:00
|
|
|
void WebContents::DidNavigateMainFrame(
|
|
|
|
const content::LoadCommittedDetails& details,
|
|
|
|
const content::FrameNavigateParams& params) {
|
|
|
|
if (details.is_navigation_to_different_page())
|
|
|
|
Emit("did-navigate-to-different-page");
|
|
|
|
}
|
|
|
|
|
2014-04-25 08:13:16 +00:00
|
|
|
bool WebContents::OnMessageReceived(const IPC::Message& message) {
|
|
|
|
bool handled = true;
|
|
|
|
IPC_BEGIN_MESSAGE_MAP(WebContents, message)
|
|
|
|
IPC_MESSAGE_HANDLER(AtomViewHostMsg_Message, OnRendererMessage)
|
|
|
|
IPC_MESSAGE_HANDLER_DELAY_REPLY(AtomViewHostMsg_Message_Sync,
|
|
|
|
OnRendererMessageSync)
|
|
|
|
IPC_MESSAGE_UNHANDLED(handled = false)
|
|
|
|
IPC_END_MESSAGE_MAP()
|
|
|
|
|
|
|
|
return handled;
|
|
|
|
}
|
|
|
|
|
2014-10-23 15:08:48 +00:00
|
|
|
void WebContents::RenderViewReady() {
|
2014-10-24 08:09:18 +00:00
|
|
|
if (!is_guest())
|
|
|
|
return;
|
|
|
|
|
2014-10-24 08:48:21 +00:00
|
|
|
// We don't want to accidentally set the opacity of an interstitial page.
|
|
|
|
// WebContents::GetRenderWidgetHostView will return the RWHV of an
|
|
|
|
// interstitial page if one is showing at this time. We only want opacity
|
|
|
|
// to apply to web pages.
|
|
|
|
web_contents()->GetRenderViewHost()->GetView()->
|
|
|
|
SetBackgroundOpaque(guest_opaque_);
|
|
|
|
|
2014-10-24 08:09:18 +00:00
|
|
|
content::RenderViewHost* rvh = web_contents()->GetRenderViewHost();
|
|
|
|
if (auto_size_enabled_) {
|
|
|
|
rvh->EnableAutoResize(min_auto_size_, max_auto_size_);
|
|
|
|
} else {
|
|
|
|
rvh->DisableAutoResize(element_size_);
|
|
|
|
}
|
2014-10-23 15:08:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-28 07:29:51 +00:00
|
|
|
void WebContents::WebContentsDestroyed() {
|
2014-04-25 05:10:16 +00:00
|
|
|
// The RenderViewDeleted was not called when the WebContents is destroyed.
|
2014-10-23 06:04:13 +00:00
|
|
|
RenderViewDeleted(web_contents()->GetRenderViewHost());
|
2014-04-25 03:59:33 +00:00
|
|
|
Emit("destroyed");
|
2014-04-25 03:22:51 +00:00
|
|
|
}
|
|
|
|
|
2014-12-07 15:43:26 +00:00
|
|
|
void WebContents::DidAttach(int guest_proxy_routing_id) {
|
2014-12-08 16:05:34 +00:00
|
|
|
Emit("did-attach");
|
2014-10-23 09:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebContents::ElementSizeChanged(const gfx::Size& old_size,
|
|
|
|
const gfx::Size& new_size) {
|
2014-10-24 06:37:14 +00:00
|
|
|
element_size_ = new_size;
|
2014-10-23 09:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebContents::GuestSizeChanged(const gfx::Size& old_size,
|
|
|
|
const gfx::Size& new_size) {
|
2014-10-24 06:37:14 +00:00
|
|
|
if (!auto_size_enabled_)
|
|
|
|
return;
|
|
|
|
guest_size_ = new_size;
|
|
|
|
GuestSizeChangedDueToAutoSize(old_size, new_size);
|
2014-10-23 09:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebContents::RegisterDestructionCallback(
|
|
|
|
const DestructionCallback& callback) {
|
|
|
|
destruction_callback_ = callback;
|
|
|
|
}
|
|
|
|
|
2014-12-07 15:43:26 +00:00
|
|
|
void WebContents::WillAttach(content::WebContents* embedder_web_contents,
|
|
|
|
int browser_plugin_instance_id) {
|
|
|
|
embedder_web_contents_ = embedder_web_contents;
|
2014-12-08 16:05:34 +00:00
|
|
|
element_instance_id_ = browser_plugin_instance_id;
|
2014-12-07 15:43:26 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 06:04:13 +00:00
|
|
|
void WebContents::Destroy() {
|
|
|
|
if (storage_) {
|
2014-10-23 09:54:52 +00:00
|
|
|
if (!destruction_callback_.is_null())
|
|
|
|
destruction_callback_.Run();
|
|
|
|
|
2014-10-23 06:04:13 +00:00
|
|
|
Observe(nullptr);
|
|
|
|
storage_.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-25 03:22:51 +00:00
|
|
|
bool WebContents::IsAlive() const {
|
2014-04-25 05:10:16 +00:00
|
|
|
return web_contents() != NULL;
|
2014-04-25 03:22:51 +00:00
|
|
|
}
|
|
|
|
|
2014-11-06 19:29:41 +00:00
|
|
|
void WebContents::LoadURL(const GURL& url, const mate::Dictionary& options) {
|
2014-04-25 04:52:30 +00:00
|
|
|
content::NavigationController::LoadURLParams params(url);
|
2014-11-06 19:29:41 +00:00
|
|
|
|
2014-11-12 02:28:50 +00:00
|
|
|
GURL http_referrer;
|
|
|
|
if (options.Get("httpreferrer", &http_referrer))
|
|
|
|
params.referrer = content::Referrer(http_referrer.GetAsReferrer(),
|
|
|
|
blink::WebReferrerPolicyDefault);
|
2014-11-06 19:29:41 +00:00
|
|
|
|
2014-12-07 15:43:26 +00:00
|
|
|
params.transition_type = ui::PAGE_TRANSITION_TYPED;
|
2014-04-25 04:52:30 +00:00
|
|
|
params.override_user_agent = content::NavigationController::UA_OVERRIDE_TRUE;
|
2014-04-25 05:10:16 +00:00
|
|
|
web_contents()->GetController().LoadURLWithParams(params);
|
2014-04-25 04:52:30 +00:00
|
|
|
}
|
|
|
|
|
2014-04-24 09:00:41 +00:00
|
|
|
GURL WebContents::GetURL() const {
|
2014-04-25 05:10:16 +00:00
|
|
|
return web_contents()->GetURL();
|
2014-04-24 09:00:41 +00:00
|
|
|
}
|
|
|
|
|
2014-06-28 11:36:57 +00:00
|
|
|
base::string16 WebContents::GetTitle() const {
|
2014-04-25 05:10:16 +00:00
|
|
|
return web_contents()->GetTitle();
|
2014-04-24 09:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebContents::IsLoading() const {
|
2014-04-25 05:10:16 +00:00
|
|
|
return web_contents()->IsLoading();
|
2014-04-24 09:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebContents::IsWaitingForResponse() const {
|
2014-04-25 05:10:16 +00:00
|
|
|
return web_contents()->IsWaitingForResponse();
|
2014-04-24 09:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebContents::Stop() {
|
2014-04-25 05:10:16 +00:00
|
|
|
web_contents()->Stop();
|
2014-04-24 09:00:41 +00:00
|
|
|
}
|
|
|
|
|
2014-11-06 19:29:41 +00:00
|
|
|
void WebContents::Reload(const mate::Dictionary& options) {
|
2014-04-29 07:35:12 +00:00
|
|
|
// Navigating to a URL would always restart the renderer process, we want this
|
|
|
|
// because normal reloading will break our node integration.
|
|
|
|
// This is done by AtomBrowserClient::ShouldSwapProcessesForNavigation.
|
2014-11-06 19:29:41 +00:00
|
|
|
LoadURL(GetURL(), options);
|
2014-04-25 04:52:30 +00:00
|
|
|
}
|
|
|
|
|
2014-11-06 19:29:41 +00:00
|
|
|
void WebContents::ReloadIgnoringCache(const mate::Dictionary& options) {
|
|
|
|
Reload(options);
|
2014-04-25 04:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebContents::CanGoBack() const {
|
2014-04-25 05:10:16 +00:00
|
|
|
return web_contents()->GetController().CanGoBack();
|
2014-04-25 04:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebContents::CanGoForward() const {
|
2014-04-25 05:10:16 +00:00
|
|
|
return web_contents()->GetController().CanGoForward();
|
2014-04-25 04:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebContents::CanGoToOffset(int offset) const {
|
2014-04-25 05:10:16 +00:00
|
|
|
return web_contents()->GetController().CanGoToOffset(offset);
|
2014-04-25 04:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebContents::GoBack() {
|
2014-04-25 05:10:16 +00:00
|
|
|
web_contents()->GetController().GoBack();
|
2014-04-25 04:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebContents::GoForward() {
|
2014-04-25 05:10:16 +00:00
|
|
|
web_contents()->GetController().GoForward();
|
2014-04-25 04:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebContents::GoToIndex(int index) {
|
2014-04-25 05:10:16 +00:00
|
|
|
web_contents()->GetController().GoToIndex(index);
|
2014-04-25 04:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebContents::GoToOffset(int offset) {
|
2014-04-25 05:10:16 +00:00
|
|
|
web_contents()->GetController().GoToOffset(offset);
|
2014-04-25 04:52:30 +00:00
|
|
|
}
|
|
|
|
|
2014-04-24 09:00:41 +00:00
|
|
|
int WebContents::GetRoutingID() const {
|
2014-04-25 05:10:16 +00:00
|
|
|
return web_contents()->GetRoutingID();
|
2014-04-24 09:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int WebContents::GetProcessID() const {
|
2014-04-25 05:10:16 +00:00
|
|
|
return web_contents()->GetRenderProcessHost()->GetID();
|
2014-04-24 09:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebContents::IsCrashed() const {
|
2014-04-25 05:10:16 +00:00
|
|
|
return web_contents()->IsCrashed();
|
2014-04-24 09:00:41 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 12:57:44 +00:00
|
|
|
void WebContents::SetUserAgent(const std::string& user_agent) {
|
|
|
|
web_contents()->SetUserAgentOverride(user_agent);
|
|
|
|
}
|
|
|
|
|
2014-10-24 13:04:50 +00:00
|
|
|
void WebContents::InsertCSS(const std::string& css) {
|
|
|
|
web_contents()->InsertCSS(css);
|
|
|
|
}
|
|
|
|
|
2014-06-28 11:36:57 +00:00
|
|
|
void WebContents::ExecuteJavaScript(const base::string16& code) {
|
2014-07-28 07:29:51 +00:00
|
|
|
web_contents()->GetMainFrame()->ExecuteJavaScript(code);
|
2014-04-25 02:34:40 +00:00
|
|
|
}
|
|
|
|
|
2014-11-02 14:34:22 +00:00
|
|
|
void WebContents::OpenDevTools() {
|
2014-11-03 13:48:25 +00:00
|
|
|
storage_->SetCanDock(false);
|
2014-11-03 11:45:09 +00:00
|
|
|
storage_->ShowDevTools();
|
2014-11-02 14:34:22 +00:00
|
|
|
}
|
|
|
|
|
2014-11-03 14:12:56 +00:00
|
|
|
void WebContents::CloseDevTools() {
|
|
|
|
storage_->CloseDevTools();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebContents::IsDevToolsOpened() {
|
|
|
|
return storage_->IsDevToolsViewShowing();
|
|
|
|
}
|
|
|
|
|
2014-06-28 11:36:57 +00:00
|
|
|
bool WebContents::SendIPCMessage(const base::string16& channel,
|
2014-04-25 05:10:16 +00:00
|
|
|
const base::ListValue& args) {
|
|
|
|
return Send(new AtomViewMsg_Message(routing_id(), channel, args));
|
|
|
|
}
|
|
|
|
|
2014-10-24 06:37:14 +00:00
|
|
|
void WebContents::SetAutoSize(bool enabled,
|
|
|
|
const gfx::Size& min_size,
|
|
|
|
const gfx::Size& max_size) {
|
|
|
|
min_auto_size_ = min_size;
|
|
|
|
min_auto_size_.SetToMin(max_size);
|
|
|
|
max_auto_size_ = max_size;
|
|
|
|
max_auto_size_.SetToMax(min_size);
|
|
|
|
|
|
|
|
enabled &= !min_auto_size_.IsEmpty() && !max_auto_size_.IsEmpty();
|
|
|
|
if (!enabled && !auto_size_enabled_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto_size_enabled_ = enabled;
|
|
|
|
|
|
|
|
if (!attached())
|
|
|
|
return;
|
|
|
|
|
|
|
|
content::RenderViewHost* rvh = web_contents()->GetRenderViewHost();
|
|
|
|
if (auto_size_enabled_) {
|
|
|
|
rvh->EnableAutoResize(min_auto_size_, max_auto_size_);
|
|
|
|
} else {
|
|
|
|
rvh->DisableAutoResize(element_size_);
|
|
|
|
guest_size_ = element_size_;
|
|
|
|
GuestSizeChangedDueToAutoSize(guest_size_, element_size_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-24 08:48:21 +00:00
|
|
|
void WebContents::SetAllowTransparency(bool allow) {
|
|
|
|
if (guest_opaque_ != allow)
|
|
|
|
return;
|
|
|
|
|
|
|
|
guest_opaque_ = !allow;
|
|
|
|
if (!web_contents()->GetRenderViewHost()->GetView())
|
|
|
|
return;
|
|
|
|
|
|
|
|
web_contents()->GetRenderViewHost()->GetView()->SetBackgroundOpaque(!allow);
|
|
|
|
}
|
|
|
|
|
2014-04-24 08:45:25 +00:00
|
|
|
mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) {
|
2014-10-23 05:31:10 +00:00
|
|
|
if (template_.IsEmpty())
|
|
|
|
template_.Reset(isolate, mate::ObjectTemplateBuilder(isolate)
|
2014-10-23 06:04:13 +00:00
|
|
|
.SetMethod("destroy", &WebContents::Destroy)
|
2014-10-23 05:31:10 +00:00
|
|
|
.SetMethod("isAlive", &WebContents::IsAlive)
|
2014-11-12 02:28:50 +00:00
|
|
|
.SetMethod("_loadUrl", &WebContents::LoadURL)
|
2014-10-23 05:31:10 +00:00
|
|
|
.SetMethod("getUrl", &WebContents::GetURL)
|
|
|
|
.SetMethod("getTitle", &WebContents::GetTitle)
|
|
|
|
.SetMethod("isLoading", &WebContents::IsLoading)
|
|
|
|
.SetMethod("isWaitingForResponse", &WebContents::IsWaitingForResponse)
|
|
|
|
.SetMethod("stop", &WebContents::Stop)
|
2014-11-12 02:28:50 +00:00
|
|
|
.SetMethod("_reload", &WebContents::Reload)
|
|
|
|
.SetMethod("_reloadIgnoringCache", &WebContents::ReloadIgnoringCache)
|
2014-10-23 05:31:10 +00:00
|
|
|
.SetMethod("canGoBack", &WebContents::CanGoBack)
|
|
|
|
.SetMethod("canGoForward", &WebContents::CanGoForward)
|
|
|
|
.SetMethod("canGoToOffset", &WebContents::CanGoToOffset)
|
|
|
|
.SetMethod("goBack", &WebContents::GoBack)
|
|
|
|
.SetMethod("goForward", &WebContents::GoForward)
|
|
|
|
.SetMethod("goToIndex", &WebContents::GoToIndex)
|
|
|
|
.SetMethod("goToOffset", &WebContents::GoToOffset)
|
|
|
|
.SetMethod("getRoutingId", &WebContents::GetRoutingID)
|
|
|
|
.SetMethod("getProcessId", &WebContents::GetProcessID)
|
|
|
|
.SetMethod("isCrashed", &WebContents::IsCrashed)
|
2014-10-24 12:57:44 +00:00
|
|
|
.SetMethod("setUserAgent", &WebContents::SetUserAgent)
|
2014-10-24 13:04:50 +00:00
|
|
|
.SetMethod("insertCSS", &WebContents::InsertCSS)
|
2014-10-23 05:31:10 +00:00
|
|
|
.SetMethod("_executeJavaScript", &WebContents::ExecuteJavaScript)
|
|
|
|
.SetMethod("_send", &WebContents::SendIPCMessage)
|
2014-10-24 06:37:14 +00:00
|
|
|
.SetMethod("setAutoSize", &WebContents::SetAutoSize)
|
2014-10-24 08:48:21 +00:00
|
|
|
.SetMethod("setAllowTransparency", &WebContents::SetAllowTransparency)
|
2014-10-27 13:56:04 +00:00
|
|
|
.SetMethod("isGuest", &WebContents::is_guest)
|
2014-11-02 14:34:22 +00:00
|
|
|
.SetMethod("openDevTools", &WebContents::OpenDevTools)
|
2014-11-03 14:12:56 +00:00
|
|
|
.SetMethod("closeDevTools", &WebContents::CloseDevTools)
|
|
|
|
.SetMethod("isDevToolsOpened", &WebContents::IsDevToolsOpened)
|
2014-10-23 05:31:10 +00:00
|
|
|
.Build());
|
|
|
|
|
|
|
|
return mate::ObjectTemplateBuilder(
|
|
|
|
isolate, v8::Local<v8::ObjectTemplate>::New(isolate, template_));
|
2014-04-25 08:13:16 +00:00
|
|
|
}
|
|
|
|
|
2014-06-28 11:36:57 +00:00
|
|
|
void WebContents::OnRendererMessage(const base::string16& channel,
|
2014-04-25 08:13:16 +00:00
|
|
|
const base::ListValue& args) {
|
|
|
|
// webContents.emit(channel, new Event(), args...);
|
2014-10-24 06:37:14 +00:00
|
|
|
Emit(base::UTF16ToUTF8(channel), args);
|
2014-04-25 08:13:16 +00:00
|
|
|
}
|
|
|
|
|
2014-06-28 11:36:57 +00:00
|
|
|
void WebContents::OnRendererMessageSync(const base::string16& channel,
|
2014-04-25 08:13:16 +00:00
|
|
|
const base::ListValue& args,
|
|
|
|
IPC::Message* message) {
|
|
|
|
// webContents.emit(channel, new Event(sender, message), args...);
|
2014-07-11 04:19:01 +00:00
|
|
|
Emit(base::UTF16ToUTF8(channel), args, web_contents(), message);
|
2014-04-24 08:45:25 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 06:37:14 +00:00
|
|
|
void WebContents::GuestSizeChangedDueToAutoSize(const gfx::Size& old_size,
|
|
|
|
const gfx::Size& new_size) {
|
|
|
|
base::ListValue args;
|
|
|
|
args.AppendInteger(old_size.width());
|
|
|
|
args.AppendInteger(old_size.height());
|
|
|
|
args.AppendInteger(new_size.width());
|
|
|
|
args.AppendInteger(new_size.height());
|
|
|
|
Emit("size-changed", args);
|
|
|
|
}
|
|
|
|
|
2014-04-24 08:45:25 +00:00
|
|
|
// static
|
2014-10-23 06:04:13 +00:00
|
|
|
mate::Handle<WebContents> WebContents::CreateFrom(
|
2014-04-24 08:45:25 +00:00
|
|
|
v8::Isolate* isolate, content::WebContents* web_contents) {
|
2014-04-29 03:39:31 +00:00
|
|
|
return mate::CreateHandle(isolate, new WebContents(web_contents));
|
2014-04-24 08:45:25 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 06:04:13 +00:00
|
|
|
// static
|
|
|
|
mate::Handle<WebContents> WebContents::Create(
|
|
|
|
v8::Isolate* isolate, const mate::Dictionary& options) {
|
|
|
|
return mate::CreateHandle(isolate, new WebContents(options));
|
|
|
|
}
|
|
|
|
|
2014-04-24 08:45:25 +00:00
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|
2014-10-23 06:04:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
void Initialize(v8::Handle<v8::Object> exports, v8::Handle<v8::Value> unused,
|
|
|
|
v8::Handle<v8::Context> context, void* priv) {
|
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
|
|
|
mate::Dictionary dict(isolate, exports);
|
|
|
|
dict.SetMethod("create", &atom::api::WebContents::Create);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_web_contents, Initialize)
|