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
3 // found in the LICENSE file.
5 #include "chrome/renderer/external_extension.h"
7 #include "base/command_line.h"
8 #include "chrome/common/chrome_switches.h"
9 #include "chrome/common/render_messages.h"
10 #include "chrome/common/search_provider.h"
11 #include "content/public/renderer/render_view.h"
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
15 #include "v8/include/v8.h"
17 using WebKit::WebFrame
;
18 using WebKit::WebView
;
19 using content::RenderView
;
21 namespace extensions_v8
{
23 static const char* const kSearchProviderApi
=
27 "external.AddSearchProvider = function(name) {"
28 " native function NativeAddSearchProvider();"
29 " NativeAddSearchProvider(name);"
31 "external.IsSearchProviderInstalled = function(name) {"
32 " native function NativeIsSearchProviderInstalled();"
33 " return NativeIsSearchProviderInstalled(name);"
36 const char* const kExternalExtensionName
= "v8/External";
38 class ExternalExtensionWrapper
: public v8::Extension
{
40 ExternalExtensionWrapper();
42 // Allows v8's javascript code to call the native functions defined
43 // in this class for window.external.
44 virtual v8::Handle
<v8::FunctionTemplate
> GetNativeFunction(
45 v8::Handle
<v8::String
> name
) OVERRIDE
;
47 // Helper function to find the RenderView. May return NULL.
48 static RenderView
* GetRenderView();
50 // Implementation of window.external.AddSearchProvider.
51 static v8::Handle
<v8::Value
> AddSearchProvider(const v8::Arguments
& args
);
53 // Implementation of window.external.IsSearchProviderInstalled.
54 static v8::Handle
<v8::Value
> IsSearchProviderInstalled(
55 const v8::Arguments
& args
);
58 DISALLOW_COPY_AND_ASSIGN(ExternalExtensionWrapper
);
61 ExternalExtensionWrapper::ExternalExtensionWrapper()
63 kExternalExtensionName
,
67 v8::Handle
<v8::FunctionTemplate
> ExternalExtensionWrapper::GetNativeFunction(
68 v8::Handle
<v8::String
> name
) {
69 if (name
->Equals(v8::String::New("NativeAddSearchProvider")))
70 return v8::FunctionTemplate::New(AddSearchProvider
);
72 if (name
->Equals(v8::String::New("NativeIsSearchProviderInstalled")))
73 return v8::FunctionTemplate::New(IsSearchProviderInstalled
);
75 return v8::Handle
<v8::FunctionTemplate
>();
79 RenderView
* ExternalExtensionWrapper::GetRenderView() {
80 WebFrame
* webframe
= WebFrame::frameForCurrentContext();
81 DCHECK(webframe
) << "There should be an active frame since we just got "
82 "a native function called.";
83 if (!webframe
) return NULL
;
85 WebView
* webview
= webframe
->view();
86 if (!webview
) return NULL
; // can happen during closing
88 return RenderView::FromWebView(webview
);
92 v8::Handle
<v8::Value
> ExternalExtensionWrapper::AddSearchProvider(
93 const v8::Arguments
& args
) {
94 if (!args
.Length()) return v8::Undefined();
96 std::string name
= std::string(*v8::String::Utf8Value(args
[0]));
97 if (!name
.length()) return v8::Undefined();
99 RenderView
* render_view
= GetRenderView();
100 if (!render_view
) return v8::Undefined();
103 if (!osd_url
.is_empty() && osd_url
.is_valid()) {
104 render_view
->Send(new ChromeViewHostMsg_PageHasOSDD(
105 render_view
->GetRoutingID(), render_view
->GetPageId(), osd_url
,
106 search_provider::EXPLICIT_PROVIDER
));
109 return v8::Undefined();
113 v8::Handle
<v8::Value
> ExternalExtensionWrapper::IsSearchProviderInstalled(
114 const v8::Arguments
& args
) {
115 if (!args
.Length()) return v8::Undefined();
116 v8::String::Utf8Value
utf8name(args
[0]);
117 if (!utf8name
.length()) return v8::Undefined();
119 std::string name
= std::string(*utf8name
);
120 RenderView
* render_view
= GetRenderView();
121 if (!render_view
) return v8::Undefined();
123 WebFrame
* webframe
= WebFrame::frameForCurrentContext();
124 if (!webframe
) return v8::Undefined();
126 search_provider::InstallState install
= search_provider::DENIED
;
127 GURL inquiry_url
= GURL(name
);
128 if (!inquiry_url
.is_empty()) {
129 render_view
->Send(new ChromeViewHostMsg_GetSearchProviderInstallState(
130 render_view
->GetRoutingID(),
131 webframe
->document().url(),
136 if (install
== search_provider::DENIED
) {
137 // FIXME: throw access denied exception.
138 return v8::ThrowException(v8::Exception::Error(v8::String::Empty()));
140 return v8::Integer::New(install
);
143 v8::Extension
* ExternalExtension::Get() {
144 return new ExternalExtensionWrapper();
147 } // namespace extensions_v8