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

Side by Side Diff: chrome/renderer/extensions/webstore_bindings.cc

Issue 2791533002: Convert Web Store Inline Install IPCs to mojo (Closed)
Patch Set: Addressed Devlin's comments Created 3 years, 7 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
« no previous file with comments | « chrome/renderer/extensions/webstore_bindings.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/extensions/webstore_bindings.h" 5 #include "chrome/renderer/extensions/webstore_bindings.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 "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
11 #include "chrome/common/extensions/api/webstore/webstore_api_constants.h" 11 #include "chrome/common/extensions/api/webstore/webstore_api_constants.h"
12 #include "chrome/common/extensions/chrome_extension_messages.h"
13 #include "components/crx_file/id_util.h" 12 #include "components/crx_file/id_util.h"
13 #include "content/public/common/associated_interface_provider.h"
14 #include "content/public/renderer/render_frame.h" 14 #include "content/public/renderer/render_frame.h"
15 #include "extensions/common/extension.h" 15 #include "extensions/common/extension.h"
16 #include "extensions/common/extension_urls.h" 16 #include "extensions/common/extension_urls.h"
17 #include "extensions/renderer/script_context.h" 17 #include "extensions/renderer/script_context.h"
18 #include "third_party/WebKit/public/web/WebDocument.h" 18 #include "third_party/WebKit/public/web/WebDocument.h"
19 #include "third_party/WebKit/public/web/WebElement.h" 19 #include "third_party/WebKit/public/web/WebElement.h"
20 #include "third_party/WebKit/public/web/WebLocalFrame.h" 20 #include "third_party/WebKit/public/web/WebLocalFrame.h"
21 #include "third_party/WebKit/public/web/WebNode.h" 21 #include "third_party/WebKit/public/web/WebNode.h"
22 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" 22 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
23 #include "url/gurl.h" 23 #include "url/gurl.h"
(...skipping 14 matching lines...) Expand all
38 "Chrome Web Store installations can only be started by the top frame."; 38 "Chrome Web Store installations can only be started by the top frame.";
39 const char kNotUserGestureError[] = 39 const char kNotUserGestureError[] =
40 "Chrome Web Store installations can only be initated by a user gesture."; 40 "Chrome Web Store installations can only be initated by a user gesture.";
41 const char kNoWebstoreItemLinkFoundError[] = 41 const char kNoWebstoreItemLinkFoundError[] =
42 "No Chrome Web Store item link found."; 42 "No Chrome Web Store item link found.";
43 const char kInvalidWebstoreItemUrlError[] = 43 const char kInvalidWebstoreItemUrlError[] =
44 "Invalid Chrome Web Store item URL."; 44 "Invalid Chrome Web Store item URL.";
45 45
46 // chrome.webstore.install() calls generate an install ID so that the install's 46 // chrome.webstore.install() calls generate an install ID so that the install's
47 // callbacks may be fired when the browser notifies us of install completion 47 // callbacks may be fired when the browser notifies us of install completion
48 // (successful or not) via OnInlineWebstoreInstallResponse. 48 // (successful or not) via |InlineInstallResponse|.
49 int g_next_install_id = 0; 49 int g_next_install_id = 0;
50 50
51 } // anonymous namespace 51 } // anonymous namespace
52 52
53 WebstoreBindings::WebstoreBindings(ScriptContext* context) 53 WebstoreBindings::WebstoreBindings(ScriptContext* context)
54 : ObjectBackedNativeHandler(context) { 54 : ObjectBackedNativeHandler(context) {
55 RouteFunction("Install", "webstore", 55 RouteFunction("Install", "webstore",
56 base::Bind(&WebstoreBindings::Install, base::Unretained(this))); 56 base::Bind(&WebstoreBindings::Install, base::Unretained(this)));
57 content::RenderFrame* render_frame = context->GetRenderFrame();
58 if (render_frame)
59 render_frame->GetRemoteAssociatedInterfaces()->GetInterface(
60 &inline_installer_);
61 }
62
63 WebstoreBindings::~WebstoreBindings() {}
64
65 void WebstoreBindings::InlineInstallResponse(int install_id,
66 bool success,
67 const std::string& error,
68 webstore_install::Result result) {
69 v8::Isolate* isolate = context()->isolate();
70 v8::HandleScope handle_scope(isolate);
71 v8::Context::Scope context_scope(context()->v8_context());
72 v8::Local<v8::Value> argv[] = {
73 v8::Integer::New(isolate, install_id), v8::Boolean::New(isolate, success),
74 v8::String::NewFromUtf8(isolate, error.c_str()),
75 v8::String::NewFromUtf8(
76 isolate,
77 api::webstore::kInstallResultCodes[static_cast<int>(result)])};
78 context()->module_system()->CallModuleMethodSafe(
79 "webstore", "onInstallResponse", arraysize(argv), argv);
80 }
81
82 void WebstoreBindings::InlineInstallStageChanged(
83 api::webstore::InstallStage stage) {
84 const char* stage_string = nullptr;
85 switch (stage) {
86 case api::webstore::INSTALL_STAGE_DOWNLOADING:
87 stage_string = api::webstore::kInstallStageDownloading;
88 break;
89 case api::webstore::INSTALL_STAGE_INSTALLING:
90 stage_string = api::webstore::kInstallStageInstalling;
91 break;
92 }
93 v8::Isolate* isolate = context()->isolate();
94 v8::HandleScope handle_scope(isolate);
95 v8::Context::Scope context_scope(context()->v8_context());
96 v8::Local<v8::Value> argv[] = {
97 v8::String::NewFromUtf8(isolate, stage_string)};
98 context()->module_system()->CallModuleMethodSafe(
99 "webstore", "onInstallStageChanged", arraysize(argv), argv);
100 }
101
102 void WebstoreBindings::InlineInstallDownloadProgress(int percent_downloaded) {
103 v8::Isolate* isolate = context()->isolate();
104 v8::HandleScope handle_scope(isolate);
105 v8::Context::Scope context_scope(context()->v8_context());
106 v8::Local<v8::Value> argv[] = {
107 v8::Number::New(isolate, percent_downloaded / 100.0)};
108 context()->module_system()->CallModuleMethodSafe(
109 "webstore", "onDownloadProgress", arraysize(argv), argv);
57 } 110 }
58 111
59 void WebstoreBindings::Install( 112 void WebstoreBindings::Install(
60 const v8::FunctionCallbackInfo<v8::Value>& args) { 113 const v8::FunctionCallbackInfo<v8::Value>& args) {
61 content::RenderFrame* render_frame = context()->GetRenderFrame(); 114 content::RenderFrame* render_frame = context()->GetRenderFrame();
62 if (!render_frame) 115 if (!render_frame)
63 return; 116 return;
64 117
65 // The first two arguments indicate whether or not there are install stage 118 // The first two arguments indicate whether or not there are install stage
66 // or download progress listeners. 119 // or download progress listeners.
(...skipping 17 matching lines...) Expand all
84 137
85 if (!GetWebstoreItemIdFromFrame( 138 if (!GetWebstoreItemIdFromFrame(
86 frame, preferred_store_link_url, &webstore_item_id, &error)) { 139 frame, preferred_store_link_url, &webstore_item_id, &error)) {
87 args.GetIsolate()->ThrowException( 140 args.GetIsolate()->ThrowException(
88 v8::String::NewFromUtf8(args.GetIsolate(), error.c_str())); 141 v8::String::NewFromUtf8(args.GetIsolate(), error.c_str()));
89 return; 142 return;
90 } 143 }
91 144
92 int install_id = g_next_install_id++; 145 int install_id = g_next_install_id++;
93 146
94 Send(new ExtensionHostMsg_InlineWebstoreInstall( 147 mojom::InlineInstallProgressListenerPtr install_progress_listener =
95 render_frame->GetRoutingID(), install_id, GetRoutingID(), 148 install_progress_listener_bindings_.CreateInterfacePtrAndBind(this);
96 webstore_item_id, listener_mask));
97 149
150 inline_installer_->DoInlineInstall(
151 webstore_item_id, listener_mask, std::move(install_progress_listener),
152 base::Bind(&WebstoreBindings::InlineInstallResponse,
153 base::Unretained(this), install_id));
98 args.GetReturnValue().Set(static_cast<int32_t>(install_id)); 154 args.GetReturnValue().Set(static_cast<int32_t>(install_id));
99 } 155 }
100 156
101 // static 157 // static
102 bool WebstoreBindings::GetWebstoreItemIdFromFrame( 158 bool WebstoreBindings::GetWebstoreItemIdFromFrame(
103 blink::WebLocalFrame* frame, 159 blink::WebLocalFrame* frame,
104 const std::string& preferred_store_link_url, 160 const std::string& preferred_store_link_url,
105 std::string* webstore_item_id, 161 std::string* webstore_item_id,
106 std::string* error) { 162 std::string* error) {
107 if (frame != frame->Top()) { 163 if (frame != frame->Top()) {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 } 235 }
180 236
181 *webstore_item_id = candidate_webstore_item_id; 237 *webstore_item_id = candidate_webstore_item_id;
182 return true; 238 return true;
183 } 239 }
184 240
185 *error = kNoWebstoreItemLinkFoundError; 241 *error = kNoWebstoreItemLinkFoundError;
186 return false; 242 return false;
187 } 243 }
188 244
189 bool WebstoreBindings::OnMessageReceived(const IPC::Message& message) {
190 IPC_BEGIN_MESSAGE_MAP(WebstoreBindings, message)
191 IPC_MESSAGE_HANDLER(ExtensionMsg_InlineWebstoreInstallResponse,
192 OnInlineWebstoreInstallResponse)
193 IPC_MESSAGE_HANDLER(ExtensionMsg_InlineInstallStageChanged,
194 OnInlineInstallStageChanged)
195 IPC_MESSAGE_HANDLER(ExtensionMsg_InlineInstallDownloadProgress,
196 OnInlineInstallDownloadProgress)
197 IPC_MESSAGE_UNHANDLED(CHECK(false) << "Unhandled IPC message")
198 IPC_END_MESSAGE_MAP()
199 return true;
200 }
201
202 void WebstoreBindings::OnInlineWebstoreInstallResponse(
203 int install_id,
204 bool success,
205 const std::string& error,
206 webstore_install::Result result) {
207 v8::Isolate* isolate = context()->isolate();
208 v8::HandleScope handle_scope(isolate);
209 v8::Context::Scope context_scope(context()->v8_context());
210 v8::Local<v8::Value> argv[] = {
211 v8::Integer::New(isolate, install_id),
212 v8::Boolean::New(isolate, success),
213 v8::String::NewFromUtf8(isolate, error.c_str()),
214 v8::String::NewFromUtf8(
215 isolate, api::webstore::kInstallResultCodes[static_cast<int>(result)])
216 };
217 context()->module_system()->CallModuleMethodSafe(
218 "webstore", "onInstallResponse", arraysize(argv), argv);
219 }
220
221 void WebstoreBindings::OnInlineInstallStageChanged(int stage) {
222 const char* stage_string = NULL;
223 api::webstore::InstallStage install_stage =
224 static_cast<api::webstore::InstallStage>(stage);
225 switch (install_stage) {
226 case api::webstore::INSTALL_STAGE_DOWNLOADING:
227 stage_string = api::webstore::kInstallStageDownloading;
228 break;
229 case api::webstore::INSTALL_STAGE_INSTALLING:
230 stage_string = api::webstore::kInstallStageInstalling;
231 break;
232 }
233 v8::Isolate* isolate = context()->isolate();
234 v8::HandleScope handle_scope(isolate);
235 v8::Context::Scope context_scope(context()->v8_context());
236 v8::Local<v8::Value> argv[] = {
237 v8::String::NewFromUtf8(isolate, stage_string)};
238 context()->module_system()->CallModuleMethodSafe(
239 "webstore", "onInstallStageChanged", arraysize(argv), argv);
240 }
241
242 void WebstoreBindings::OnInlineInstallDownloadProgress(int percent_downloaded) {
243 v8::Isolate* isolate = context()->isolate();
244 v8::HandleScope handle_scope(isolate);
245 v8::Context::Scope context_scope(context()->v8_context());
246 v8::Local<v8::Value> argv[] = {
247 v8::Number::New(isolate, percent_downloaded / 100.0)};
248 context()->module_system()->CallModuleMethodSafe(
249 "webstore", "onDownloadProgress", arraysize(argv), argv);
250 }
251
252 } // namespace extensions 245 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/renderer/extensions/webstore_bindings.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698