[email protected] | a76226d | 2012-04-11 07:58:29 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 27131e7 | 2011-10-06 03:34:56 | [diff] [blame] | 5 | #include "chrome/renderer/extensions/chrome_v8_context_set.h" |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 6 | |
| 7 | #include "base/logging.h" |
| 8 | #include "base/message_loop.h" |
| 9 | #include "base/tracked_objects.h" |
[email protected] | 52647690 | 2011-10-06 20:34:06 | [diff] [blame] | 10 | #include "base/values.h" |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 11 | #include "chrome/common/extensions/extension.h" |
[email protected] | 2a80aee | 2011-10-07 16:06:03 | [diff] [blame] | 12 | #include "chrome/common/url_constants.h" |
[email protected] | 27131e7 | 2011-10-06 03:34:56 | [diff] [blame] | 13 | #include "chrome/renderer/extensions/chrome_v8_context.h" |
[email protected] | 52647690 | 2011-10-06 20:34:06 | [diff] [blame] | 14 | #include "content/public/renderer/render_thread.h" |
[email protected] | a2ef54c | 2011-10-10 16:20:31 | [diff] [blame] | 15 | #include "content/public/renderer/render_view.h" |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame^] | 16 | #include "content/public/renderer/v8_value_converter.h" |
| 17 | #include "extensions/common/constants.h" |
[email protected] | 75bff52 | 2011-12-03 00:04:20 | [diff] [blame] | 18 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" |
| 19 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLRequest.h" |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame^] | 20 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 21 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 22 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame^] | 23 | #include "v8/include/v8.h" |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 24 | |
[email protected] | 52647690 | 2011-10-06 20:34:06 | [diff] [blame] | 25 | using content::RenderThread; |
[email protected] | 8d86f13d | 2011-10-04 17:01:19 | [diff] [blame] | 26 | using content::V8ValueConverter; |
| 27 | |
[email protected] | 8fe74bf | 2012-08-07 21:08:42 | [diff] [blame] | 28 | namespace extensions { |
| 29 | |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | // Returns true if the extension running in the given |render_view| has |
| 33 | // sufficient permissions to access the data. |
| 34 | // |
| 35 | // TODO(aa): This looks super suspicious. Is it correct? Can we use something |
| 36 | // else already in the system? Should it be moved elsewhere? |
[email protected] | a2ef54c | 2011-10-10 16:20:31 | [diff] [blame] | 37 | bool HasSufficientPermissions(content::RenderView* render_view, |
| 38 | const GURL& event_url) { |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 39 | // During unit tests, we might be invoked without a v8 context. In these |
| 40 | // cases, we only allow empty event_urls and short-circuit before retrieving |
| 41 | // the render view from the current context. |
| 42 | if (!event_url.is_valid()) |
| 43 | return true; |
| 44 | |
| 45 | WebKit::WebDocument document = |
[email protected] | a2ef54c | 2011-10-10 16:20:31 | [diff] [blame] | 46 | render_view->GetWebView()->mainFrame()->document(); |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame^] | 47 | return GURL(document.url()).SchemeIs(extensions::kExtensionScheme) && |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 48 | document.securityOrigin().canRequest(event_url); |
| 49 | } |
| 50 | |
| 51 | } |
| 52 | |
[email protected] | 27131e7 | 2011-10-06 03:34:56 | [diff] [blame] | 53 | ChromeV8ContextSet::ChromeV8ContextSet() { |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 54 | } |
[email protected] | 27131e7 | 2011-10-06 03:34:56 | [diff] [blame] | 55 | ChromeV8ContextSet::~ChromeV8ContextSet() { |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 56 | } |
| 57 | |
[email protected] | 27131e7 | 2011-10-06 03:34:56 | [diff] [blame] | 58 | int ChromeV8ContextSet::size() const { |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 59 | return static_cast<int>(contexts_.size()); |
| 60 | } |
| 61 | |
[email protected] | 27131e7 | 2011-10-06 03:34:56 | [diff] [blame] | 62 | void ChromeV8ContextSet::Add(ChromeV8Context* context) { |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 63 | #ifndef NDEBUG |
| 64 | // It's OK to insert the same context twice, but we should only ever have one |
[email protected] | 27131e7 | 2011-10-06 03:34:56 | [diff] [blame] | 65 | // ChromeV8Context per v8::Context. |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 66 | for (ContextSet::iterator iter = contexts_.begin(); iter != contexts_.end(); |
| 67 | ++iter) { |
[email protected] | 27131e7 | 2011-10-06 03:34:56 | [diff] [blame] | 68 | ChromeV8Context* candidate = *iter; |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 69 | if (candidate != context) |
| 70 | DCHECK(candidate->v8_context() != context->v8_context()); |
| 71 | } |
| 72 | #endif |
| 73 | contexts_.insert(context); |
| 74 | } |
| 75 | |
[email protected] | 27131e7 | 2011-10-06 03:34:56 | [diff] [blame] | 76 | void ChromeV8ContextSet::Remove(ChromeV8Context* context) { |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 77 | if (contexts_.erase(context)) { |
| 78 | context->clear_web_frame(); |
[email protected] | 38024409 | 2011-10-07 17:26:27 | [diff] [blame] | 79 | MessageLoop::current()->DeleteSoon(FROM_HERE, context); |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
[email protected] | 27131e7 | 2011-10-06 03:34:56 | [diff] [blame] | 83 | ChromeV8ContextSet::ContextSet ChromeV8ContextSet::GetAll() |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 84 | const { |
| 85 | return contexts_; |
| 86 | } |
| 87 | |
[email protected] | 27131e7 | 2011-10-06 03:34:56 | [diff] [blame] | 88 | ChromeV8Context* ChromeV8ContextSet::GetCurrent() const { |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 89 | if (!v8::Context::InContext()) |
| 90 | return NULL; |
| 91 | else |
| 92 | return GetByV8Context(v8::Context::GetCurrent()); |
| 93 | } |
| 94 | |
[email protected] | 27131e7 | 2011-10-06 03:34:56 | [diff] [blame] | 95 | ChromeV8Context* ChromeV8ContextSet::GetByV8Context( |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 96 | v8::Handle<v8::Context> v8_context) const { |
| 97 | for (ContextSet::const_iterator iter = contexts_.begin(); |
| 98 | iter != contexts_.end(); ++iter) { |
| 99 | if ((*iter)->v8_context() == v8_context) |
| 100 | return *iter; |
| 101 | } |
| 102 | |
| 103 | return NULL; |
| 104 | } |
| 105 | |
[email protected] | 27131e7 | 2011-10-06 03:34:56 | [diff] [blame] | 106 | void ChromeV8ContextSet::DispatchChromeHiddenMethod( |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 107 | const std::string& extension_id, |
| 108 | const std::string& method_name, |
| 109 | const base::ListValue& arguments, |
[email protected] | a2ef54c | 2011-10-10 16:20:31 | [diff] [blame] | 110 | content::RenderView* render_view, |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 111 | const GURL& event_url) const { |
| 112 | v8::HandleScope handle_scope; |
| 113 | |
| 114 | // We copy the context list, because calling into javascript may modify it |
| 115 | // out from under us. |
| 116 | ContextSet contexts = GetAll(); |
| 117 | |
[email protected] | 8d86f13d | 2011-10-04 17:01:19 | [diff] [blame] | 118 | scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 119 | for (ContextSet::iterator it = contexts.begin(); it != contexts.end(); |
| 120 | ++it) { |
| 121 | if ((*it)->v8_context().IsEmpty()) |
| 122 | continue; |
| 123 | |
[email protected] | a76226d | 2012-04-11 07:58:29 | [diff] [blame] | 124 | if (!extension_id.empty()) { |
[email protected] | 8fe74bf | 2012-08-07 21:08:42 | [diff] [blame] | 125 | const Extension* extension = (*it)->extension(); |
[email protected] | a76226d | 2012-04-11 07:58:29 | [diff] [blame] | 126 | if (!extension || (extension_id != extension->id())) |
| 127 | continue; |
| 128 | } |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 129 | |
[email protected] | a2ef54c | 2011-10-10 16:20:31 | [diff] [blame] | 130 | content::RenderView* context_render_view = (*it)->GetRenderView(); |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 131 | if (!context_render_view) |
| 132 | continue; |
| 133 | |
| 134 | if (render_view && render_view != context_render_view) |
| 135 | continue; |
| 136 | |
| 137 | if (!HasSufficientPermissions(context_render_view, event_url)) |
| 138 | continue; |
| 139 | |
| 140 | v8::Local<v8::Context> context(*((*it)->v8_context())); |
| 141 | std::vector<v8::Handle<v8::Value> > v8_arguments; |
| 142 | for (size_t i = 0; i < arguments.GetSize(); ++i) { |
[email protected] | 5d30f92bf | 2012-08-03 08:43:37 | [diff] [blame] | 143 | const base::Value* item = NULL; |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 144 | CHECK(arguments.Get(i, &item)); |
[email protected] | 8d86f13d | 2011-10-04 17:01:19 | [diff] [blame] | 145 | v8_arguments.push_back(converter->ToV8Value(item, context)); |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 146 | } |
| 147 | |
[email protected] | 2df5db74 | 2011-10-12 01:37:22 | [diff] [blame] | 148 | v8::Handle<v8::Value> retval; |
| 149 | (*it)->CallChromeHiddenMethod( |
| 150 | method_name, v8_arguments.size(), &v8_arguments[0], &retval); |
[email protected] | 2ee1e3a | 2011-10-04 15:04:04 | [diff] [blame] | 151 | } |
| 152 | } |
[email protected] | 8fe74bf | 2012-08-07 21:08:42 | [diff] [blame] | 153 | |
| 154 | } // namespace extensions |