Mechanical change for updating the paths to Notification Blink API files.
[chromium-blink-merge.git] / extensions / renderer / extension_helper.cc
blob100d3429b3e2345a14854fd0fd0a7fbde83fdcfd
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/constants.h"
10 #include "extensions/common/extension_messages.h"
11 #include "extensions/common/permissions/permissions_data.h"
12 #include "extensions/common/url_pattern_set.h"
13 #include "extensions/renderer/api/automation/automation_api_helper.h"
14 #include "extensions/renderer/console.h"
15 #include "extensions/renderer/dispatcher.h"
16 #include "third_party/WebKit/public/platform/WebURLRequest.h"
17 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
18 #include "third_party/WebKit/public/web/WebDocument.h"
19 #include "third_party/WebKit/public/web/WebLocalFrame.h"
20 #include "third_party/WebKit/public/web/WebView.h"
22 using content::ConsoleMessageLevel;
23 using blink::WebConsoleMessage;
24 using blink::WebDataSource;
25 using blink::WebFrame;
26 using blink::WebLocalFrame;
27 using blink::WebURLRequest;
28 using blink::WebView;
30 namespace extensions {
32 namespace {
34 // A RenderViewVisitor class that iterates through the set of available
35 // views, looking for a view of the given type, in the given browser window
36 // and within the given extension.
37 // Used to accumulate the list of views associated with an extension.
38 class ViewAccumulator : public content::RenderViewVisitor {
39 public:
40 ViewAccumulator(const std::string& extension_id,
41 int browser_window_id,
42 ViewType view_type)
43 : extension_id_(extension_id),
44 browser_window_id_(browser_window_id),
45 view_type_(view_type) {
48 std::vector<content::RenderView*> views() { return views_; }
50 // Returns false to terminate the iteration.
51 bool Visit(content::RenderView* render_view) override {
52 ExtensionHelper* helper = ExtensionHelper::Get(render_view);
53 if (!ViewTypeMatches(helper->view_type(), view_type_))
54 return true;
56 GURL url = render_view->GetWebView()->mainFrame()->document().url();
57 if (!url.SchemeIs(kExtensionScheme))
58 return true;
59 const std::string& extension_id = url.host();
60 if (extension_id != extension_id_)
61 return true;
63 if (browser_window_id_ != extension_misc::kUnknownWindowId &&
64 helper->browser_window_id() != browser_window_id_) {
65 return true;
68 views_.push_back(render_view);
70 if (view_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE)
71 return false; // There can be only one...
72 return true;
75 private:
76 // Returns true if |type| "isa" |match|.
77 static bool ViewTypeMatches(ViewType type, ViewType match) {
78 if (type == match)
79 return true;
81 // INVALID means match all.
82 if (match == VIEW_TYPE_INVALID)
83 return true;
85 return false;
88 std::string extension_id_;
89 int browser_window_id_;
90 ViewType view_type_;
91 std::vector<content::RenderView*> views_;
94 } // namespace
96 // static
97 std::vector<content::RenderView*> ExtensionHelper::GetExtensionViews(
98 const std::string& extension_id,
99 int browser_window_id,
100 ViewType view_type) {
101 ViewAccumulator accumulator(extension_id, browser_window_id, view_type);
102 content::RenderView::ForEach(&accumulator);
103 return accumulator.views();
106 // static
107 content::RenderView* ExtensionHelper::GetBackgroundPage(
108 const std::string& extension_id) {
109 ViewAccumulator accumulator(extension_id, extension_misc::kUnknownWindowId,
110 VIEW_TYPE_EXTENSION_BACKGROUND_PAGE);
111 content::RenderView::ForEach(&accumulator);
112 CHECK_LE(accumulator.views().size(), 1u);
113 if (accumulator.views().size() == 0)
114 return NULL;
115 return accumulator.views()[0];
118 ExtensionHelper::ExtensionHelper(content::RenderView* render_view,
119 Dispatcher* dispatcher)
120 : content::RenderViewObserver(render_view),
121 content::RenderViewObserverTracker<ExtensionHelper>(render_view),
122 dispatcher_(dispatcher),
123 view_type_(VIEW_TYPE_INVALID),
124 tab_id_(-1),
125 browser_window_id_(-1) {
126 // Lifecycle managed by RenderViewObserver.
127 new AutomationApiHelper(render_view);
130 ExtensionHelper::~ExtensionHelper() {
133 bool ExtensionHelper::OnMessageReceived(const IPC::Message& message) {
134 bool handled = true;
135 IPC_BEGIN_MESSAGE_MAP(ExtensionHelper, message)
136 IPC_MESSAGE_HANDLER(ExtensionMsg_Response, OnExtensionResponse)
137 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnExtensionMessageInvoke)
138 IPC_MESSAGE_HANDLER(ExtensionMsg_SetFrameName, OnSetFrameName)
139 IPC_MESSAGE_HANDLER(ExtensionMsg_SetTabId, OnSetTabId)
140 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateBrowserWindowId,
141 OnUpdateBrowserWindowId)
142 IPC_MESSAGE_HANDLER(ExtensionMsg_NotifyRenderViewType,
143 OnNotifyRendererViewType)
144 IPC_MESSAGE_HANDLER(ExtensionMsg_AddMessageToConsole,
145 OnAddMessageToConsole)
146 IPC_MESSAGE_HANDLER(ExtensionMsg_AppWindowClosed,
147 OnAppWindowClosed)
148 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateTabSpecificPermissions,
149 OnUpdateTabSpecificPermissions)
150 IPC_MESSAGE_HANDLER(ExtensionMsg_ClearTabSpecificPermissions,
151 OnClearTabSpecificPermissions)
152 IPC_MESSAGE_UNHANDLED(handled = false)
153 IPC_END_MESSAGE_MAP()
154 return handled;
157 void ExtensionHelper::DidCreateDocumentElement(WebLocalFrame* frame) {
158 dispatcher_->DidCreateDocumentElement(frame);
161 void ExtensionHelper::DraggableRegionsChanged(blink::WebFrame* frame) {
162 blink::WebVector<blink::WebDraggableRegion> webregions =
163 frame->document().draggableRegions();
164 std::vector<DraggableRegion> regions;
165 for (size_t i = 0; i < webregions.size(); ++i) {
166 DraggableRegion region;
167 region.bounds = webregions[i].bounds;
168 region.draggable = webregions[i].draggable;
169 regions.push_back(region);
171 Send(new ExtensionHostMsg_UpdateDraggableRegions(routing_id(), regions));
174 void ExtensionHelper::DidMatchCSS(
175 blink::WebLocalFrame* frame,
176 const blink::WebVector<blink::WebString>& newly_matching_selectors,
177 const blink::WebVector<blink::WebString>& stopped_matching_selectors) {
178 dispatcher_->DidMatchCSS(
179 frame, newly_matching_selectors, stopped_matching_selectors);
182 void ExtensionHelper::OnExtensionResponse(int request_id,
183 bool success,
184 const base::ListValue& response,
185 const std::string& error) {
186 dispatcher_->OnExtensionResponse(request_id,
187 success,
188 response,
189 error);
192 void ExtensionHelper::OnExtensionMessageInvoke(const std::string& extension_id,
193 const std::string& module_name,
194 const std::string& function_name,
195 const base::ListValue& args,
196 bool user_gesture) {
197 dispatcher_->InvokeModuleSystemMethod(
198 render_view(), extension_id, module_name, function_name, args,
199 user_gesture);
202 void ExtensionHelper::OnNotifyRendererViewType(ViewType type) {
203 view_type_ = type;
206 void ExtensionHelper::OnSetFrameName(const std::string& name) {
207 blink::WebView* web_view = render_view()->GetWebView();
208 if (web_view)
209 web_view->mainFrame()->setName(blink::WebString::fromUTF8(name));
212 void ExtensionHelper::OnSetTabId(int init_tab_id) {
213 CHECK_EQ(tab_id_, -1);
214 CHECK_GE(init_tab_id, 0);
215 tab_id_ = init_tab_id;
218 void ExtensionHelper::OnUpdateBrowserWindowId(int window_id) {
219 browser_window_id_ = window_id;
222 void ExtensionHelper::OnAddMessageToConsole(ConsoleMessageLevel level,
223 const std::string& message) {
224 console::AddMessage(render_view(), level, message);
227 void ExtensionHelper::OnAppWindowClosed() {
228 v8::HandleScope scope(v8::Isolate::GetCurrent());
229 v8::Handle<v8::Context> v8_context =
230 render_view()->GetWebView()->mainFrame()->mainWorldScriptContext();
231 ScriptContext* script_context =
232 dispatcher_->script_context_set().GetByV8Context(v8_context);
233 if (!script_context)
234 return;
235 script_context->module_system()->CallModuleMethod("app.window",
236 "onAppWindowClosed");
239 void ExtensionHelper::OnUpdateTabSpecificPermissions(
240 const GURL& url,
241 const std::string& extension_id,
242 const extensions::URLPatternSet& origin_set) {
243 // Check against the URL to avoid races.
244 GURL active_url(render_view()->GetWebView()->mainFrame()->document().url());
245 if (active_url != url)
246 return;
248 const Extension* extension = dispatcher_->extensions()->GetByID(extension_id);
249 if (!extension)
250 return;
252 extension->permissions_data()->UpdateTabSpecificPermissions(
253 tab_id_,
254 new extensions::PermissionSet(extensions::APIPermissionSet(),
255 extensions::ManifestPermissionSet(),
256 origin_set,
257 extensions::URLPatternSet()));
260 void ExtensionHelper::OnClearTabSpecificPermissions(
261 const std::vector<std::string>& extension_ids) {
262 for (const auto& id : extension_ids) {
263 const extensions::Extension* extension =
264 dispatcher_->extensions()->GetByID(id);
265 if (extension)
266 extension->permissions_data()->ClearTabSpecificPermissions(tab_id_);
270 } // namespace extensions