Chromium Code Reviews| 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/chrome_view_type.h" | 15 #include "chrome/common/chrome_view_type.h" |
| 16 #include "chrome/common/extensions/extension_messages.h" | 16 #include "chrome/common/extensions/extension_messages.h" |
| 17 #include "chrome/common/render_messages.h" | 17 #include "chrome/common/render_messages.h" |
| 18 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
| 19 #include "chrome/renderer/extensions/chrome_v8_context.h" | 19 #include "chrome/renderer/extensions/chrome_v8_context.h" |
| 20 #include "chrome/renderer/extensions/extension_dispatcher.h" | 20 #include "chrome/renderer/extensions/extension_dispatcher.h" |
| 21 #include "chrome/renderer/extensions/miscellaneous_bindings.h" | 21 #include "chrome/renderer/extensions/miscellaneous_bindings.h" |
| 22 #include "chrome/renderer/extensions/user_script_scheduler.h" | 22 #include "chrome/renderer/extensions/user_script_scheduler.h" |
| 23 #include "chrome/renderer/extensions/user_script_slave.h" | 23 #include "chrome/renderer/extensions/user_script_slave.h" |
| 24 #include "content/public/renderer/render_view.h" | 24 #include "content/public/renderer/render_view.h" |
| 25 #include "content/public/renderer/render_view_visitor.h" | |
| 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" |
| 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | |
| 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 27 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques t.h" | 29 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques t.h" |
| 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedUserGesture. h" | 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedUserGesture. h" |
| 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 30 #include "webkit/glue/image_resource_fetcher.h" | 32 #include "webkit/glue/image_resource_fetcher.h" |
| 31 #include "webkit/glue/resource_fetcher.h" | 33 #include "webkit/glue/resource_fetcher.h" |
| 32 | 34 |
| 33 using extensions::MiscellaneousBindings; | 35 using extensions::MiscellaneousBindings; |
| 34 using WebKit::WebConsoleMessage; | 36 using WebKit::WebConsoleMessage; |
| 35 using WebKit::WebDataSource; | 37 using WebKit::WebDataSource; |
| 36 using WebKit::WebFrame; | 38 using WebKit::WebFrame; |
| 37 using WebKit::WebURLRequest; | 39 using WebKit::WebURLRequest; |
| 38 using WebKit::WebScopedUserGesture; | 40 using WebKit::WebScopedUserGesture; |
| 39 using WebKit::WebView; | 41 using WebKit::WebView; |
| 40 using webkit_glue::ImageResourceFetcher; | 42 using webkit_glue::ImageResourceFetcher; |
| 41 using webkit_glue::ResourceFetcher; | 43 using webkit_glue::ResourceFetcher; |
| 42 | 44 |
| 43 namespace { | 45 namespace { |
| 44 // Keeps a mapping from the frame pointer to a UserScriptScheduler object. | 46 // Keeps a mapping from the frame pointer to a UserScriptScheduler object. |
| 45 // We store this mapping per process, because a frame can jump from one | 47 // We store this mapping per process, because a frame can jump from one |
| 46 // document to another with adoptNode, and so having the object be a | 48 // document to another with adoptNode, and so having the object be a |
| 47 // RenderViewObserver means it might miss some notifications after it moves. | 49 // RenderViewObserver means it might miss some notifications after it moves. |
| 48 typedef std::map<WebFrame*, UserScriptScheduler*> SchedulerMap; | 50 typedef std::map<WebFrame*, UserScriptScheduler*> SchedulerMap; |
| 49 static base::LazyInstance<SchedulerMap> g_schedulers = | 51 static base::LazyInstance<SchedulerMap> g_schedulers = |
| 50 LAZY_INSTANCE_INITIALIZER; | 52 LAZY_INSTANCE_INITIALIZER; |
| 53 | |
| 54 // A RenderViewVisitor class that iterates through the set of available | |
| 55 // views, looking for a view of the given type, in the given browser window | |
| 56 // and within the given extension. | |
| 57 // Used to accumulate the list of views associated with an extension. | |
| 58 class ExtensionViewAccumulator : public content::RenderViewVisitor { | |
| 59 public: | |
| 60 ExtensionViewAccumulator(const std::string& extension_id, | |
| 61 int browser_window_id, | |
| 62 content::ViewType view_type) | |
| 63 : extension_id_(extension_id), | |
| 64 browser_window_id_(browser_window_id), | |
| 65 view_type_(view_type) { | |
| 66 } | |
| 67 | |
| 68 std::vector<content::RenderView*> views() { return views_; } | |
| 69 | |
| 70 // Returns false to terminate the iteration. | |
| 71 virtual bool Visit(content::RenderView* render_view) { | |
| 72 ExtensionHelper* helper = ExtensionHelper::Get(render_view); | |
| 73 if (!ViewTypeMatches(helper->view_type(), view_type_)) | |
| 74 return true; | |
| 75 | |
| 76 GURL url = render_view->GetWebView()->mainFrame()->document().url(); | |
| 77 if (!url.SchemeIs(chrome::kExtensionScheme)) | |
| 78 return true; | |
| 79 const std::string& extension_id = url.host(); | |
| 80 if (extension_id != extension_id_) | |
| 81 return true; | |
| 82 | |
| 83 if (browser_window_id_ != extension_misc::kUnknownWindowId && | |
| 84 helper->browser_window_id() != browser_window_id_) { | |
| 85 return true; | |
| 86 } | |
| 87 | |
| 88 views_.push_back(render_view); | |
| 89 | |
| 90 if (view_type_ == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) | |
| 91 return false; // There can be only one... | |
| 92 return true; | |
| 93 } | |
| 94 | |
| 95 private: | |
| 96 // Returns true is |type| "isa" |match|. | |
|
Yoyo Zhou
2012/04/19 21:47:13
nit: is -> if
Matt Perry
2012/04/19 22:21:53
Done.
| |
| 97 static bool ViewTypeMatches(content::ViewType type, content::ViewType match) { | |
| 98 if (type == match) | |
| 99 return true; | |
| 100 | |
| 101 // INVALID means match all. | |
| 102 if (match == content::VIEW_TYPE_INVALID) | |
| 103 return true; | |
| 104 | |
| 105 return false; | |
| 106 } | |
| 107 | |
| 108 std::string extension_id_; | |
| 109 int browser_window_id_; | |
| 110 content::ViewType view_type_; | |
| 111 std::vector<content::RenderView*> views_; | |
| 112 }; | |
| 113 | |
| 114 } | |
| 115 | |
| 116 // static | |
| 117 std::vector<content::RenderView*> ExtensionHelper::GetExtensionViews( | |
| 118 const std::string& extension_id, | |
| 119 int browser_window_id, | |
| 120 content::ViewType view_type) { | |
| 121 ExtensionViewAccumulator accumulator( | |
| 122 extension_id, browser_window_id, view_type); | |
| 123 content::RenderView::ForEach(&accumulator); | |
| 124 return accumulator.views(); | |
| 125 } | |
| 126 | |
| 127 // static | |
| 128 content::RenderView* ExtensionHelper::GetBackgroundPage( | |
| 129 const std::string& extension_id) { | |
| 130 ExtensionViewAccumulator accumulator( | |
| 131 extension_id, extension_misc::kUnknownWindowId, | |
| 132 chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); | |
| 133 content::RenderView::ForEach(&accumulator); | |
| 134 CHECK_LE(accumulator.views().size(), 1u); | |
| 135 if (accumulator.views().size() == 0) | |
| 136 return NULL; | |
| 137 return accumulator.views()[0]; | |
| 51 } | 138 } |
| 52 | 139 |
| 53 ExtensionHelper::ExtensionHelper(content::RenderView* render_view, | 140 ExtensionHelper::ExtensionHelper(content::RenderView* render_view, |
| 54 ExtensionDispatcher* extension_dispatcher) | 141 ExtensionDispatcher* extension_dispatcher) |
| 55 : content::RenderViewObserver(render_view), | 142 : content::RenderViewObserver(render_view), |
| 56 content::RenderViewObserverTracker<ExtensionHelper>(render_view), | 143 content::RenderViewObserverTracker<ExtensionHelper>(render_view), |
| 57 extension_dispatcher_(extension_dispatcher), | 144 extension_dispatcher_(extension_dispatcher), |
| 58 pending_app_icon_requests_(0), | 145 pending_app_icon_requests_(0), |
| 59 view_type_(content::VIEW_TYPE_INVALID), | 146 view_type_(content::VIEW_TYPE_INVALID), |
| 60 browser_window_id_(-1) { | 147 browser_window_id_(-1) { |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 382 routing_id(), *pending_app_info_)); | 469 routing_id(), *pending_app_info_)); |
| 383 pending_app_info_.reset(NULL); | 470 pending_app_info_.reset(NULL); |
| 384 } | 471 } |
| 385 | 472 |
| 386 void ExtensionHelper::AddErrorToRootConsole(const string16& message) { | 473 void ExtensionHelper::AddErrorToRootConsole(const string16& message) { |
| 387 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 474 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 388 render_view()->GetWebView()->mainFrame()->addMessageToConsole( | 475 render_view()->GetWebView()->mainFrame()->addMessageToConsole( |
| 389 WebConsoleMessage(WebConsoleMessage::LevelError, message)); | 476 WebConsoleMessage(WebConsoleMessage::LevelError, message)); |
| 390 } | 477 } |
| 391 } | 478 } |
| OLD | NEW |