Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(552)

Side by Side Diff: chrome/renderer/external_extension.cc

Issue 2072613003: Convert GetSearchProviderInstallState to Mojo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/search_provider.mojom.h"
11 #include "chrome/common/search_provider.h" 11 #include "content/public/renderer/render_thread.h"
12 #include "content/public/renderer/render_view.h" 12 #include "services/shell/public/cpp/interface_provider.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"
16 #include "v8/include/v8.h" 15 #include "v8/include/v8.h"
17 16
18 using blink::WebLocalFrame; 17 using blink::WebLocalFrame;
19 using blink::WebView; 18 using blink::WebView;
20 using content::RenderView;
21 19
22 namespace extensions_v8 { 20 namespace extensions_v8 {
23 21
24 namespace { 22 namespace {
25 23
26 const char* const kSearchProviderApi = 24 const char* const kSearchProviderApi =
27 "var external;" 25 "var external;"
28 "if (!external)" 26 "if (!external)"
29 " external = {};" 27 " external = {};"
30 "external.IsSearchProviderInstalled = function(name) {" 28 "external.IsSearchProviderInstalled = function(name) {"
31 " native function NativeIsSearchProviderInstalled();" 29 " native function NativeIsSearchProviderInstalled();"
32 " return NativeIsSearchProviderInstalled(name);" 30 " return NativeIsSearchProviderInstalled(name);"
33 "};"; 31 "};";
34 32
35 const char kExternalExtensionName[] = "v8/External"; 33 const char kExternalExtensionName[] = "v8/External";
36 34
37 } // namespace 35 } // namespace
38 36
39 class ExternalExtensionWrapper : public v8::Extension { 37 class ExternalExtensionWrapper : public v8::Extension {
40 public: 38 public:
41 ExternalExtensionWrapper(); 39 ExternalExtensionWrapper();
42 40
43 // Allows v8's javascript code to call the native functions defined 41 // Allows v8's javascript code to call the native functions defined
44 // in this class for window.external. 42 // in this class for window.external.
45 v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate( 43 v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate(
46 v8::Isolate* isolate, 44 v8::Isolate* isolate,
47 v8::Local<v8::String> name) override; 45 v8::Local<v8::String> name) override;
48 46
49 // Helper function to find the RenderView. May return NULL.
50 static RenderView* GetRenderView();
51
52 // Implementation of window.external.IsSearchProviderInstalled. 47 // Implementation of window.external.IsSearchProviderInstalled.
53 static void IsSearchProviderInstalled( 48 static void IsSearchProviderInstalled(
54 const v8::FunctionCallbackInfo<v8::Value>& args); 49 const v8::FunctionCallbackInfo<v8::Value>& args);
55 50
56 private: 51 private:
57 DISALLOW_COPY_AND_ASSIGN(ExternalExtensionWrapper); 52 DISALLOW_COPY_AND_ASSIGN(ExternalExtensionWrapper);
58 }; 53 };
59 54
60 ExternalExtensionWrapper::ExternalExtensionWrapper() 55 ExternalExtensionWrapper::ExternalExtensionWrapper()
61 : v8::Extension(kExternalExtensionName, kSearchProviderApi) { 56 : v8::Extension(kExternalExtensionName, kSearchProviderApi) {
62 } 57 }
63 58
64 v8::Local<v8::FunctionTemplate> 59 v8::Local<v8::FunctionTemplate>
65 ExternalExtensionWrapper::GetNativeFunctionTemplate( 60 ExternalExtensionWrapper::GetNativeFunctionTemplate(
66 v8::Isolate* isolate, 61 v8::Isolate* isolate,
67 v8::Local<v8::String> name) { 62 v8::Local<v8::String> name) {
68 if (name->Equals(v8::String::NewFromUtf8( 63 if (name->Equals(v8::String::NewFromUtf8(
69 isolate, "NativeIsSearchProviderInstalled"))) { 64 isolate, "NativeIsSearchProviderInstalled"))) {
70 return v8::FunctionTemplate::New(isolate, IsSearchProviderInstalled); 65 return v8::FunctionTemplate::New(isolate, IsSearchProviderInstalled);
71 } 66 }
72 67
73 return v8::Local<v8::FunctionTemplate>(); 68 return v8::Local<v8::FunctionTemplate>();
74 } 69 }
75 70
76 // static 71 // static
77 RenderView* ExternalExtensionWrapper::GetRenderView() {
78 WebLocalFrame* webframe = WebLocalFrame::frameForCurrentContext();
79 DCHECK(webframe) << "There should be an active frame since we just got "
80 "a native function called.";
81 if (!webframe)
82 return NULL;
83
84 WebView* webview = webframe->view();
85 if (!webview)
86 return NULL; // can happen during closing
87
88 return RenderView::FromWebView(webview);
89 }
90
91 // static
92 void ExternalExtensionWrapper::IsSearchProviderInstalled( 72 void ExternalExtensionWrapper::IsSearchProviderInstalled(
93 const v8::FunctionCallbackInfo<v8::Value>& args) { 73 const v8::FunctionCallbackInfo<v8::Value>& args) {
94 if (!args.Length() || !args[0]->IsString()) 74 if (!args.Length() || !args[0]->IsString())
95 return; 75 return;
96 76
97 v8::String::Utf8Value utf8name(args[0]); 77 v8::String::Utf8Value utf8name(args[0]);
98 if (!utf8name.length()) 78 if (!utf8name.length())
99 return; 79 return;
100 80
101 std::string name(*utf8name); 81 std::string name(*utf8name);
102 RenderView* render_view = GetRenderView();
103 if (!render_view)
104 return;
105 82
106 WebLocalFrame* webframe = WebLocalFrame::frameForCurrentContext(); 83 WebLocalFrame* webframe = WebLocalFrame::frameForCurrentContext();
107 if (!webframe) 84 if (!webframe)
108 return; 85 return;
109 86
110 search_provider::InstallState install = search_provider::DENIED; 87 chrome::mojom::InstallState install = chrome::mojom::InstallState::DENIED;
111 GURL inquiry_url = GURL(webframe->document().url()).Resolve(name); 88 GURL inquiry_url = GURL(webframe->document().url()).Resolve(name);
112 if (!inquiry_url.is_empty()) { 89 if (!inquiry_url.is_empty()) {
113 webframe->didCallIsSearchProviderInstalled(); 90 webframe->didCallIsSearchProviderInstalled();
114 render_view->Send(new ChromeViewHostMsg_GetSearchProviderInstallState( 91 chrome::mojom::SearchProviderInstallStatePtr search_provider_service;
115 render_view->GetRoutingID(), webframe->document().url(), inquiry_url, 92 content::RenderThread::Get()->GetRemoteInterfaces()->GetInterface(
116 &install)); 93 mojo::GetProxy(&search_provider_service));
94 if (!search_provider_service->GetInstallState(webframe->document().url(),
95 inquiry_url, &install)) {
96 DLOG(ERROR) << "Can't fetch search provider install state";
97 }
117 } 98 }
118 99
119 if (install == search_provider::DENIED) { 100 if (install == chrome::mojom::InstallState::DENIED) {
120 // FIXME: throw access denied exception. 101 // FIXME: throw access denied exception.
121 v8::Isolate* isolate = args.GetIsolate(); 102 v8::Isolate* isolate = args.GetIsolate();
122 isolate->ThrowException(v8::Exception::Error(v8::String::Empty(isolate))); 103 isolate->ThrowException(v8::Exception::Error(v8::String::Empty(isolate)));
123 return; 104 return;
124 } 105 }
125 args.GetReturnValue().Set(static_cast<int32_t>(install)); 106 args.GetReturnValue().Set(static_cast<int32_t>(install));
126 } 107 }
127 108
128 v8::Extension* ExternalExtension::Get() { 109 v8::Extension* ExternalExtension::Get() {
129 return new ExternalExtensionWrapper(); 110 return new ExternalExtensionWrapper();
130 } 111 }
131 112
132 } // namespace extensions_v8 113 } // namespace extensions_v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698