| 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/chrome_v8_extension.h" | 5 #include "chrome/renderer/extensions/chrome_v8_extension.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "chrome/common/extensions/extension.h" | 11 #include "chrome/common/extensions/extension.h" |
| 12 #include "chrome/common/extensions/extension_set.h" | 12 #include "chrome/common/extensions/extension_set.h" |
| 13 #include "chrome/common/extensions/api/extension_api.h" | 13 #include "chrome/common/extensions/api/extension_api.h" |
| 14 #include "chrome/renderer/extensions/chrome_v8_context.h" | 14 #include "chrome/renderer/extensions/chrome_v8_context.h" |
| 15 #include "chrome/renderer/extensions/dispatcher.h" | 15 #include "chrome/renderer/extensions/dispatcher.h" |
| 16 #include "content/public/renderer/render_view.h" | 16 #include "content/public/renderer/render_view.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 20 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
| 21 | 21 |
| 22 using WebKit::WebDocument; | 22 using WebKit::WebDocument; |
| 23 using WebKit::WebFrame; | 23 using WebKit::WebFrame; |
| 24 using WebKit::WebView; | 24 using WebKit::WebView; |
| 25 | 25 |
| 26 namespace extensions { | 26 namespace extensions { |
| 27 | 27 |
| 28 ChromeV8Extension::ChromeV8Extension(Dispatcher* dispatcher) | 28 namespace { |
| 29 : ObjectBackedNativeHandler(v8::Context::GetCurrent()), | 29 |
| 30 dispatcher_(dispatcher) { | 30 static base::LazyInstance<ChromeV8Extension::InstanceSet> g_instances = |
| 31 LAZY_INSTANCE_INITIALIZER; |
| 32 |
| 33 } // namespace |
| 34 |
| 35 // static |
| 36 content::RenderView* ChromeV8Extension::GetCurrentRenderView() { |
| 37 WebFrame* webframe = WebFrame::frameForCurrentContext(); |
| 38 DCHECK(webframe) << "RetrieveCurrentFrame called when not in a V8 context."; |
| 39 if (!webframe) |
| 40 return NULL; |
| 41 |
| 42 WebView* webview = webframe->view(); |
| 43 if (!webview) |
| 44 return NULL; // can happen during closing |
| 45 |
| 46 content::RenderView* renderview = content::RenderView::FromWebView(webview); |
| 47 DCHECK(renderview) << "Encountered a WebView without a WebViewDelegate"; |
| 48 return renderview; |
| 31 } | 49 } |
| 32 | 50 |
| 33 ChromeV8Extension::ChromeV8Extension(Dispatcher* dispatcher, | 51 ChromeV8Extension::ChromeV8Extension(Dispatcher* dispatcher) |
| 34 v8::Handle<v8::Context> context) | 52 // TODO(svenpanne) It would be nice to remove the GetCurrent() call and use |
| 35 : ObjectBackedNativeHandler(context), | 53 // an additional constructor parameter instead, but this would involve too |
| 54 // many changes for now. |
| 55 : NativeHandler(v8::Isolate::GetCurrent()), |
| 36 dispatcher_(dispatcher) { | 56 dispatcher_(dispatcher) { |
| 57 g_instances.Get().insert(this); |
| 37 } | 58 } |
| 38 | 59 |
| 39 ChromeV8Extension::~ChromeV8Extension() { | 60 ChromeV8Extension::~ChromeV8Extension() { |
| 61 g_instances.Get().erase(this); |
| 40 } | 62 } |
| 41 | 63 |
| 42 ChromeV8Context* ChromeV8Extension::GetContext() { | 64 // static |
| 43 CHECK(dispatcher_); | 65 const ChromeV8Extension::InstanceSet& ChromeV8Extension::GetAll() { |
| 44 return dispatcher_->v8_context_set().GetByV8Context(v8_context()); | 66 return g_instances.Get(); |
| 45 } | 67 } |
| 46 | 68 |
| 47 content::RenderView* ChromeV8Extension::GetRenderView() { | 69 const Extension* ChromeV8Extension::GetExtensionForCurrentRenderView() const { |
| 48 ChromeV8Context* context = GetContext(); | 70 content::RenderView* renderview = GetCurrentRenderView(); |
| 49 return context ? context->GetRenderView() : NULL; | 71 if (!renderview) |
| 50 } | 72 return NULL; // this can happen as a tab is closing. |
| 51 | 73 |
| 52 const Extension* ChromeV8Extension::GetExtensionForRenderView() { | 74 WebDocument document = renderview->GetWebView()->mainFrame()->document(); |
| 53 ChromeV8Context* context = GetContext(); | 75 GURL url = document.url(); |
| 54 return context ? context->extension() : NULL; | 76 const ExtensionSet* extensions = dispatcher_->extensions(); |
| 77 if (!extensions->ExtensionBindingsAllowed( |
| 78 ExtensionURLInfo(document.securityOrigin(), url))) |
| 79 return NULL; |
| 80 |
| 81 return extensions->GetExtensionOrAppByURL( |
| 82 ExtensionURLInfo(document.securityOrigin(), url)); |
| 55 } | 83 } |
| 56 | 84 |
| 57 } // namespace extensions | 85 } // namespace extensions |
| OLD | NEW |