1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/renderer/extension_helper.h"
7 #include "content/public/renderer/render_view.h"
8 #include "content/public/renderer/render_view_visitor.h"
9 #include "extensions/common/api/messaging/message.h"
10 #include "extensions/common/constants.h"
11 #include "extensions/common/extension_messages.h"
12 #include "extensions/renderer/console.h"
13 #include "extensions/renderer/dispatcher.h"
14 #include "extensions/renderer/messaging_bindings.h"
15 #include "third_party/WebKit/public/platform/WebURLRequest.h"
16 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
17 #include "third_party/WebKit/public/web/WebDocument.h"
18 #include "third_party/WebKit/public/web/WebLocalFrame.h"
19 #include "third_party/WebKit/public/web/WebView.h"
21 using content::ConsoleMessageLevel;
22 using blink::WebConsoleMessage;
23 using blink::WebDataSource;
24 using blink::WebFrame;
25 using blink::WebLocalFrame;
26 using blink::WebURLRequest;
29 namespace extensions {
33 // A RenderViewVisitor class that iterates through the set of available
34 // views, looking for a view of the given type, in the given browser window
35 // and within the given extension.
36 // Used to accumulate the list of views associated with an extension.
37 class ViewAccumulator : public content::RenderViewVisitor {
39 ViewAccumulator(const std::string& extension_id,
40 int browser_window_id,
42 : extension_id_(extension_id),
43 browser_window_id_(browser_window_id),
44 view_type_(view_type) {
47 std::vector<content::RenderView*> views() { return views_; }
49 // Returns false to terminate the iteration.
50 virtual bool Visit(content::RenderView* render_view) OVERRIDE {
51 ExtensionHelper* helper = ExtensionHelper::Get(render_view);
52 if (!ViewTypeMatches(helper->view_type(), view_type_))
55 GURL url = render_view->GetWebView()->mainFrame()->document().url();
56 if (!url.SchemeIs(kExtensionScheme))
58 const std::string& extension_id = url.host();
59 if (extension_id != extension_id_)
62 if (browser_window_id_ != extension_misc::kUnknownWindowId &&
63 helper->browser_window_id() != browser_window_id_) {
67 views_.push_back(render_view);
69 if (view_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE)
70 return false; // There can be only one...
75 // Returns true if |type| "isa" |match|.
76 static bool ViewTypeMatches(ViewType type, ViewType match) {
80 // INVALID means match all.
81 if (match == VIEW_TYPE_INVALID)
87 std::string extension_id_;
88 int browser_window_id_;
90 std::vector<content::RenderView*> views_;
96 std::vector<content::RenderView*> ExtensionHelper::GetExtensionViews(
97 const std::string& extension_id,
98 int browser_window_id,
100 ViewAccumulator accumulator(extension_id, browser_window_id, view_type);
101 content::RenderView::ForEach(&accumulator);
102 return accumulator.views();
106 content::RenderView* ExtensionHelper::GetBackgroundPage(
107 const std::string& extension_id) {
108 ViewAccumulator accumulator(extension_id, extension_misc::kUnknownWindowId,
109 VIEW_TYPE_EXTENSION_BACKGROUND_PAGE);
110 content::RenderView::ForEach(&accumulator);
111 CHECK_LE(accumulator.views().size(), 1u);
112 if (accumulator.views().size() == 0)
114 return accumulator.views()[0];
117 ExtensionHelper::ExtensionHelper(content::RenderView* render_view,
118 Dispatcher* dispatcher)
119 : content::RenderViewObserver(render_view),
120 content::RenderViewObserverTracker<ExtensionHelper>(render_view),
121 dispatcher_(dispatcher),
122 view_type_(VIEW_TYPE_INVALID),
124 browser_window_id_(-1) {
127 ExtensionHelper::~ExtensionHelper() {
130 bool ExtensionHelper::OnMessageReceived(const IPC::Message& message) {
132 IPC_BEGIN_MESSAGE_MAP(ExtensionHelper, message)
133 IPC_MESSAGE_HANDLER(ExtensionMsg_Response, OnExtensionResponse)
134 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnExtensionMessageInvoke)
135 IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchOnConnect,
136 OnExtensionDispatchOnConnect)
137 IPC_MESSAGE_HANDLER(ExtensionMsg_DeliverMessage, OnExtensionDeliverMessage)
138 IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchOnDisconnect,
139 OnExtensionDispatchOnDisconnect)
140 IPC_MESSAGE_HANDLER(ExtensionMsg_SetTabId, OnSetTabId)
141 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateBrowserWindowId,
142 OnUpdateBrowserWindowId)
143 IPC_MESSAGE_HANDLER(ExtensionMsg_NotifyRenderViewType,
144 OnNotifyRendererViewType)
145 IPC_MESSAGE_HANDLER(ExtensionMsg_AddMessageToConsole,
146 OnAddMessageToConsole)
147 IPC_MESSAGE_HANDLER(ExtensionMsg_AppWindowClosed,
149 IPC_MESSAGE_UNHANDLED(handled = false)
150 IPC_END_MESSAGE_MAP()
154 void ExtensionHelper::DidCreateDocumentElement(WebLocalFrame* frame) {
155 dispatcher_->DidCreateDocumentElement(frame);
158 void ExtensionHelper::DraggableRegionsChanged(blink::WebFrame* frame) {
159 blink::WebVector<blink::WebDraggableRegion> webregions =
160 frame->document().draggableRegions();
161 std::vector<DraggableRegion> regions;
162 for (size_t i = 0; i < webregions.size(); ++i) {
163 DraggableRegion region;
164 region.bounds = webregions[i].bounds;
165 region.draggable = webregions[i].draggable;
166 regions.push_back(region);
168 Send(new ExtensionHostMsg_UpdateDraggableRegions(routing_id(), regions));
171 void ExtensionHelper::DidMatchCSS(
172 blink::WebLocalFrame* frame,
173 const blink::WebVector<blink::WebString>& newly_matching_selectors,
174 const blink::WebVector<blink::WebString>& stopped_matching_selectors) {
175 dispatcher_->DidMatchCSS(
176 frame, newly_matching_selectors, stopped_matching_selectors);
179 void ExtensionHelper::OnExtensionResponse(int request_id,
181 const base::ListValue& response,
182 const std::string& error) {
183 dispatcher_->OnExtensionResponse(request_id,
189 void ExtensionHelper::OnExtensionMessageInvoke(const std::string& extension_id,
190 const std::string& module_name,
191 const std::string& function_name,
192 const base::ListValue& args,
194 dispatcher_->InvokeModuleSystemMethod(
195 render_view(), extension_id, module_name, function_name, args,
199 void ExtensionHelper::OnExtensionDispatchOnConnect(
201 const std::string& channel_name,
202 const base::DictionaryValue& source_tab,
203 const ExtensionMsg_ExternalConnectionInfo& info,
204 const std::string& tls_channel_id) {
205 MessagingBindings::DispatchOnConnect(dispatcher_->script_context_set(),
214 void ExtensionHelper::OnExtensionDeliverMessage(int target_id,
215 const Message& message) {
216 MessagingBindings::DeliverMessage(
217 dispatcher_->script_context_set(), target_id, message, render_view());
220 void ExtensionHelper::OnExtensionDispatchOnDisconnect(
222 const std::string& error_message) {
223 MessagingBindings::DispatchOnDisconnect(
224 dispatcher_->script_context_set(), port_id, error_message, render_view());
227 void ExtensionHelper::OnNotifyRendererViewType(ViewType type) {
231 void ExtensionHelper::OnSetTabId(int init_tab_id) {
232 CHECK_EQ(tab_id_, -1);
233 CHECK_GE(init_tab_id, 0);
234 tab_id_ = init_tab_id;
237 void ExtensionHelper::OnUpdateBrowserWindowId(int window_id) {
238 browser_window_id_ = window_id;
241 void ExtensionHelper::OnAddMessageToConsole(ConsoleMessageLevel level,
242 const std::string& message) {
243 console::AddMessage(render_view(), level, message);
246 void ExtensionHelper::OnAppWindowClosed() {
247 v8::HandleScope scope(v8::Isolate::GetCurrent());
248 v8::Handle<v8::Context> v8_context =
249 render_view()->GetWebView()->mainFrame()->mainWorldScriptContext();
250 ScriptContext* script_context =
251 dispatcher_->script_context_set().GetByV8Context(v8_context);
254 script_context->module_system()->CallModuleMethod("app.window",
255 "onAppWindowClosed");
258 } // namespace extensions