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 namespace { | 28 ChromeV8Extension::ChromeV8Extension(Dispatcher* dispatcher, |
29 | 29 v8::Handle<v8::Context> context) |
30 static base::LazyInstance<ChromeV8Extension::InstanceSet> g_instances = | 30 : ObjectBackedNativeHandler(context), |
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; | |
49 } | |
50 | |
51 ChromeV8Extension::ChromeV8Extension(Dispatcher* dispatcher) | |
52 // TODO(svenpanne) It would be nice to remove the GetCurrent() call and use | |
53 // an additional constructor parameter instead, but this would involve too | |
54 // many changes for now. | |
55 : NativeHandler(v8::Isolate::GetCurrent()), | |
56 dispatcher_(dispatcher) { | 31 dispatcher_(dispatcher) { |
57 g_instances.Get().insert(this); | 32 CHECK(dispatcher); |
58 } | 33 } |
59 | 34 |
60 ChromeV8Extension::~ChromeV8Extension() { | 35 ChromeV8Extension::~ChromeV8Extension() { |
61 g_instances.Get().erase(this); | |
62 } | 36 } |
63 | 37 |
64 // static | 38 ChromeV8Context* ChromeV8Extension::GetContext() { |
65 const ChromeV8Extension::InstanceSet& ChromeV8Extension::GetAll() { | 39 return dispatcher_->v8_context_set().GetByV8Context(v8_context()); |
66 return g_instances.Get(); | |
67 } | 40 } |
68 | 41 |
69 const Extension* ChromeV8Extension::GetExtensionForCurrentRenderView() const { | 42 content::RenderView* ChromeV8Extension::GetRenderView() { |
70 content::RenderView* renderview = GetCurrentRenderView(); | 43 ChromeV8Context* context = GetContext(); |
71 if (!renderview) | 44 return context ? context->GetRenderView() : NULL; |
72 return NULL; // this can happen as a tab is closing. | 45 } |
73 | 46 |
74 WebDocument document = renderview->GetWebView()->mainFrame()->document(); | 47 const Extension* ChromeV8Extension::GetExtensionForRenderView() { |
75 GURL url = document.url(); | 48 ChromeV8Context* context = GetContext(); |
76 const ExtensionSet* extensions = dispatcher_->extensions(); | 49 return context ? context->extension() : NULL; |
77 if (!extensions->ExtensionBindingsAllowed( | |
78 ExtensionURLInfo(document.securityOrigin(), url))) | |
79 return NULL; | |
80 | |
81 return extensions->GetExtensionOrAppByURL( | |
82 ExtensionURLInfo(document.securityOrigin(), url)); | |
83 } | 50 } |
84 | 51 |
85 } // namespace extensions | 52 } // namespace extensions |
OLD | NEW |