diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 68ed0ba754..5b6b16b435 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -970,15 +970,6 @@ void WebContents::ShowAutofillPopup(content::RenderFrameHost* frame_host, } } -// Should only be used for IPC message maps -bool WebContents::Send(IPC::Message* message) { - auto host = web_contents()->GetRenderViewHost(); - - if (host) - return host->Send(message); - return false; -} - bool WebContents::OnMessageReceived(const IPC::Message& message) { bool handled = true; IPC_BEGIN_MESSAGE_MAP(WebContents, message) @@ -1542,10 +1533,10 @@ void WebContents::TabTraverse(bool reverse) { bool WebContents::SendIPCMessage(bool all_frames, const base::string16& channel, const base::ListValue& args) { - auto host = web_contents()->GetRenderViewHost(); - if (host) { - return host->Send(new AtomViewMsg_Message(host->GetRoutingID(), - all_frames, channel, args)); + auto frame_host = web_contents()->GetMainFrame(); + if (frame_host) { + return frame_host->Send(new AtomFrameMsg_Message( + frame_host->GetRoutingID(), all_frames, channel, args)); } return false; } @@ -1836,18 +1827,15 @@ void WebContents::OnSetTemporaryZoomLevel(content::RenderFrameHost* rfh, IPC::Message* reply_msg) { zoom_controller_->SetTemporaryZoomLevel(level); double new_level = zoom_controller_->GetZoomLevel(); - AtomViewHostMsg_SetTemporaryZoomLevel::WriteReplyParams(reply_msg, new_level); - - auto host = web_contents()->GetRenderViewHost(); - if (host) - host->Send(reply_msg); + AtomFrameHostMsg_SetTemporaryZoomLevel::WriteReplyParams(reply_msg, + new_level); + rfh->Send(reply_msg); } -void WebContents::OnGetZoomLevel(IPC::Message* reply_msg) { - AtomViewHostMsg_GetZoomLevel::WriteReplyParams(reply_msg, GetZoomLevel()); - auto host = web_contents()->GetRenderViewHost(); - if (host) - host->Send(reply_msg); +void WebContents::OnGetZoomLevel(content::RenderFrameHost* rfh, + IPC::Message* reply_msg) { + AtomFrameHostMsg_GetZoomLevel::WriteReplyParams(reply_msg, GetZoomLevel()); + rfh->Send(reply_msg); } v8::Local WebContents::GetWebPreferences(v8::Isolate* isolate) { diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 96bbd42650..00e4b5a84a 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -149,9 +149,6 @@ class WebContents : public mate::TrackableObject, void AddWorkSpace(mate::Arguments* args, const base::FilePath& path); void RemoveWorkSpace(mate::Arguments* args, const base::FilePath& path); - // IPC - bool Send(IPC::Message* message); - // Editing commands. void Undo(); void Redo(); diff --git a/atom/browser/api/event.cc b/atom/browser/api/event.cc index 673576c6d6..1ee0d57e36 100644 --- a/atom/browser/api/event.cc +++ b/atom/browser/api/event.cc @@ -6,7 +6,7 @@ #include "atom/common/api/api_messages.h" #include "atom/common/native_mate_converters/string16_converter.h" -#include "content/public/browser/render_view_host.h" +#include "content/public/browser/render_frame_host.h" #include "content/public/browser/web_contents.h" #include "native_mate/object_template_builder.h" @@ -60,10 +60,8 @@ bool Event::SendReply(const base::string16& json) { if (message_ == nullptr || sender_ == nullptr) return false; - AtomViewHostMsg_Message_Sync::WriteReplyParams(message_, json); - auto host = sender_->GetRenderViewHost(); - if (!host) return false; - bool success = host->Send(message_); + AtomFrameHostMsg_Message_Sync::WriteReplyParams(message_, json); + bool success = sender_->Send(message_); message_ = nullptr; sender_ = nullptr; return success; diff --git a/atom/common/api/remote_callback_freer.cc b/atom/common/api/remote_callback_freer.cc index d91891e55f..c15d5389a2 100644 --- a/atom/common/api/remote_callback_freer.cc +++ b/atom/common/api/remote_callback_freer.cc @@ -7,7 +7,7 @@ #include "atom/common/api/api_messages.h" #include "base/strings/utf_string_conversions.h" #include "base/values.h" -#include "content/public/browser/render_view_host.h" +#include "content/public/browser/render_frame_host.h" #include "content/public/browser/web_contents.h" namespace atom { @@ -37,10 +37,10 @@ void RemoteCallbackFreer::RunDestructor() { base::ASCIIToUTF16("ELECTRON_RENDERER_RELEASE_CALLBACK"); base::ListValue args; args.AppendInteger(object_id_); - auto host = web_contents()->GetRenderViewHost(); - if (host) { - host->Send(new AtomViewMsg_Message( - host->GetRoutingID(), false, channel, args)); + auto frame_host = web_contents()->GetMainFrame(); + if (frame_host) { + frame_host->Send(new AtomFrameMsg_Message(frame_host->GetRoutingID(), false, + channel, args)); } Observe(nullptr); diff --git a/atom/renderer/api/atom_api_renderer_ipc.cc b/atom/renderer/api/atom_api_renderer_ipc.cc index 483f5639bf..dcb59bdb07 100644 --- a/atom/renderer/api/atom_api_renderer_ipc.cc +++ b/atom/renderer/api/atom_api_renderer_ipc.cc @@ -10,7 +10,6 @@ #include "content/public/renderer/render_frame.h" #include "native_mate/dictionary.h" #include "third_party/WebKit/public/web/WebLocalFrame.h" -#include "third_party/WebKit/public/web/WebView.h" using content::RenderFrame; using blink::WebLocalFrame; diff --git a/atom/renderer/atom_render_frame_observer.cc b/atom/renderer/atom_render_frame_observer.cc index 4ddf9186b7..07b634331a 100644 --- a/atom/renderer/atom_render_frame_observer.cc +++ b/atom/renderer/atom_render_frame_observer.cc @@ -7,8 +7,8 @@ #include #include +// Put this before event_emitter_caller.h to have string16 support. #include "atom/common/native_mate_converters/string16_converter.h" - #include "atom/common/api/api_messages.h" #include "atom/common/api/event_emitter_caller.h" #include "atom/common/native_mate_converters/value_converter.h" @@ -127,6 +127,46 @@ void AtomRenderFrameObserver::OnDestruct() { delete this; } +bool AtomRenderFrameObserver::OnMessageReceived(const IPC::Message& message) { + bool handled = true; + IPC_BEGIN_MESSAGE_MAP(AtomRenderFrameObserver, message) + IPC_MESSAGE_HANDLER(AtomFrameMsg_Message, OnBrowserMessage) + IPC_MESSAGE_UNHANDLED(handled = false) + IPC_END_MESSAGE_MAP() + + return handled; +} + +void AtomRenderFrameObserver::OnBrowserMessage(bool send_to_all, + const base::string16& channel, + const base::ListValue& args) { + blink::WebLocalFrame* frame = render_frame_->GetWebFrame(); + if (!frame || !render_frame_->IsMainFrame()) + return; + + // Don't handle browser messages before document element is created. + // When we receive a message from the browser, we try to transfer it + // to a web page, and when we do that Blink creates an empty + // document element if it hasn't been created yet, and it makes our init + // script to run while `window.location` is still "about:blank". + blink::WebDocument document = frame->GetDocument(); + blink::WebElement html_element = document.DocumentElement(); + if (html_element.IsNull()) { + return; + } + + EmitIPCEvent(frame, channel, args); + + // Also send the message to all sub-frames. + if (send_to_all) { + for (blink::WebFrame* child = frame->FirstChild(); child; + child = child->NextSibling()) + if (child->IsWebLocalFrame()) { + EmitIPCEvent(child->ToWebLocalFrame(), channel, args); + } + } +} + void AtomRenderFrameObserver::CreateIsolatedWorldContext() { auto frame = render_frame_->GetWebFrame(); diff --git a/atom/renderer/atom_render_view_observer.cc b/atom/renderer/atom_render_view_observer.cc index 1a425dfa1d..30bd8d004a 100644 --- a/atom/renderer/atom_render_view_observer.cc +++ b/atom/renderer/atom_render_view_observer.cc @@ -14,7 +14,8 @@ namespace atom { AtomRenderViewObserver::AtomRenderViewObserver(content::RenderView* render_view) : content::RenderViewObserver(render_view) {} -AtomRenderViewObserver::~AtomRenderViewObserver() {} +AtomRenderViewObserver::~AtomRenderViewObserver() { +} bool AtomRenderViewObserver::OnMessageReceived(const IPC::Message& message) { bool handled = true;