| 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" | |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeList.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeList.h" |
| 19 #include "v8/include/v8.h" | 18 #include "v8/include/v8.h" |
| 20 | 19 |
| 21 using WebKit::WebDocument; | 20 using WebKit::WebDocument; |
| 22 using WebKit::WebElement; | 21 using WebKit::WebElement; |
| 23 using WebKit::WebFrame; | 22 using WebKit::WebFrame; |
| 24 using WebKit::WebNode; | 23 using WebKit::WebNode; |
| 25 using WebKit::WebNodeList; | 24 using WebKit::WebNodeList; |
| 26 | 25 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 41 const char kNoWebstoreItemLinkFoundError[] = | 40 const char kNoWebstoreItemLinkFoundError[] = |
| 42 "No Chrome Web Store item link found."; | 41 "No Chrome Web Store item link found."; |
| 43 const char kInvalidWebstoreItemUrlError[] = | 42 const char kInvalidWebstoreItemUrlError[] = |
| 44 "Invalid Chrome Web Store item URL."; | 43 "Invalid Chrome Web Store item URL."; |
| 45 | 44 |
| 46 // chrome.webstore.install() calls generate an install ID so that the install's | 45 // 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 | 46 // callbacks may be fired when the browser notifies us of install completion |
| 48 // (successful or not) via OnInlineWebstoreInstallResponse. | 47 // (successful or not) via OnInlineWebstoreInstallResponse. |
| 49 int g_next_install_id = 0; | 48 int g_next_install_id = 0; |
| 50 | 49 |
| 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 | |
| 83 } // anonymous namespace | 50 } // anonymous namespace |
| 84 | 51 |
| 85 WebstoreBindings::WebstoreBindings(ExtensionDispatcher* dispatcher) | 52 WebstoreBindings::WebstoreBindings(ExtensionDispatcher* dispatcher, |
| 86 : ChromeV8Extension("extensions/webstore.js", IDR_WEBSTORE_BINDINGS_JS, | 53 ChromeV8Context* context) |
| 87 dispatcher) { | 54 : ChromeV8Extension(dispatcher), |
| 55 ChromeV8ExtensionHandler(context) { |
| 56 RouteFunction("Install", |
| 57 base::Bind(&WebstoreBindings::Install, base::Unretained(this))); |
| 88 } | 58 } |
| 89 | 59 |
| 90 ChromeV8ExtensionHandler* WebstoreBindings::CreateHandler( | 60 v8::Handle<v8::Value> WebstoreBindings::Install( |
| 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( | |
| 114 const v8::Arguments& args) { | 61 const v8::Arguments& args) { |
| 115 WebFrame* frame = WebFrame::frameForCurrentContext(); | 62 WebFrame* frame = WebFrame::frameForCurrentContext(); |
| 116 if (!frame || !frame->view()) | 63 if (!frame || !frame->view()) |
| 117 return v8::Undefined(); | 64 return v8::Undefined(); |
| 118 | 65 |
| 119 content::RenderView* render_view = | 66 content::RenderView* render_view = |
| 120 content::RenderView::FromWebView(frame->view()); | 67 content::RenderView::FromWebView(frame->view()); |
| 121 if (!render_view) | 68 if (!render_view) |
| 122 return v8::Undefined(); | 69 return v8::Undefined(); |
| 123 | 70 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 154 render_view->GetRoutingID(), | 101 render_view->GetRoutingID(), |
| 155 install_id, | 102 install_id, |
| 156 GetRoutingID(), | 103 GetRoutingID(), |
| 157 webstore_item_id, | 104 webstore_item_id, |
| 158 frame->document().url())); | 105 frame->document().url())); |
| 159 | 106 |
| 160 return v8::Integer::New(install_id); | 107 return v8::Integer::New(install_id); |
| 161 } | 108 } |
| 162 | 109 |
| 163 // static | 110 // static |
| 164 bool WebstoreBindingsHandler::GetWebstoreItemIdFromFrame( | 111 bool WebstoreBindings::GetWebstoreItemIdFromFrame( |
| 165 WebFrame* frame, const std::string& preferred_store_link_url, | 112 WebFrame* frame, const std::string& preferred_store_link_url, |
| 166 std::string* webstore_item_id, std::string* error) { | 113 std::string* webstore_item_id, std::string* error) { |
| 167 if (frame != frame->top()) { | 114 if (frame != frame->top()) { |
| 168 *error = kNotInTopFrameError; | 115 *error = kNotInTopFrameError; |
| 169 return false; | 116 return false; |
| 170 } | 117 } |
| 171 | 118 |
| 172 if (!frame->isProcessingUserGesture()) { | 119 if (!frame->isProcessingUserGesture()) { |
| 173 *error = kNotUserGestureError; | 120 *error = kNotUserGestureError; |
| 174 return false; | 121 return false; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 } | 187 } |
| 241 | 188 |
| 242 *webstore_item_id = candidate_webstore_item_id; | 189 *webstore_item_id = candidate_webstore_item_id; |
| 243 return true; | 190 return true; |
| 244 } | 191 } |
| 245 | 192 |
| 246 *error = kNoWebstoreItemLinkFoundError; | 193 *error = kNoWebstoreItemLinkFoundError; |
| 247 return false; | 194 return false; |
| 248 } | 195 } |
| 249 | 196 |
| 250 bool WebstoreBindingsHandler::OnMessageReceived(const IPC::Message& message) { | 197 bool WebstoreBindings::OnMessageReceived(const IPC::Message& message) { |
| 251 IPC_BEGIN_MESSAGE_MAP(WebstoreBindingsHandler, message) | 198 IPC_BEGIN_MESSAGE_MAP(WebstoreBindings, message) |
| 252 IPC_MESSAGE_HANDLER(ExtensionMsg_InlineWebstoreInstallResponse, | 199 IPC_MESSAGE_HANDLER(ExtensionMsg_InlineWebstoreInstallResponse, |
| 253 OnInlineWebstoreInstallResponse) | 200 OnInlineWebstoreInstallResponse) |
| 254 IPC_MESSAGE_UNHANDLED(CHECK(false) << "Unhandled IPC message") | 201 IPC_MESSAGE_UNHANDLED(CHECK(false) << "Unhandled IPC message") |
| 255 IPC_END_MESSAGE_MAP() | 202 IPC_END_MESSAGE_MAP() |
| 256 return true; | 203 return true; |
| 257 } | 204 } |
| 258 | 205 |
| 259 void WebstoreBindingsHandler::OnInlineWebstoreInstallResponse( | 206 void WebstoreBindings::OnInlineWebstoreInstallResponse( |
| 260 int install_id, | 207 int install_id, |
| 261 bool success, | 208 bool success, |
| 262 const std::string& error) { | 209 const std::string& error) { |
| 263 v8::HandleScope handle_scope; | 210 v8::HandleScope handle_scope; |
| 264 v8::Context::Scope context_scope(context_->v8_context()); | 211 v8::Context::Scope context_scope(context_->v8_context()); |
| 265 v8::Handle<v8::Value> argv[3]; | 212 v8::Handle<v8::Value> argv[3]; |
| 266 argv[0] = v8::Integer::New(install_id); | 213 argv[0] = v8::Integer::New(install_id); |
| 267 argv[1] = v8::Boolean::New(success); | 214 argv[1] = v8::Boolean::New(success); |
| 268 argv[2] = v8::String::New(error.c_str()); | 215 argv[2] = v8::String::New(error.c_str()); |
| 269 CHECK(context_->CallChromeHiddenMethod("webstore.onInstallResponse", | 216 CHECK(context_->CallChromeHiddenMethod("webstore.onInstallResponse", |
| 270 arraysize(argv), argv, NULL)); | 217 arraysize(argv), argv, NULL)); |
| 271 } | 218 } |
| OLD | NEW |