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 extensions::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(extensions::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_ == extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) |
Yoyo Zhou
2013/04/01 17:36:51
''
| |
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(extensions::ViewType type, |
105 extensions::ViewType match) { | |
106 if (type == match) | 106 if (type == match) |
107 return true; | 107 return true; |
108 | 108 |
109 // INVALID means match all. | 109 // INVALID means match all. |
110 if (match == chrome::VIEW_TYPE_INVALID) | 110 if (match == extensions::VIEW_TYPE_INVALID) |
111 return true; | 111 return true; |
112 | 112 |
113 return false; | 113 return false; |
114 } | 114 } |
115 | 115 |
116 std::string extension_id_; | 116 std::string extension_id_; |
117 int browser_window_id_; | 117 int browser_window_id_; |
118 chrome::ViewType view_type_; | 118 extensions::ViewType view_type_; |
119 std::vector<content::RenderView*> views_; | 119 std::vector<content::RenderView*> views_; |
120 }; | 120 }; |
121 | 121 |
122 } // namespace | 122 } // namespace |
123 | 123 |
124 // static | 124 // static |
125 std::vector<content::RenderView*> ExtensionHelper::GetExtensionViews( | 125 std::vector<content::RenderView*> ExtensionHelper::GetExtensionViews( |
126 const std::string& extension_id, | 126 const std::string& extension_id, |
127 int browser_window_id, | 127 int browser_window_id, |
128 chrome::ViewType view_type) { | 128 extensions::ViewType view_type) { |
129 ViewAccumulator accumulator(extension_id, browser_window_id, view_type); | 129 ViewAccumulator accumulator(extension_id, browser_window_id, view_type); |
130 content::RenderView::ForEach(&accumulator); | 130 content::RenderView::ForEach(&accumulator); |
131 return accumulator.views(); | 131 return accumulator.views(); |
132 } | 132 } |
133 | 133 |
134 // static | 134 // static |
135 content::RenderView* ExtensionHelper::GetBackgroundPage( | 135 content::RenderView* ExtensionHelper::GetBackgroundPage( |
136 const std::string& extension_id) { | 136 const std::string& extension_id) { |
137 ViewAccumulator accumulator(extension_id, extension_misc::kUnknownWindowId, | 137 ViewAccumulator accumulator(extension_id, extension_misc::kUnknownWindowId, |
138 chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); | 138 extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); |
139 content::RenderView::ForEach(&accumulator); | 139 content::RenderView::ForEach(&accumulator); |
140 CHECK_LE(accumulator.views().size(), 1u); | 140 CHECK_LE(accumulator.views().size(), 1u); |
141 if (accumulator.views().size() == 0) | 141 if (accumulator.views().size() == 0) |
142 return NULL; | 142 return NULL; |
143 return accumulator.views()[0]; | 143 return accumulator.views()[0]; |
144 } | 144 } |
145 | 145 |
146 ExtensionHelper::ExtensionHelper(content::RenderView* render_view, | 146 ExtensionHelper::ExtensionHelper(content::RenderView* render_view, |
147 Dispatcher* dispatcher) | 147 Dispatcher* dispatcher) |
148 : content::RenderViewObserver(render_view), | 148 : content::RenderViewObserver(render_view), |
149 content::RenderViewObserverTracker<ExtensionHelper>(render_view), | 149 content::RenderViewObserverTracker<ExtensionHelper>(render_view), |
150 dispatcher_(dispatcher), | 150 dispatcher_(dispatcher), |
151 pending_app_icon_requests_(0), | 151 pending_app_icon_requests_(0), |
152 view_type_(chrome::VIEW_TYPE_INVALID), | 152 view_type_(extensions::VIEW_TYPE_INVALID), |
153 tab_id_(-1), | 153 tab_id_(-1), |
154 browser_window_id_(-1) { | 154 browser_window_id_(-1) { |
155 } | 155 } |
156 | 156 |
157 ExtensionHelper::~ExtensionHelper() { | 157 ExtensionHelper::~ExtensionHelper() { |
158 } | 158 } |
159 | 159 |
160 bool ExtensionHelper::OnMessageReceived(const IPC::Message& message) { | 160 bool ExtensionHelper::OnMessageReceived(const IPC::Message& message) { |
161 bool handled = true; | 161 bool handled = true; |
162 IPC_BEGIN_MESSAGE_MAP(ExtensionHelper, message) | 162 IPC_BEGIN_MESSAGE_MAP(ExtensionHelper, message) |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
342 if (app_info.icons[i].url.SchemeIs(chrome::kDataScheme)) { | 342 if (app_info.icons[i].url.SchemeIs(chrome::kDataScheme)) { |
343 app_info.icons.erase(app_info.icons.begin() + i); | 343 app_info.icons.erase(app_info.icons.begin() + i); |
344 --i; | 344 --i; |
345 } | 345 } |
346 } | 346 } |
347 | 347 |
348 Send(new ExtensionHostMsg_DidGetApplicationInfo( | 348 Send(new ExtensionHostMsg_DidGetApplicationInfo( |
349 routing_id(), page_id, app_info)); | 349 routing_id(), page_id, app_info)); |
350 } | 350 } |
351 | 351 |
352 void ExtensionHelper::OnNotifyRendererViewType(chrome::ViewType type) { | 352 void ExtensionHelper::OnNotifyRendererViewType(extensions::ViewType type) { |
353 view_type_ = type; | 353 view_type_ = type; |
354 } | 354 } |
355 | 355 |
356 void ExtensionHelper::OnSetTabId(int init_tab_id) { | 356 void ExtensionHelper::OnSetTabId(int init_tab_id) { |
357 CHECK_EQ(tab_id_, -1); | 357 CHECK_EQ(tab_id_, -1); |
358 CHECK_GE(init_tab_id, 0); | 358 CHECK_GE(init_tab_id, 0); |
359 tab_id_ = init_tab_id; | 359 tab_id_ = init_tab_id; |
360 } | 360 } |
361 | 361 |
362 void ExtensionHelper::OnUpdateBrowserWindowId(int window_id) { | 362 void ExtensionHelper::OnUpdateBrowserWindowId(int window_id) { |
(...skipping 10 matching lines...) Expand all Loading... | |
373 v8::Handle<v8::Context> script_context = | 373 v8::Handle<v8::Context> script_context = |
374 render_view()->GetWebView()->mainFrame()->mainWorldScriptContext(); | 374 render_view()->GetWebView()->mainFrame()->mainWorldScriptContext(); |
375 ChromeV8Context* chrome_v8_context = | 375 ChromeV8Context* chrome_v8_context = |
376 dispatcher_->v8_context_set().GetByV8Context(script_context); | 376 dispatcher_->v8_context_set().GetByV8Context(script_context); |
377 if (!chrome_v8_context) | 377 if (!chrome_v8_context) |
378 return; | 378 return; |
379 chrome_v8_context->CallChromeHiddenMethod("OnAppWindowClosed", 0, NULL, NULL); | 379 chrome_v8_context->CallChromeHiddenMethod("OnAppWindowClosed", 0, NULL, NULL); |
380 } | 380 } |
381 | 381 |
382 } // namespace extensions | 382 } // namespace extensions |
OLD | NEW |