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/external_extension.h" | 5 #include "chrome/renderer/external_extension.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "chrome/common/render_messages.h" | 10 #include "chrome/common/render_messages.h" |
11 #include "chrome/common/search_provider.h" | 11 #include "chrome/common/search_provider.h" |
12 #include "content/public/renderer/render_view.h" | 12 #include "content/public/renderer/render_view.h" |
13 #include "third_party/WebKit/public/web/WebDocument.h" | 13 #include "third_party/WebKit/public/web/WebDocument.h" |
14 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 14 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
15 #include "third_party/WebKit/public/web/WebView.h" | 15 #include "third_party/WebKit/public/web/WebView.h" |
16 #include "v8/include/v8.h" | 16 #include "v8/include/v8.h" |
17 | 17 |
18 using blink::WebLocalFrame; | 18 using blink::WebLocalFrame; |
19 using blink::WebView; | 19 using blink::WebView; |
20 using content::RenderView; | 20 using content::RenderView; |
21 | 21 |
22 namespace extensions_v8 { | 22 namespace extensions_v8 { |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 const char* const kSearchProviderApi = | 26 const char* const kSearchProviderApi = |
27 "var external;" | 27 "var external;" |
28 "if (!external)" | 28 "if (!external)" |
29 " external = {};" | 29 " external = {};" |
30 "external.AddSearchProvider = function(name) {" | |
31 " native function NativeAddSearchProvider();" | |
32 " NativeAddSearchProvider(name);" | |
33 "};" | |
34 "external.IsSearchProviderInstalled = function(name) {" | 30 "external.IsSearchProviderInstalled = function(name) {" |
35 " native function NativeIsSearchProviderInstalled();" | 31 " native function NativeIsSearchProviderInstalled();" |
36 " return NativeIsSearchProviderInstalled(name);" | 32 " return NativeIsSearchProviderInstalled(name);" |
37 "};"; | 33 "};"; |
38 | 34 |
39 const char kExternalExtensionName[] = "v8/External"; | 35 const char kExternalExtensionName[] = "v8/External"; |
40 | 36 |
41 } // namespace | 37 } // namespace |
42 | 38 |
43 class ExternalExtensionWrapper : public v8::Extension { | 39 class ExternalExtensionWrapper : public v8::Extension { |
44 public: | 40 public: |
45 ExternalExtensionWrapper(); | 41 ExternalExtensionWrapper(); |
46 | 42 |
47 // Allows v8's javascript code to call the native functions defined | 43 // Allows v8's javascript code to call the native functions defined |
48 // in this class for window.external. | 44 // in this class for window.external. |
49 v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate( | 45 v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate( |
50 v8::Isolate* isolate, | 46 v8::Isolate* isolate, |
51 v8::Local<v8::String> name) override; | 47 v8::Local<v8::String> name) override; |
52 | 48 |
53 // Helper function to find the RenderView. May return NULL. | 49 // Helper function to find the RenderView. May return NULL. |
54 static RenderView* GetRenderView(); | 50 static RenderView* GetRenderView(); |
55 | 51 |
56 // Implementation of window.external.AddSearchProvider. | |
57 static void AddSearchProvider( | |
58 const v8::FunctionCallbackInfo<v8::Value>& args); | |
59 | |
60 // Implementation of window.external.IsSearchProviderInstalled. | 52 // Implementation of window.external.IsSearchProviderInstalled. |
61 static void IsSearchProviderInstalled( | 53 static void IsSearchProviderInstalled( |
62 const v8::FunctionCallbackInfo<v8::Value>& args); | 54 const v8::FunctionCallbackInfo<v8::Value>& args); |
63 | 55 |
64 private: | 56 private: |
65 DISALLOW_COPY_AND_ASSIGN(ExternalExtensionWrapper); | 57 DISALLOW_COPY_AND_ASSIGN(ExternalExtensionWrapper); |
66 }; | 58 }; |
67 | 59 |
68 ExternalExtensionWrapper::ExternalExtensionWrapper() | 60 ExternalExtensionWrapper::ExternalExtensionWrapper() |
69 : v8::Extension(kExternalExtensionName, kSearchProviderApi) { | 61 : v8::Extension(kExternalExtensionName, kSearchProviderApi) { |
70 } | 62 } |
71 | 63 |
72 v8::Local<v8::FunctionTemplate> | 64 v8::Local<v8::FunctionTemplate> |
73 ExternalExtensionWrapper::GetNativeFunctionTemplate( | 65 ExternalExtensionWrapper::GetNativeFunctionTemplate( |
74 v8::Isolate* isolate, | 66 v8::Isolate* isolate, |
75 v8::Local<v8::String> name) { | 67 v8::Local<v8::String> name) { |
76 if (name->Equals(v8::String::NewFromUtf8(isolate, "NativeAddSearchProvider"))) | |
77 return v8::FunctionTemplate::New(isolate, AddSearchProvider); | |
78 | |
79 if (name->Equals(v8::String::NewFromUtf8( | 68 if (name->Equals(v8::String::NewFromUtf8( |
80 isolate, "NativeIsSearchProviderInstalled"))) { | 69 isolate, "NativeIsSearchProviderInstalled"))) { |
81 return v8::FunctionTemplate::New(isolate, IsSearchProviderInstalled); | 70 return v8::FunctionTemplate::New(isolate, IsSearchProviderInstalled); |
82 } | 71 } |
83 | 72 |
84 return v8::Local<v8::FunctionTemplate>(); | 73 return v8::Local<v8::FunctionTemplate>(); |
85 } | 74 } |
86 | 75 |
87 // static | 76 // static |
88 RenderView* ExternalExtensionWrapper::GetRenderView() { | 77 RenderView* ExternalExtensionWrapper::GetRenderView() { |
89 WebLocalFrame* webframe = WebLocalFrame::frameForCurrentContext(); | 78 WebLocalFrame* webframe = WebLocalFrame::frameForCurrentContext(); |
90 DCHECK(webframe) << "There should be an active frame since we just got " | 79 DCHECK(webframe) << "There should be an active frame since we just got " |
91 "a native function called."; | 80 "a native function called."; |
92 if (!webframe) | 81 if (!webframe) |
93 return NULL; | 82 return NULL; |
94 | 83 |
95 WebView* webview = webframe->view(); | 84 WebView* webview = webframe->view(); |
96 if (!webview) | 85 if (!webview) |
97 return NULL; // can happen during closing | 86 return NULL; // can happen during closing |
98 | 87 |
99 return RenderView::FromWebView(webview); | 88 return RenderView::FromWebView(webview); |
100 } | 89 } |
101 | 90 |
102 // static | 91 // static |
103 void ExternalExtensionWrapper::AddSearchProvider( | |
104 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
105 if (!args.Length() || !args[0]->IsString()) | |
106 return; | |
107 | |
108 std::string osdd_string(*v8::String::Utf8Value(args[0])); | |
109 if (osdd_string.empty()) | |
110 return; | |
111 | |
112 RenderView* render_view = GetRenderView(); | |
113 if (!render_view) | |
114 return; | |
115 | |
116 WebLocalFrame* webframe = WebLocalFrame::frameForCurrentContext(); | |
117 if (!webframe) | |
118 return; | |
119 | |
120 GURL osdd_url = GURL(webframe->document().url()).Resolve(osdd_string); | |
121 if (osdd_url.is_valid()) { | |
122 webframe->didCallAddSearchProvider(); | |
123 render_view->Send(new ChromeViewHostMsg_PageHasOSDD( | |
124 render_view->GetRoutingID(), webframe->document().url(), osdd_url, | |
125 search_provider::EXPLICIT_PROVIDER)); | |
126 } | |
127 } | |
128 | |
129 // static | |
130 void ExternalExtensionWrapper::IsSearchProviderInstalled( | 92 void ExternalExtensionWrapper::IsSearchProviderInstalled( |
131 const v8::FunctionCallbackInfo<v8::Value>& args) { | 93 const v8::FunctionCallbackInfo<v8::Value>& args) { |
132 if (!args.Length() || !args[0]->IsString()) | 94 if (!args.Length() || !args[0]->IsString()) |
133 return; | 95 return; |
134 | 96 |
135 v8::String::Utf8Value utf8name(args[0]); | 97 v8::String::Utf8Value utf8name(args[0]); |
136 if (!utf8name.length()) | 98 if (!utf8name.length()) |
137 return; | 99 return; |
138 | 100 |
139 std::string name(*utf8name); | 101 std::string name(*utf8name); |
(...skipping 21 matching lines...) Expand all Loading... |
161 return; | 123 return; |
162 } | 124 } |
163 args.GetReturnValue().Set(static_cast<int32_t>(install)); | 125 args.GetReturnValue().Set(static_cast<int32_t>(install)); |
164 } | 126 } |
165 | 127 |
166 v8::Extension* ExternalExtension::Get() { | 128 v8::Extension* ExternalExtension::Get() { |
167 return new ExternalExtensionWrapper(); | 129 return new ExternalExtensionWrapper(); |
168 } | 130 } |
169 | 131 |
170 } // namespace extensions_v8 | 132 } // namespace extensions_v8 |
OLD | NEW |