From 91320a9e55c7911d0e4dee48e16f6cbffb2526a3 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 23 Jan 2015 20:43:38 -0800 Subject: [PATCH] Remove AtomRendererBindings After the simplification of code this class is no longer needed. --- atom.gyp | 2 - atom/renderer/api/atom_renderer_bindings.cc | 71 --------------------- atom/renderer/api/atom_renderer_bindings.h | 42 ------------ atom/renderer/atom_render_view_observer.cc | 46 +++++++++++-- atom/renderer/atom_renderer_client.cc | 5 +- atom/renderer/atom_renderer_client.h | 6 +- 6 files changed, 45 insertions(+), 127 deletions(-) delete mode 100644 atom/renderer/api/atom_renderer_bindings.cc delete mode 100644 atom/renderer/api/atom_renderer_bindings.h diff --git a/atom.gyp b/atom.gyp index 83da6b453c2a..87188a7049ce 100644 --- a/atom.gyp +++ b/atom.gyp @@ -266,8 +266,6 @@ 'atom/renderer/api/atom_api_spell_check_client.h', 'atom/renderer/api/atom_api_web_frame.cc', 'atom/renderer/api/atom_api_web_frame.h', - 'atom/renderer/api/atom_renderer_bindings.cc', - 'atom/renderer/api/atom_renderer_bindings.h', 'atom/renderer/atom_render_view_observer.cc', 'atom/renderer/atom_render_view_observer.h', 'atom/renderer/atom_renderer_client.cc', diff --git a/atom/renderer/api/atom_renderer_bindings.cc b/atom/renderer/api/atom_renderer_bindings.cc deleted file mode 100644 index 11517606ce99..000000000000 --- a/atom/renderer/api/atom_renderer_bindings.cc +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright (c) 2013 GitHub, Inc. -// Use of this source code is governed by the MIT license that can be -// found in the LICENSE file. - -#include "atom/renderer/api/atom_renderer_bindings.h" - -#include - -#include "atom/common/native_mate_converters/string16_converter.h" -#include "atom/common/native_mate_converters/v8_value_converter.h" -#include "base/memory/scoped_ptr.h" -#include "base/values.h" -#include "content/public/renderer/render_view.h" -#include "third_party/WebKit/public/web/WebFrame.h" -#include "third_party/WebKit/public/web/WebView.h" - -#include "atom/common/node_includes.h" - -namespace atom { - -namespace { - -v8::Handle GetProcessObject(v8::Handle context) { - v8::Handle process = context->Global()->Get( - mate::StringToV8(context->GetIsolate(), "process"))->ToObject(); - DCHECK(!process.IsEmpty()); - - return process; -} - -} // namespace - -AtomRendererBindings::AtomRendererBindings() { -} - -AtomRendererBindings::~AtomRendererBindings() { -} - -void AtomRendererBindings::OnBrowserMessage(content::RenderView* render_view, - const base::string16& channel, - const base::ListValue& args) { - if (!render_view->GetWebView()) - return; - - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - v8::HandleScope handle_scope(isolate); - - v8::Local context = - render_view->GetWebView()->mainFrame()->mainWorldScriptContext(); - if (context.IsEmpty()) - return; - - v8::Context::Scope context_scope(context); - - v8::Handle process = GetProcessObject(context); - scoped_ptr converter(new V8ValueConverter); - - std::vector> arguments; - arguments.reserve(1 + args.GetSize()); - arguments.push_back(mate::ConvertToV8(isolate, channel)); - - for (size_t i = 0; i < args.GetSize(); i++) { - const base::Value* value; - if (args.Get(i, &value)) - arguments.push_back(converter->ToV8Value(value, context)); - } - - node::MakeCallback(isolate, process, "emit", arguments.size(), &arguments[0]); -} - -} // namespace atom diff --git a/atom/renderer/api/atom_renderer_bindings.h b/atom/renderer/api/atom_renderer_bindings.h deleted file mode 100644 index f5f89afb206e..000000000000 --- a/atom/renderer/api/atom_renderer_bindings.h +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright (c) 2013 GitHub, Inc. -// Use of this source code is governed by the MIT license that can be -// found in the LICENSE file. - -#ifndef ATOM_RENDERER_API_ATOM_RENDERER_BINDINGS_H_ -#define ATOM_RENDERER_API_ATOM_RENDERER_BINDINGS_H_ - -#include "atom/common/api/atom_bindings.h" - -#include "base/strings/string16.h" - -namespace base { -class ListValue; -} - -namespace content { -class RenderView; -} - -namespace blink { -class WebFrame; -} - -namespace atom { - -class AtomRendererBindings : public AtomBindings { - public: - AtomRendererBindings(); - virtual ~AtomRendererBindings(); - - // Dispatch messages from browser. - void OnBrowserMessage(content::RenderView* render_view, - const base::string16& channel, - const base::ListValue& args); - - private: - DISALLOW_COPY_AND_ASSIGN(AtomRendererBindings); -}; - -} // namespace atom - -#endif // ATOM_RENDERER_API_ATOM_RENDERER_BINDINGS_H_ diff --git a/atom/renderer/atom_render_view_observer.cc b/atom/renderer/atom_render_view_observer.cc index ceec7d60494a..ebb836fb9a63 100644 --- a/atom/renderer/atom_render_view_observer.cc +++ b/atom/renderer/atom_render_view_observer.cc @@ -8,8 +8,9 @@ #include #include "atom/common/api/api_messages.h" +#include "atom/common/native_mate_converters/string16_converter.h" +#include "atom/common/native_mate_converters/value_converter.h" #include "atom/common/options_switches.h" -#include "atom/renderer/api/atom_renderer_bindings.h" #include "atom/renderer/atom_renderer_client.h" #include "base/command_line.h" #include "base/strings/string_number_conversions.h" @@ -19,14 +20,32 @@ #include "third_party/WebKit/public/web/WebDocument.h" #include "third_party/WebKit/public/web/WebFrame.h" #include "third_party/WebKit/public/web/WebLocalFrame.h" +#include "third_party/WebKit/public/web/WebKit.h" #include "third_party/WebKit/public/web/WebView.h" #include "atom/common/node_includes.h" -using blink::WebFrame; - namespace atom { +namespace { + +v8::Handle GetProcessObject(v8::Isolate* isolate, + v8::Handle context) { + v8::Handle key = mate::StringToV8(isolate, "process"); + return context->Global()->Get(key)->ToObject(); +} + +std::vector> ListValueToVector( + v8::Isolate* isolate, + const base::ListValue& list) { + v8::Handle array = mate::ConvertToV8(isolate, list); + std::vector> result; + mate::ConvertFromV8(isolate, array, &result); + return result; +} + +} // namespace + AtomRenderViewObserver::AtomRenderViewObserver( content::RenderView* render_view, AtomRendererClient* renderer_client) @@ -82,8 +101,25 @@ void AtomRenderViewObserver::OnBrowserMessage(const base::string16& channel, if (!document_created_) return; - renderer_client_->atom_bindings()->OnBrowserMessage( - render_view(), channel, args); + if (!render_view()->GetWebView()) + return; + + blink::WebFrame* frame = render_view()->GetWebView()->mainFrame(); + if (!frame || frame->isWebRemoteFrame()) + return; + + v8::Isolate* isolate = blink::mainThreadIsolate(); + v8::HandleScope handle_scope(isolate); + + v8::Local context = frame->mainWorldScriptContext(); + v8::Context::Scope context_scope(context); + + std::vector> arguments = ListValueToVector( + isolate, args); + arguments.insert(arguments.begin(), mate::ConvertToV8(isolate, channel)); + + v8::Handle process = GetProcessObject(isolate, context); + node::MakeCallback(isolate, process, "emit", arguments.size(), &arguments[0]); } } // namespace atom diff --git a/atom/renderer/atom_renderer_client.cc b/atom/renderer/atom_renderer_client.cc index f40a89028e5f..73e074bc321b 100644 --- a/atom/renderer/atom_renderer_client.cc +++ b/atom/renderer/atom_renderer_client.cc @@ -7,9 +7,9 @@ #include #include +#include "atom/common/api/atom_bindings.h" #include "atom/common/node_bindings.h" #include "atom/common/options_switches.h" -#include "atom/renderer/api/atom_renderer_bindings.h" #include "atom/renderer/atom_render_view_observer.h" #include "chrome/renderer/printing/print_web_view_helper.h" #include "chrome/renderer/tts_dispatcher.h" @@ -18,7 +18,6 @@ #include "content/public/renderer/render_frame_observer.h" #include "content/public/renderer/render_thread.h" #include "base/command_line.h" -#include "native_mate/converter.h" #include "third_party/WebKit/public/web/WebCustomElement.h" #include "third_party/WebKit/public/web/WebFrame.h" #include "third_party/WebKit/public/web/WebPluginParams.h" @@ -67,7 +66,7 @@ class AtomRenderFrameObserver : public content::RenderFrameObserver { AtomRendererClient::AtomRendererClient() : node_bindings_(NodeBindings::Create(false)), - atom_bindings_(new AtomRendererBindings), + atom_bindings_(new AtomBindings), main_frame_(nullptr), is_initialized_(false) { } diff --git a/atom/renderer/atom_renderer_client.h b/atom/renderer/atom_renderer_client.h index 870706b837cf..c52a608cb420 100644 --- a/atom/renderer/atom_renderer_client.h +++ b/atom/renderer/atom_renderer_client.h @@ -12,7 +12,7 @@ namespace atom { -class AtomRendererBindings; +class AtomBindings; class NodeBindings; class AtomRendererClient : public content::ContentRendererClient, @@ -24,8 +24,6 @@ class AtomRendererClient : public content::ContentRendererClient, // Forwarded by RenderFrameObserver. void DidClearWindowObject(); - AtomRendererBindings* atom_bindings() const { return atom_bindings_.get(); } - private: enum NodeIntegration { ALL, @@ -61,7 +59,7 @@ class AtomRendererClient : public content::ContentRendererClient, void EnableWebRuntimeFeatures(); scoped_ptr node_bindings_; - scoped_ptr atom_bindings_; + scoped_ptr atom_bindings_; // The main frame. blink::WebFrame* main_frame_;