OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/external_extension.h" | 5 #include "chrome/renderer/external_extension.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "chrome/common/chrome_switches.h" |
6 #include "chrome/renderer/render_view.h" | 9 #include "chrome/renderer/render_view.h" |
7 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" | 10 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" |
8 | 11 |
9 using WebKit::WebFrame; | 12 using WebKit::WebFrame; |
10 using WebKit::WebView; | 13 using WebKit::WebView; |
11 | 14 |
12 namespace extensions_v8 { | 15 namespace extensions_v8 { |
13 | 16 |
| 17 #define SEARCH_PROVIDER_API_V1 \ |
| 18 "var external;" \ |
| 19 "if (!external)" \ |
| 20 " external = {};" \ |
| 21 "external.AddSearchProvider = function(name) {" \ |
| 22 " native function NativeAddSearchProvider();" \ |
| 23 " NativeAddSearchProvider(name);" \ |
| 24 "};" |
| 25 |
| 26 static const char* const kSearchProviderApiV1 = SEARCH_PROVIDER_API_V1; |
| 27 static const char* const kSearchProviderApiV2 = |
| 28 SEARCH_PROVIDER_API_V1 |
| 29 "external.IsSearchProviderInstalled = function(name) {" |
| 30 " native function NativeIsSearchProviderInstalled();" |
| 31 " return NativeIsSearchProviderInstalled(name);" |
| 32 "};"; |
| 33 |
| 34 #undef SEARCH_PROVIDER_API_V1 |
| 35 |
14 const char* const kExternalExtensionName = "v8/External"; | 36 const char* const kExternalExtensionName = "v8/External"; |
15 | 37 |
| 38 // Should the new api's "IsSearchProviderInstalled and InstallSearchProvider |
| 39 // with an extra parameter to indicate if the provider should be the default" |
| 40 // be available? |
| 41 static bool EnableSearchProviderV2() { |
| 42 return CommandLine::ForCurrentProcess()->HasSwitch( |
| 43 switches::kEnableSearchProviderApiV2); |
| 44 } |
| 45 |
16 class ExternalExtensionWrapper : public v8::Extension { | 46 class ExternalExtensionWrapper : public v8::Extension { |
17 public: | 47 public: |
18 ExternalExtensionWrapper() | 48 ExternalExtensionWrapper(); |
19 : v8::Extension( | |
20 kExternalExtensionName, | |
21 "var external;" | |
22 "if (!external)" | |
23 " external = {};" | |
24 "external.AddSearchProvider = function(name) {" | |
25 " native function NativeAddSearchProvider();" | |
26 " NativeAddSearchProvider(name);" | |
27 "}") { | |
28 } | |
29 | 49 |
| 50 // Allows v8's javascript code to call the native functions defined |
| 51 // in this class for window.external. |
30 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | 52 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
31 v8::Handle<v8::String> name) { | 53 v8::Handle<v8::String> name); |
32 if (name->Equals(v8::String::New("NativeAddSearchProvider"))) { | |
33 return v8::FunctionTemplate::New(AddSearchProvider); | |
34 } | |
35 return v8::Handle<v8::FunctionTemplate>(); | |
36 } | |
37 | 54 |
38 static v8::Handle<v8::Value> AddSearchProvider(const v8::Arguments& args) { | 55 // Helper function to find the RenderView. May return NULL. |
39 if (!args.Length()) | 56 static RenderView* GetRenderView(); |
40 return v8::Undefined(); | |
41 | 57 |
42 WebFrame* webframe = WebFrame::frameForEnteredContext(); | 58 // Implementation of window.external.AddSearchProvider. |
43 DCHECK(webframe) << "There should be an active frame since we just got " | 59 static v8::Handle<v8::Value> AddSearchProvider(const v8::Arguments& args); |
44 "a native function called."; | |
45 if (!webframe) return v8::Undefined(); | |
46 | 60 |
47 WebView* webview = webframe->view(); | 61 // Implementation of window.external.IsSearchProviderInstalled. |
48 if (!webview) return v8::Undefined(); // can happen during closing | 62 static v8::Handle<v8::Value> IsSearchProviderInstalled( |
| 63 const v8::Arguments& args); |
49 | 64 |
50 RenderView* renderview = RenderView::FromWebView(webview); | 65 private: |
51 if (!renderview) return v8::Undefined(); | 66 DISALLOW_COPY_AND_ASSIGN(ExternalExtensionWrapper); |
52 | |
53 std::string name = std::string(*v8::String::Utf8Value(args[0])); | |
54 if (!name.length()) return v8::Undefined(); | |
55 | |
56 renderview->AddSearchProvider(name); | |
57 return v8::Undefined(); | |
58 } | |
59 }; | 67 }; |
60 | 68 |
61 v8::Extension* ExternalExtension::Get() { | 69 ExternalExtensionWrapper::ExternalExtensionWrapper() |
| 70 : v8::Extension( |
| 71 kExternalExtensionName, |
| 72 !EnableSearchProviderV2() ? |
| 73 kSearchProviderApiV1 : kSearchProviderApiV2) { |
| 74 } |
| 75 |
| 76 v8::Handle<v8::FunctionTemplate> ExternalExtensionWrapper::GetNativeFunction( |
| 77 v8::Handle<v8::String> name) { |
| 78 if (name->Equals(v8::String::New("NativeAddSearchProvider"))) |
| 79 return v8::FunctionTemplate::New(AddSearchProvider); |
| 80 |
| 81 if (name->Equals(v8::String::New("NativeIsSearchProviderInstalled"))) |
| 82 return v8::FunctionTemplate::New(IsSearchProviderInstalled); |
| 83 |
| 84 return v8::Handle<v8::FunctionTemplate>(); |
| 85 } |
| 86 |
| 87 // static |
| 88 RenderView* ExternalExtensionWrapper::GetRenderView() { |
| 89 WebFrame* webframe = WebFrame::frameForEnteredContext(); |
| 90 DCHECK(webframe) << "There should be an active frame since we just got " |
| 91 "a native function called."; |
| 92 if (!webframe) return NULL; |
| 93 |
| 94 WebView* webview = webframe->view(); |
| 95 if (!webview) return NULL; // can happen during closing |
| 96 |
| 97 return RenderView::FromWebView(webview); |
| 98 } |
| 99 |
| 100 // static |
| 101 v8::Handle<v8::Value> ExternalExtensionWrapper::AddSearchProvider( |
| 102 const v8::Arguments& args) { |
| 103 if (!args.Length()) return v8::Undefined(); |
| 104 |
| 105 std::string name = std::string(*v8::String::Utf8Value(args[0])); |
| 106 if (!name.length()) return v8::Undefined(); |
| 107 |
| 108 RenderView* render_view = GetRenderView(); |
| 109 if (!render_view) return v8::Undefined(); |
| 110 |
| 111 render_view->AddSearchProvider(name); |
| 112 return v8::Undefined(); |
| 113 } |
| 114 |
| 115 // static |
| 116 v8::Handle<v8::Value> ExternalExtensionWrapper::IsSearchProviderInstalled( |
| 117 const v8::Arguments& args) { |
| 118 if (!args.Length()) return v8::Undefined(); |
| 119 |
| 120 std::string name = std::string(*v8::String::Utf8Value(args[0])); |
| 121 if (!name.length()) return v8::Undefined(); |
| 122 |
| 123 RenderView* render_view = GetRenderView(); |
| 124 if (!render_view) return v8::Undefined(); |
| 125 |
| 126 ViewHostMsg_GetSearchProviderInstallState_Params install |
| 127 = render_view->GetSearchProviderInstallState(name); |
| 128 if (install.state == |
| 129 ViewHostMsg_GetSearchProviderInstallState_Params::DENIED) { |
| 130 // FIXME: throw access denied exception. |
| 131 return v8::ThrowException(v8::Exception::Error(v8::String::Empty())); |
| 132 } |
| 133 return v8::Integer::New(install.state); |
| 134 } |
| 135 |
| 136 v8::Extension* ExternalExtension::Get() { |
62 return new ExternalExtensionWrapper(); | 137 return new ExternalExtensionWrapper(); |
63 } | 138 } |
64 | 139 |
65 } // namespace extensions_v8 | 140 } // namespace extensions_v8 |
OLD | NEW |