| 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 "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "chrome/common/extensions/extension.h" | 8 #include "chrome/common/extensions/extension.h" |
| 9 #include "chrome/common/extensions/extension_messages.h" | 9 #include "chrome/common/extensions/extension_messages.h" |
| 10 #include "chrome/renderer/extensions/chrome_v8_context.h" | 10 #include "chrome/renderer/extensions/chrome_v8_context.h" |
| 11 #include "content/public/renderer/render_view.h" | 11 #include "content/public/renderer/render_view.h" |
| 12 #include "googleurl/src/gurl.h" | 12 #include "googleurl/src/gurl.h" |
| 13 #include "grit/renderer_resources.h" | 13 #include "grit/renderer_resources.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeList.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeList.h" |
| 18 #include "v8/include/v8.h" | 19 #include "v8/include/v8.h" |
| 19 | 20 |
| 20 using WebKit::WebDocument; | 21 using WebKit::WebDocument; |
| 21 using WebKit::WebElement; | 22 using WebKit::WebElement; |
| 22 using WebKit::WebFrame; | 23 using WebKit::WebFrame; |
| 23 using WebKit::WebNode; | 24 using WebKit::WebNode; |
| 24 using WebKit::WebNodeList; | 25 using WebKit::WebNodeList; |
| 25 | 26 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 40 const char kNoWebstoreItemLinkFoundError[] = | 41 const char kNoWebstoreItemLinkFoundError[] = |
| 41 "No Chrome Web Store item link found."; | 42 "No Chrome Web Store item link found."; |
| 42 const char kInvalidWebstoreItemUrlError[] = | 43 const char kInvalidWebstoreItemUrlError[] = |
| 43 "Invalid Chrome Web Store item URL."; | 44 "Invalid Chrome Web Store item URL."; |
| 44 | 45 |
| 45 // 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 |
| 46 // 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 |
| 47 // (successful or not) via OnInlineWebstoreInstallResponse. | 48 // (successful or not) via OnInlineWebstoreInstallResponse. |
| 48 int g_next_install_id = 0; | 49 int g_next_install_id = 0; |
| 49 | 50 |
| 51 class WebstoreBindingsHandler : public ChromeV8ExtensionHandler { |
| 52 public: |
| 53 WebstoreBindingsHandler( |
| 54 ExtensionDispatcher* dispatcher, ChromeV8Context* context); |
| 55 |
| 56 // ChromeV8ExtensionHandler |
| 57 virtual v8::Handle<v8::Value> HandleNativeFunction( |
| 58 const std::string& name, |
| 59 const v8::Arguments& arguments) OVERRIDE; |
| 60 |
| 61 // IPC::Channel::Listener |
| 62 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 63 |
| 64 private: |
| 65 v8::Handle<v8::Value> Install(const v8::Arguments& args); |
| 66 |
| 67 void OnInlineWebstoreInstallResponse( |
| 68 int install_id, bool success, const std::string& error); |
| 69 |
| 70 // Extracts a Web Store item ID from a <link rel="chrome-webstore-item" |
| 71 // href="https://chrome.google.com/webstore/detail/id"> node found in the |
| 72 // frame. On success, true will be returned and the |webstore_item_id| |
| 73 // parameter will be populated with the ID. On failure, false will be returned |
| 74 // and |error| will be populated with the error. |
| 75 static bool GetWebstoreItemIdFromFrame( |
| 76 WebFrame* frame, const std::string& preferred_store_link_url, |
| 77 std::string* webstore_item_id, std::string* error); |
| 78 |
| 79 ExtensionDispatcher* dispatcher_; |
| 80 DISALLOW_COPY_AND_ASSIGN(WebstoreBindingsHandler); |
| 81 }; |
| 82 |
| 50 } // anonymous namespace | 83 } // anonymous namespace |
| 51 | 84 |
| 52 WebstoreBindings::WebstoreBindings(ExtensionDispatcher* dispatcher, | 85 WebstoreBindings::WebstoreBindings(ExtensionDispatcher* dispatcher) |
| 53 ChromeV8Context* context) | 86 : ChromeV8Extension("extensions/webstore.js", IDR_WEBSTORE_BINDINGS_JS, |
| 54 : ChromeV8Extension(dispatcher), | 87 dispatcher) { |
| 55 ChromeV8ExtensionHandler(context) { | |
| 56 RouteFunction("Install", | |
| 57 base::Bind(&WebstoreBindings::Install, base::Unretained(this))); | |
| 58 } | 88 } |
| 59 | 89 |
| 60 v8::Handle<v8::Value> WebstoreBindings::Install( | 90 ChromeV8ExtensionHandler* WebstoreBindings::CreateHandler( |
| 91 ChromeV8Context* context) { |
| 92 return new WebstoreBindingsHandler(extension_dispatcher(), context); |
| 93 } |
| 94 |
| 95 WebstoreBindingsHandler::WebstoreBindingsHandler( |
| 96 ExtensionDispatcher* dispatcher, |
| 97 ChromeV8Context* context) |
| 98 : ChromeV8ExtensionHandler(context), |
| 99 dispatcher_(dispatcher) { |
| 100 } |
| 101 |
| 102 v8::Handle<v8::Value> WebstoreBindingsHandler::HandleNativeFunction( |
| 103 const std::string& name, const v8::Arguments& args) { |
| 104 if (name == "Install") { |
| 105 return Install(args); |
| 106 } else { |
| 107 CHECK(false) << "Unknown native function: " << name; |
| 108 } |
| 109 |
| 110 return v8::Undefined(); |
| 111 } |
| 112 |
| 113 v8::Handle<v8::Value> WebstoreBindingsHandler::Install( |
| 61 const v8::Arguments& args) { | 114 const v8::Arguments& args) { |
| 62 WebFrame* frame = WebFrame::frameForCurrentContext(); | 115 WebFrame* frame = WebFrame::frameForCurrentContext(); |
| 63 if (!frame || !frame->view()) | 116 if (!frame || !frame->view()) |
| 64 return v8::Undefined(); | 117 return v8::Undefined(); |
| 65 | 118 |
| 66 content::RenderView* render_view = | 119 content::RenderView* render_view = |
| 67 content::RenderView::FromWebView(frame->view()); | 120 content::RenderView::FromWebView(frame->view()); |
| 68 if (!render_view) | 121 if (!render_view) |
| 69 return v8::Undefined(); | 122 return v8::Undefined(); |
| 70 | 123 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 101 render_view->GetRoutingID(), | 154 render_view->GetRoutingID(), |
| 102 install_id, | 155 install_id, |
| 103 GetRoutingID(), | 156 GetRoutingID(), |
| 104 webstore_item_id, | 157 webstore_item_id, |
| 105 frame->document().url())); | 158 frame->document().url())); |
| 106 | 159 |
| 107 return v8::Integer::New(install_id); | 160 return v8::Integer::New(install_id); |
| 108 } | 161 } |
| 109 | 162 |
| 110 // static | 163 // static |
| 111 bool WebstoreBindings::GetWebstoreItemIdFromFrame( | 164 bool WebstoreBindingsHandler::GetWebstoreItemIdFromFrame( |
| 112 WebFrame* frame, const std::string& preferred_store_link_url, | 165 WebFrame* frame, const std::string& preferred_store_link_url, |
| 113 std::string* webstore_item_id, std::string* error) { | 166 std::string* webstore_item_id, std::string* error) { |
| 114 if (frame != frame->top()) { | 167 if (frame != frame->top()) { |
| 115 *error = kNotInTopFrameError; | 168 *error = kNotInTopFrameError; |
| 116 return false; | 169 return false; |
| 117 } | 170 } |
| 118 | 171 |
| 119 if (!frame->isProcessingUserGesture()) { | 172 if (!frame->isProcessingUserGesture()) { |
| 120 *error = kNotUserGestureError; | 173 *error = kNotUserGestureError; |
| 121 return false; | 174 return false; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 } | 240 } |
| 188 | 241 |
| 189 *webstore_item_id = candidate_webstore_item_id; | 242 *webstore_item_id = candidate_webstore_item_id; |
| 190 return true; | 243 return true; |
| 191 } | 244 } |
| 192 | 245 |
| 193 *error = kNoWebstoreItemLinkFoundError; | 246 *error = kNoWebstoreItemLinkFoundError; |
| 194 return false; | 247 return false; |
| 195 } | 248 } |
| 196 | 249 |
| 197 bool WebstoreBindings::OnMessageReceived(const IPC::Message& message) { | 250 bool WebstoreBindingsHandler::OnMessageReceived(const IPC::Message& message) { |
| 198 IPC_BEGIN_MESSAGE_MAP(WebstoreBindings, message) | 251 IPC_BEGIN_MESSAGE_MAP(WebstoreBindingsHandler, message) |
| 199 IPC_MESSAGE_HANDLER(ExtensionMsg_InlineWebstoreInstallResponse, | 252 IPC_MESSAGE_HANDLER(ExtensionMsg_InlineWebstoreInstallResponse, |
| 200 OnInlineWebstoreInstallResponse) | 253 OnInlineWebstoreInstallResponse) |
| 201 IPC_MESSAGE_UNHANDLED(CHECK(false) << "Unhandled IPC message") | 254 IPC_MESSAGE_UNHANDLED(CHECK(false) << "Unhandled IPC message") |
| 202 IPC_END_MESSAGE_MAP() | 255 IPC_END_MESSAGE_MAP() |
| 203 return true; | 256 return true; |
| 204 } | 257 } |
| 205 | 258 |
| 206 void WebstoreBindings::OnInlineWebstoreInstallResponse( | 259 void WebstoreBindingsHandler::OnInlineWebstoreInstallResponse( |
| 207 int install_id, | 260 int install_id, |
| 208 bool success, | 261 bool success, |
| 209 const std::string& error) { | 262 const std::string& error) { |
| 210 v8::HandleScope handle_scope; | 263 v8::HandleScope handle_scope; |
| 211 v8::Context::Scope context_scope(context_->v8_context()); | 264 v8::Context::Scope context_scope(context_->v8_context()); |
| 212 v8::Handle<v8::Value> argv[3]; | 265 v8::Handle<v8::Value> argv[3]; |
| 213 argv[0] = v8::Integer::New(install_id); | 266 argv[0] = v8::Integer::New(install_id); |
| 214 argv[1] = v8::Boolean::New(success); | 267 argv[1] = v8::Boolean::New(success); |
| 215 argv[2] = v8::String::New(error.c_str()); | 268 argv[2] = v8::String::New(error.c_str()); |
| 216 CHECK(context_->CallChromeHiddenMethod("webstore.onInstallResponse", | 269 CHECK(context_->CallChromeHiddenMethod("webstore.onInstallResponse", |
| 217 arraysize(argv), argv, NULL)); | 270 arraysize(argv), argv, NULL)); |
| 218 } | 271 } |
| OLD | NEW |