OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/renderer/extensions/extension_helper.h" | 5 #include "chrome/renderer/extensions/extension_helper.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/json/json_string_value_serializer.h" | 10 #include "base/json/json_string_value_serializer.h" |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
15 #include "chrome/common/extensions/extension_messages.h" | 15 #include "chrome/common/extensions/extension_messages.h" |
16 #include "chrome/common/render_messages.h" | 16 #include "chrome/common/render_messages.h" |
17 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
18 #include "chrome/common/view_type.h" | |
19 #include "chrome/renderer/extensions/chrome_v8_context.h" | 18 #include "chrome/renderer/extensions/chrome_v8_context.h" |
20 #include "chrome/renderer/extensions/console.h" | 19 #include "chrome/renderer/extensions/console.h" |
21 #include "chrome/renderer/extensions/dispatcher.h" | 20 #include "chrome/renderer/extensions/dispatcher.h" |
22 #include "chrome/renderer/extensions/miscellaneous_bindings.h" | 21 #include "chrome/renderer/extensions/miscellaneous_bindings.h" |
23 #include "chrome/renderer/extensions/user_script_scheduler.h" | 22 #include "chrome/renderer/extensions/user_script_scheduler.h" |
24 #include "chrome/renderer/extensions/user_script_slave.h" | 23 #include "chrome/renderer/extensions/user_script_slave.h" |
25 #include "content/public/renderer/render_view.h" | 24 #include "content/public/renderer/render_view.h" |
26 #include "content/public/renderer/render_view_visitor.h" | 25 #include "content/public/renderer/render_view_visitor.h" |
27 #include "extensions/common/constants.h" | 26 #include "extensions/common/constants.h" |
28 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLRequest.h" | 27 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLRequest.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 LAZY_INSTANCE_INITIALIZER; | 59 LAZY_INSTANCE_INITIALIZER; |
61 | 60 |
62 // A RenderViewVisitor class that iterates through the set of available | 61 // A RenderViewVisitor class that iterates through the set of available |
63 // views, looking for a view of the given type, in the given browser window | 62 // views, looking for a view of the given type, in the given browser window |
64 // and within the given extension. | 63 // and within the given extension. |
65 // Used to accumulate the list of views associated with an extension. | 64 // Used to accumulate the list of views associated with an extension. |
66 class ViewAccumulator : public content::RenderViewVisitor { | 65 class ViewAccumulator : public content::RenderViewVisitor { |
67 public: | 66 public: |
68 ViewAccumulator(const std::string& extension_id, | 67 ViewAccumulator(const std::string& extension_id, |
69 int browser_window_id, | 68 int browser_window_id, |
70 chrome::ViewType view_type) | 69 ViewType view_type) |
71 : extension_id_(extension_id), | 70 : extension_id_(extension_id), |
72 browser_window_id_(browser_window_id), | 71 browser_window_id_(browser_window_id), |
73 view_type_(view_type) { | 72 view_type_(view_type) { |
74 } | 73 } |
75 | 74 |
76 std::vector<content::RenderView*> views() { return views_; } | 75 std::vector<content::RenderView*> views() { return views_; } |
77 | 76 |
78 // Returns false to terminate the iteration. | 77 // Returns false to terminate the iteration. |
79 virtual bool Visit(content::RenderView* render_view) OVERRIDE { | 78 virtual bool Visit(content::RenderView* render_view) OVERRIDE { |
80 ExtensionHelper* helper = ExtensionHelper::Get(render_view); | 79 ExtensionHelper* helper = ExtensionHelper::Get(render_view); |
81 if (!ViewTypeMatches(helper->view_type(), view_type_)) | 80 if (!ViewTypeMatches(helper->view_type(), view_type_)) |
82 return true; | 81 return true; |
83 | 82 |
84 GURL url = render_view->GetWebView()->mainFrame()->document().url(); | 83 GURL url = render_view->GetWebView()->mainFrame()->document().url(); |
85 if (!url.SchemeIs(extensions::kExtensionScheme)) | 84 if (!url.SchemeIs(kExtensionScheme)) |
86 return true; | 85 return true; |
87 const std::string& extension_id = url.host(); | 86 const std::string& extension_id = url.host(); |
88 if (extension_id != extension_id_) | 87 if (extension_id != extension_id_) |
89 return true; | 88 return true; |
90 | 89 |
91 if (browser_window_id_ != extension_misc::kUnknownWindowId && | 90 if (browser_window_id_ != extension_misc::kUnknownWindowId && |
92 helper->browser_window_id() != browser_window_id_) { | 91 helper->browser_window_id() != browser_window_id_) { |
93 return true; | 92 return true; |
94 } | 93 } |
95 | 94 |
96 views_.push_back(render_view); | 95 views_.push_back(render_view); |
97 | 96 |
98 if (view_type_ == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) | 97 if (view_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) |
99 return false; // There can be only one... | 98 return false; // There can be only one... |
100 return true; | 99 return true; |
101 } | 100 } |
102 | 101 |
103 private: | 102 private: |
104 // Returns true if |type| "isa" |match|. | 103 // Returns true if |type| "isa" |match|. |
105 static bool ViewTypeMatches(chrome::ViewType type, chrome::ViewType match) { | 104 static bool ViewTypeMatches(ViewType type, ViewType match) { |
106 if (type == match) | 105 if (type == match) |
107 return true; | 106 return true; |
108 | 107 |
109 // INVALID means match all. | 108 // INVALID means match all. |
110 if (match == chrome::VIEW_TYPE_INVALID) | 109 if (match == VIEW_TYPE_INVALID) |
111 return true; | 110 return true; |
112 | 111 |
113 return false; | 112 return false; |
114 } | 113 } |
115 | 114 |
116 std::string extension_id_; | 115 std::string extension_id_; |
117 int browser_window_id_; | 116 int browser_window_id_; |
118 chrome::ViewType view_type_; | 117 ViewType view_type_; |
119 std::vector<content::RenderView*> views_; | 118 std::vector<content::RenderView*> views_; |
120 }; | 119 }; |
121 | 120 |
122 } // namespace | 121 } // namespace |
123 | 122 |
124 // static | 123 // static |
125 std::vector<content::RenderView*> ExtensionHelper::GetExtensionViews( | 124 std::vector<content::RenderView*> ExtensionHelper::GetExtensionViews( |
126 const std::string& extension_id, | 125 const std::string& extension_id, |
127 int browser_window_id, | 126 int browser_window_id, |
128 chrome::ViewType view_type) { | 127 ViewType view_type) { |
129 ViewAccumulator accumulator(extension_id, browser_window_id, view_type); | 128 ViewAccumulator accumulator(extension_id, browser_window_id, view_type); |
130 content::RenderView::ForEach(&accumulator); | 129 content::RenderView::ForEach(&accumulator); |
131 return accumulator.views(); | 130 return accumulator.views(); |
132 } | 131 } |
133 | 132 |
134 // static | 133 // static |
135 content::RenderView* ExtensionHelper::GetBackgroundPage( | 134 content::RenderView* ExtensionHelper::GetBackgroundPage( |
136 const std::string& extension_id) { | 135 const std::string& extension_id) { |
137 ViewAccumulator accumulator(extension_id, extension_misc::kUnknownWindowId, | 136 ViewAccumulator accumulator(extension_id, extension_misc::kUnknownWindowId, |
138 chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); | 137 VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); |
139 content::RenderView::ForEach(&accumulator); | 138 content::RenderView::ForEach(&accumulator); |
140 CHECK_LE(accumulator.views().size(), 1u); | 139 CHECK_LE(accumulator.views().size(), 1u); |
141 if (accumulator.views().size() == 0) | 140 if (accumulator.views().size() == 0) |
142 return NULL; | 141 return NULL; |
143 return accumulator.views()[0]; | 142 return accumulator.views()[0]; |
144 } | 143 } |
145 | 144 |
146 ExtensionHelper::ExtensionHelper(content::RenderView* render_view, | 145 ExtensionHelper::ExtensionHelper(content::RenderView* render_view, |
147 Dispatcher* dispatcher) | 146 Dispatcher* dispatcher) |
148 : content::RenderViewObserver(render_view), | 147 : content::RenderViewObserver(render_view), |
149 content::RenderViewObserverTracker<ExtensionHelper>(render_view), | 148 content::RenderViewObserverTracker<ExtensionHelper>(render_view), |
150 dispatcher_(dispatcher), | 149 dispatcher_(dispatcher), |
151 pending_app_icon_requests_(0), | 150 pending_app_icon_requests_(0), |
152 view_type_(chrome::VIEW_TYPE_INVALID), | 151 view_type_(VIEW_TYPE_INVALID), |
153 tab_id_(-1), | 152 tab_id_(-1), |
154 browser_window_id_(-1) { | 153 browser_window_id_(-1) { |
155 } | 154 } |
156 | 155 |
157 ExtensionHelper::~ExtensionHelper() { | 156 ExtensionHelper::~ExtensionHelper() { |
158 } | 157 } |
159 | 158 |
160 bool ExtensionHelper::OnMessageReceived(const IPC::Message& message) { | 159 bool ExtensionHelper::OnMessageReceived(const IPC::Message& message) { |
161 bool handled = true; | 160 bool handled = true; |
162 IPC_BEGIN_MESSAGE_MAP(ExtensionHelper, message) | 161 IPC_BEGIN_MESSAGE_MAP(ExtensionHelper, message) |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 | 209 |
211 void ExtensionHelper::DidStartProvisionalLoad(WebKit::WebFrame* frame) { | 210 void ExtensionHelper::DidStartProvisionalLoad(WebKit::WebFrame* frame) { |
212 SchedulerMap::iterator i = g_schedulers.Get().find(frame); | 211 SchedulerMap::iterator i = g_schedulers.Get().find(frame); |
213 if (i != g_schedulers.Get().end()) | 212 if (i != g_schedulers.Get().end()) |
214 i->second->DidStartProvisionalLoad(); | 213 i->second->DidStartProvisionalLoad(); |
215 } | 214 } |
216 | 215 |
217 void ExtensionHelper::DraggableRegionsChanged(WebKit::WebFrame* frame) { | 216 void ExtensionHelper::DraggableRegionsChanged(WebKit::WebFrame* frame) { |
218 WebKit::WebVector<WebKit::WebDraggableRegion> webregions = | 217 WebKit::WebVector<WebKit::WebDraggableRegion> webregions = |
219 frame->document().draggableRegions(); | 218 frame->document().draggableRegions(); |
220 std::vector<extensions::DraggableRegion> regions; | 219 std::vector<DraggableRegion> regions; |
221 for (size_t i = 0; i < webregions.size(); ++i) { | 220 for (size_t i = 0; i < webregions.size(); ++i) { |
222 extensions::DraggableRegion region; | 221 DraggableRegion region; |
223 region.bounds = webregions[i].bounds; | 222 region.bounds = webregions[i].bounds; |
224 region.draggable = webregions[i].draggable; | 223 region.draggable = webregions[i].draggable; |
225 regions.push_back(region); | 224 regions.push_back(region); |
226 } | 225 } |
227 Send(new ExtensionHostMsg_UpdateDraggableRegions(routing_id(), regions)); | 226 Send(new ExtensionHostMsg_UpdateDraggableRegions(routing_id(), regions)); |
228 } | 227 } |
229 | 228 |
230 void ExtensionHelper::FrameDetached(WebFrame* frame) { | 229 void ExtensionHelper::FrameDetached(WebFrame* frame) { |
231 // This could be called before DidCreateDataSource, in which case the frame | 230 // This could be called before DidCreateDataSource, in which case the frame |
232 // won't be in the map. | 231 // won't be in the map. |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 if (app_info.icons[i].url.SchemeIs(chrome::kDataScheme)) { | 341 if (app_info.icons[i].url.SchemeIs(chrome::kDataScheme)) { |
343 app_info.icons.erase(app_info.icons.begin() + i); | 342 app_info.icons.erase(app_info.icons.begin() + i); |
344 --i; | 343 --i; |
345 } | 344 } |
346 } | 345 } |
347 | 346 |
348 Send(new ExtensionHostMsg_DidGetApplicationInfo( | 347 Send(new ExtensionHostMsg_DidGetApplicationInfo( |
349 routing_id(), page_id, app_info)); | 348 routing_id(), page_id, app_info)); |
350 } | 349 } |
351 | 350 |
352 void ExtensionHelper::OnNotifyRendererViewType(chrome::ViewType type) { | 351 void ExtensionHelper::OnNotifyRendererViewType(ViewType type) { |
353 view_type_ = type; | 352 view_type_ = type; |
354 } | 353 } |
355 | 354 |
356 void ExtensionHelper::OnSetTabId(int init_tab_id) { | 355 void ExtensionHelper::OnSetTabId(int init_tab_id) { |
357 CHECK_EQ(tab_id_, -1); | 356 CHECK_EQ(tab_id_, -1); |
358 CHECK_GE(init_tab_id, 0); | 357 CHECK_GE(init_tab_id, 0); |
359 tab_id_ = init_tab_id; | 358 tab_id_ = init_tab_id; |
360 } | 359 } |
361 | 360 |
362 void ExtensionHelper::OnUpdateBrowserWindowId(int window_id) { | 361 void ExtensionHelper::OnUpdateBrowserWindowId(int window_id) { |
(...skipping 10 matching lines...) Expand all Loading... |
373 v8::Handle<v8::Context> script_context = | 372 v8::Handle<v8::Context> script_context = |
374 render_view()->GetWebView()->mainFrame()->mainWorldScriptContext(); | 373 render_view()->GetWebView()->mainFrame()->mainWorldScriptContext(); |
375 ChromeV8Context* chrome_v8_context = | 374 ChromeV8Context* chrome_v8_context = |
376 dispatcher_->v8_context_set().GetByV8Context(script_context); | 375 dispatcher_->v8_context_set().GetByV8Context(script_context); |
377 if (!chrome_v8_context) | 376 if (!chrome_v8_context) |
378 return; | 377 return; |
379 chrome_v8_context->CallChromeHiddenMethod("OnAppWindowClosed", 0, NULL, NULL); | 378 chrome_v8_context->CallChromeHiddenMethod("OnAppWindowClosed", 0, NULL, NULL); |
380 } | 379 } |
381 | 380 |
382 } // namespace extensions | 381 } // namespace extensions |
OLD | NEW |