Chromium Code Reviews| 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/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 "mojo/public/cpp/bindings/strong_binding.h" | |
| 19 #include "services/service_manager/public/cpp/interface_provider.h" | |
| 20 #include "services/service_manager/public/cpp/interface_registry.h" | |
| 18 #include "third_party/WebKit/public/web/WebDocument.h" | 21 #include "third_party/WebKit/public/web/WebDocument.h" |
| 19 #include "third_party/WebKit/public/web/WebElement.h" | 22 #include "third_party/WebKit/public/web/WebElement.h" |
| 20 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 23 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 21 #include "third_party/WebKit/public/web/WebNode.h" | 24 #include "third_party/WebKit/public/web/WebNode.h" |
| 22 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" | 25 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" |
| 23 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 24 #include "v8/include/v8.h" | 27 #include "v8/include/v8.h" |
| 25 | 28 |
| 26 using blink::WebDocument; | 29 using blink::WebDocument; |
| 27 using blink::WebElement; | 30 using blink::WebElement; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 38 "Chrome Web Store installations can only be started by the top frame."; | 41 "Chrome Web Store installations can only be started by the top frame."; |
| 39 const char kNotUserGestureError[] = | 42 const char kNotUserGestureError[] = |
| 40 "Chrome Web Store installations can only be initated by a user gesture."; | 43 "Chrome Web Store installations can only be initated by a user gesture."; |
| 41 const char kNoWebstoreItemLinkFoundError[] = | 44 const char kNoWebstoreItemLinkFoundError[] = |
| 42 "No Chrome Web Store item link found."; | 45 "No Chrome Web Store item link found."; |
| 43 const char kInvalidWebstoreItemUrlError[] = | 46 const char kInvalidWebstoreItemUrlError[] = |
| 44 "Invalid Chrome Web Store item URL."; | 47 "Invalid Chrome Web Store item URL."; |
| 45 | 48 |
| 46 // chrome.webstore.install() calls generate an install ID so that the install's | 49 // 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 | 50 // callbacks may be fired when the browser notifies us of install completion |
| 48 // (successful or not) via OnInlineWebstoreInstallResponse. | 51 // (successful or not) via InlineInstallResponse. |
| 49 int g_next_install_id = 0; | 52 int g_next_install_id = 0; |
| 50 | 53 |
| 51 } // anonymous namespace | 54 } // anonymous namespace |
| 52 | 55 |
| 56 class WebstoreBindings::WebstoreBindingsHelper | |
| 57 : public ObjectBackedNativeHandler, | |
|
Devlin
2017/04/27 23:16:14
I don't think this should need to be an ObjectBack
catmullings
2017/04/28 03:07:30
Done.
catmullings
2017/04/28 03:07:30
Just for documentation: Making WebstoreBindingsHel
| |
| 58 public mojom::InlineInstallProgressListener { | |
| 59 public: | |
| 60 WebstoreBindingsHelper(ScriptContext* context); | |
| 61 ~WebstoreBindingsHelper() override; | |
| 62 mojom::InlineInstallProgressListenerPtr Create(); | |
| 63 | |
| 64 private: | |
| 65 void InlineInstallStageChanged(api::webstore::InstallStage stage) override; | |
| 66 void InlineInstallDownloadProgress(int percent_downloaded) override; | |
| 67 }; | |
| 68 | |
| 69 WebstoreBindings::WebstoreBindingsHelper::WebstoreBindingsHelper( | |
| 70 ScriptContext* context) | |
| 71 : ObjectBackedNativeHandler(context) {} | |
| 72 | |
| 73 mojom::InlineInstallProgressListenerPtr | |
| 74 WebstoreBindings::WebstoreBindingsHelper::Create() { | |
|
Devlin
2017/04/27 23:16:14
This create method is... odd. We call it on an in
catmullings
2017/04/28 03:07:30
Done.
| |
| 75 mojom::InlineInstallProgressListenerPtr install_progress_listener; | |
| 76 mojo::MakeStrongBinding(base::MakeUnique<WebstoreBindingsHelper>(context()), | |
| 77 mojo::MakeRequest(&install_progress_listener)); | |
| 78 return install_progress_listener; | |
| 79 } | |
| 80 | |
| 81 WebstoreBindings::WebstoreBindingsHelper::~WebstoreBindingsHelper() = default; | |
| 82 | |
| 83 void WebstoreBindings::WebstoreBindingsHelper::InlineInstallStageChanged( | |
|
Devlin
2017/04/27 23:16:14
nit: Can we instead leave these methods in Webstor
catmullings
2017/04/28 03:07:30
Done.
| |
| 84 api::webstore::InstallStage stage) { | |
| 85 const char* stage_string = NULL; | |
| 86 api::webstore::InstallStage install_stage = | |
| 87 static_cast<api::webstore::InstallStage>(stage); | |
| 88 switch (install_stage) { | |
| 89 case api::webstore::INSTALL_STAGE_DOWNLOADING: | |
| 90 stage_string = api::webstore::kInstallStageDownloading; | |
| 91 break; | |
| 92 case api::webstore::INSTALL_STAGE_INSTALLING: | |
| 93 stage_string = api::webstore::kInstallStageInstalling; | |
| 94 break; | |
| 95 } | |
| 96 v8::Isolate* isolate = context()->isolate(); | |
| 97 v8::HandleScope handle_scope(isolate); | |
| 98 v8::Context::Scope context_scope(context()->v8_context()); | |
| 99 v8::Local<v8::Value> argv[] = { | |
| 100 v8::String::NewFromUtf8(isolate, stage_string)}; | |
| 101 context()->module_system()->CallModuleMethodSafe( | |
| 102 "webstore", "onInstallStageChanged", arraysize(argv), argv); | |
| 103 } | |
| 104 | |
| 105 void WebstoreBindings::WebstoreBindingsHelper::InlineInstallDownloadProgress( | |
| 106 int percent_downloaded) { | |
| 107 v8::Isolate* isolate = context()->isolate(); | |
| 108 v8::HandleScope handle_scope(isolate); | |
| 109 v8::Context::Scope context_scope(context()->v8_context()); | |
| 110 v8::Local<v8::Value> argv[] = { | |
| 111 v8::Number::New(isolate, percent_downloaded / 100.0)}; | |
| 112 context()->module_system()->CallModuleMethodSafe( | |
| 113 "webstore", "onDownloadProgress", arraysize(argv), argv); | |
| 114 } | |
| 115 | |
| 53 WebstoreBindings::WebstoreBindings(ScriptContext* context) | 116 WebstoreBindings::WebstoreBindings(ScriptContext* context) |
| 54 : ObjectBackedNativeHandler(context) { | 117 : ObjectBackedNativeHandler(context), |
| 118 webstore_bindings_helper_( | |
| 119 base::MakeUnique<WebstoreBindingsHelper>(context)) { | |
| 55 RouteFunction("Install", "webstore", | 120 RouteFunction("Install", "webstore", |
| 56 base::Bind(&WebstoreBindings::Install, base::Unretained(this))); | 121 base::Bind(&WebstoreBindings::Install, base::Unretained(this))); |
| 122 context->GetRenderFrame()->GetRemoteAssociatedInterfaces()->GetInterface( | |
| 123 &inline_install_); | |
| 57 } | 124 } |
| 58 | 125 |
| 126 void WebstoreBindings::InlineInstallResponse(int install_id, | |
| 127 bool success, | |
| 128 const std::string& error, | |
| 129 webstore_install::Result result) { | |
| 130 v8::Isolate* isolate = context()->isolate(); | |
| 131 v8::HandleScope handle_scope(isolate); | |
| 132 v8::Context::Scope context_scope(context()->v8_context()); | |
| 133 v8::Local<v8::Value> argv[] = { | |
| 134 v8::Integer::New(isolate, install_id), v8::Boolean::New(isolate, success), | |
| 135 v8::String::NewFromUtf8(isolate, error.c_str()), | |
| 136 v8::String::NewFromUtf8( | |
| 137 isolate, | |
| 138 api::webstore::kInstallResultCodes[static_cast<int>(result)])}; | |
| 139 context()->module_system()->CallModuleMethodSafe( | |
| 140 "webstore", "onInstallResponse", arraysize(argv), argv); | |
| 141 } | |
| 142 | |
| 143 WebstoreBindings::~WebstoreBindings() {} | |
| 144 | |
| 59 void WebstoreBindings::Install( | 145 void WebstoreBindings::Install( |
| 60 const v8::FunctionCallbackInfo<v8::Value>& args) { | 146 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 61 content::RenderFrame* render_frame = context()->GetRenderFrame(); | 147 content::RenderFrame* render_frame = context()->GetRenderFrame(); |
| 62 if (!render_frame) | 148 if (!render_frame) |
| 63 return; | 149 return; |
| 64 | 150 |
| 65 // The first two arguments indicate whether or not there are install stage | 151 // The first two arguments indicate whether or not there are install stage |
| 66 // or download progress listeners. | 152 // or download progress listeners. |
| 67 int listener_mask = 0; | 153 int listener_mask = 0; |
| 68 CHECK(args[0]->IsBoolean()); | 154 CHECK(args[0]->IsBoolean()); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 84 | 170 |
| 85 if (!GetWebstoreItemIdFromFrame( | 171 if (!GetWebstoreItemIdFromFrame( |
| 86 frame, preferred_store_link_url, &webstore_item_id, &error)) { | 172 frame, preferred_store_link_url, &webstore_item_id, &error)) { |
| 87 args.GetIsolate()->ThrowException( | 173 args.GetIsolate()->ThrowException( |
| 88 v8::String::NewFromUtf8(args.GetIsolate(), error.c_str())); | 174 v8::String::NewFromUtf8(args.GetIsolate(), error.c_str())); |
| 89 return; | 175 return; |
| 90 } | 176 } |
| 91 | 177 |
| 92 int install_id = g_next_install_id++; | 178 int install_id = g_next_install_id++; |
| 93 | 179 |
| 94 Send(new ExtensionHostMsg_InlineWebstoreInstall( | 180 mojom::InlineInstallProgressListenerPtr install_progress_listener = |
| 95 render_frame->GetRoutingID(), install_id, GetRoutingID(), | 181 webstore_bindings_helper_->Create(); |
| 96 webstore_item_id, listener_mask)); | 182 |
| 183 inline_install_->DoInlineInstall( | |
| 184 install_id, webstore_item_id, listener_mask, | |
| 185 std::move(install_progress_listener), | |
|
Devlin
2017/04/27 23:16:14
Does this need to be a strong (i.e., owning) point
catmullings
2017/04/28 03:07:30
If std::move() isn't used, then there's a compiler
| |
| 186 base::Bind(&WebstoreBindings::InlineInstallResponse, | |
| 187 base::Unretained(this))); | |
| 97 | 188 |
| 98 args.GetReturnValue().Set(static_cast<int32_t>(install_id)); | 189 args.GetReturnValue().Set(static_cast<int32_t>(install_id)); |
| 99 } | 190 } |
| 100 | 191 |
| 101 // static | 192 // static |
| 102 bool WebstoreBindings::GetWebstoreItemIdFromFrame( | 193 bool WebstoreBindings::GetWebstoreItemIdFromFrame( |
| 103 blink::WebLocalFrame* frame, | 194 blink::WebLocalFrame* frame, |
| 104 const std::string& preferred_store_link_url, | 195 const std::string& preferred_store_link_url, |
| 105 std::string* webstore_item_id, | 196 std::string* webstore_item_id, |
| 106 std::string* error) { | 197 std::string* error) { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 180 | 271 |
| 181 *webstore_item_id = candidate_webstore_item_id; | 272 *webstore_item_id = candidate_webstore_item_id; |
| 182 return true; | 273 return true; |
| 183 } | 274 } |
| 184 | 275 |
| 185 *error = kNoWebstoreItemLinkFoundError; | 276 *error = kNoWebstoreItemLinkFoundError; |
| 186 return false; | 277 return false; |
| 187 } | 278 } |
| 188 | 279 |
| 189 bool WebstoreBindings::OnMessageReceived(const IPC::Message& message) { | 280 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; | 281 return true; |
| 200 } | 282 } |
| 201 | 283 |
| 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 | 284 } // namespace extensions |
| OLD | NEW |