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/extension_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 extensions::ExtensionAPI; | |
23 using WebKit::WebDocument; | 22 using WebKit::WebDocument; |
24 using WebKit::WebFrame; | 23 using WebKit::WebFrame; |
25 using WebKit::WebView; | 24 using WebKit::WebView; |
26 | 25 |
| 26 namespace extensions { |
| 27 |
27 namespace { | 28 namespace { |
28 | 29 |
29 static base::LazyInstance<ChromeV8Extension::InstanceSet> g_instances = | 30 static base::LazyInstance<ChromeV8Extension::InstanceSet> g_instances = |
30 LAZY_INSTANCE_INITIALIZER; | 31 LAZY_INSTANCE_INITIALIZER; |
31 | 32 |
32 } // namespace | 33 } // namespace |
33 | 34 |
34 | |
35 // static | 35 // static |
36 content::RenderView* ChromeV8Extension::GetCurrentRenderView() { | 36 content::RenderView* ChromeV8Extension::GetCurrentRenderView() { |
37 WebFrame* webframe = WebFrame::frameForCurrentContext(); | 37 WebFrame* webframe = WebFrame::frameForCurrentContext(); |
38 DCHECK(webframe) << "RetrieveCurrentFrame called when not in a V8 context."; | 38 DCHECK(webframe) << "RetrieveCurrentFrame called when not in a V8 context."; |
39 if (!webframe) | 39 if (!webframe) |
40 return NULL; | 40 return NULL; |
41 | 41 |
42 WebView* webview = webframe->view(); | 42 WebView* webview = webframe->view(); |
43 if (!webview) | 43 if (!webview) |
44 return NULL; // can happen during closing | 44 return NULL; // can happen during closing |
45 | 45 |
46 content::RenderView* renderview = content::RenderView::FromWebView(webview); | 46 content::RenderView* renderview = content::RenderView::FromWebView(webview); |
47 DCHECK(renderview) << "Encountered a WebView without a WebViewDelegate"; | 47 DCHECK(renderview) << "Encountered a WebView without a WebViewDelegate"; |
48 return renderview; | 48 return renderview; |
49 } | 49 } |
50 | 50 |
51 ChromeV8Extension::ChromeV8Extension(ExtensionDispatcher* extension_dispatcher) | 51 ChromeV8Extension::ChromeV8Extension(Dispatcher* dispatcher) |
52 : extension_dispatcher_(extension_dispatcher) { | 52 : dispatcher_(dispatcher) { |
53 g_instances.Get().insert(this); | 53 g_instances.Get().insert(this); |
54 } | 54 } |
55 | 55 |
56 ChromeV8Extension::~ChromeV8Extension() { | 56 ChromeV8Extension::~ChromeV8Extension() { |
57 g_instances.Get().erase(this); | 57 g_instances.Get().erase(this); |
58 } | 58 } |
59 | 59 |
60 // static | 60 // static |
61 const ChromeV8Extension::InstanceSet& ChromeV8Extension::GetAll() { | 61 const ChromeV8Extension::InstanceSet& ChromeV8Extension::GetAll() { |
62 return g_instances.Get(); | 62 return g_instances.Get(); |
63 } | 63 } |
64 | 64 |
65 const extensions::Extension* | 65 const Extension* ChromeV8Extension::GetExtensionForCurrentRenderView() const { |
66 ChromeV8Extension::GetExtensionForCurrentRenderView() const { | |
67 content::RenderView* renderview = GetCurrentRenderView(); | 66 content::RenderView* renderview = GetCurrentRenderView(); |
68 if (!renderview) | 67 if (!renderview) |
69 return NULL; // this can happen as a tab is closing. | 68 return NULL; // this can happen as a tab is closing. |
70 | 69 |
71 WebDocument document = renderview->GetWebView()->mainFrame()->document(); | 70 WebDocument document = renderview->GetWebView()->mainFrame()->document(); |
72 GURL url = document.url(); | 71 GURL url = document.url(); |
73 const ExtensionSet* extensions = extension_dispatcher_->extensions(); | 72 const ExtensionSet* extensions = dispatcher_->extensions(); |
74 if (!extensions->ExtensionBindingsAllowed( | 73 if (!extensions->ExtensionBindingsAllowed( |
75 ExtensionURLInfo(document.securityOrigin(), url))) | 74 ExtensionURLInfo(document.securityOrigin(), url))) |
76 return NULL; | 75 return NULL; |
77 | 76 |
78 return extensions->GetExtensionOrAppByURL( | 77 return extensions->GetExtensionOrAppByURL( |
79 ExtensionURLInfo(document.securityOrigin(), url)); | 78 ExtensionURLInfo(document.securityOrigin(), url)); |
80 } | 79 } |
| 80 |
| 81 } // namespace extensions |
OLD | NEW |