OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/chrome_webstore_bindings.h" | 5 #include "chrome/renderer/extensions/chrome_webstore_bindings.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "chrome/common/extensions/extension.h" | 9 #include "chrome/common/extensions/extension.h" |
| 10 #include "chrome/renderer/extensions/chrome_v8_extension.h" |
10 #include "chrome/renderer/extensions/extension_helper.h" | 11 #include "chrome/renderer/extensions/extension_helper.h" |
11 #include "chrome/renderer/weak_v8_function_map.h" | 12 #include "chrome/renderer/weak_v8_function_map.h" |
12 #include "content/public/renderer/render_view.h" | 13 #include "content/public/renderer/render_view.h" |
13 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
| 15 #include "grit/renderer_resources.h" |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeList.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeList.h" |
19 #include "v8/include/v8.h" | 21 #include "v8/include/v8.h" |
20 | 22 |
21 using WebKit::WebDocument; | 23 using WebKit::WebDocument; |
22 using WebKit::WebElement; | 24 using WebKit::WebElement; |
23 using WebKit::WebFrame; | 25 using WebKit::WebFrame; |
24 using WebKit::WebNode; | 26 using WebKit::WebNode; |
25 using WebKit::WebNodeList; | 27 using WebKit::WebNodeList; |
26 | 28 |
| 29 class ExtensionDispatcher; |
| 30 |
27 namespace { | 31 namespace { |
28 | 32 |
29 const char kWebstoreV8ExtensionName[] = "v8/ChromeWebstore"; | 33 const char kWebstoreV8ExtensionName[] = "v8/ChromeWebstore"; |
30 | 34 |
31 const char kWebstoreLinkRelation[] = "chrome-webstore-item"; | 35 const char kWebstoreLinkRelation[] = "chrome-webstore-item"; |
32 | 36 |
33 const char kPreferredStoreLinkUrlNotAString[] = | 37 const char kPreferredStoreLinkUrlNotAString[] = |
34 "The Chrome Web Store item link URL parameter must be a string."; | 38 "The Chrome Web Store item link URL parameter must be a string."; |
35 const char kSuccessCallbackNotAFunctionError[] = | 39 const char kSuccessCallbackNotAFunctionError[] = |
36 "The success callback parameter must be a function."; | 40 "The success callback parameter must be a function."; |
(...skipping 13 matching lines...) Expand all Loading... |
50 // (successful or not) via HandleInstallResponse. | 54 // (successful or not) via HandleInstallResponse. |
51 int g_next_install_id = 0; | 55 int g_next_install_id = 0; |
52 | 56 |
53 base::LazyInstance<WeakV8FunctionMap> g_success_callbacks = | 57 base::LazyInstance<WeakV8FunctionMap> g_success_callbacks = |
54 LAZY_INSTANCE_INITIALIZER; | 58 LAZY_INSTANCE_INITIALIZER; |
55 base::LazyInstance<WeakV8FunctionMap> g_failure_callbacks = | 59 base::LazyInstance<WeakV8FunctionMap> g_failure_callbacks = |
56 LAZY_INSTANCE_INITIALIZER; | 60 LAZY_INSTANCE_INITIALIZER; |
57 | 61 |
58 } // anonymous namespace | 62 } // anonymous namespace |
59 | 63 |
60 class ExtensionImpl : public v8::Extension { | 64 class ExtensionImpl : public ChromeV8Extension { |
61 public: | 65 public: |
62 ExtensionImpl() : | 66 explicit ExtensionImpl(ExtensionDispatcher* dispatcher) : |
63 v8::Extension( | 67 ChromeV8Extension(kWebstoreV8ExtensionName, IDR_WEBSTORE_BINDINGS_JS, |
64 kWebstoreV8ExtensionName, | 68 dispatcher) {} |
65 "var chrome = chrome || {};" | |
66 "if (!chrome.webstore) {" | |
67 " chrome.webstore = new function() {" | |
68 " native function Install(preferredStoreUrl, onSuccess, onFailure);" | |
69 " this.install = Install;" | |
70 " };" | |
71 "}") { | |
72 } | |
73 | 69 |
74 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | 70 virtual void SetNativeFunctions(v8::Handle<v8::Object> object) OVERRIDE { |
75 v8::Handle<v8::String> name) { | 71 RouteFunctionToStatic("WebStoreInstall", Install, object); |
76 if (name->Equals(v8::String::New("Install"))) { | |
77 return v8::FunctionTemplate::New(Install); | |
78 } else { | |
79 return v8::Handle<v8::FunctionTemplate>(); | |
80 } | |
81 } | 72 } |
82 | 73 |
83 static v8::Handle<v8::Value> Install(const v8::Arguments& args) { | 74 static v8::Handle<v8::Value> Install(const v8::Arguments& args) { |
84 WebFrame* frame = WebFrame::frameForCurrentContext(); | 75 WebFrame* frame = WebFrame::frameForCurrentContext(); |
85 if (!frame || !frame->view()) | 76 if (!frame || !frame->view()) |
86 return v8::Undefined(); | 77 return v8::Undefined(); |
87 | 78 |
88 content::RenderView* render_view = | 79 content::RenderView* render_view = |
89 content::RenderView::FromWebView(frame->view()); | 80 content::RenderView::FromWebView(frame->view()); |
90 if (!render_view) | 81 if (!render_view) |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 *webstore_item_id = candidate_webstore_item_id; | 210 *webstore_item_id = candidate_webstore_item_id; |
220 return true; | 211 return true; |
221 } | 212 } |
222 | 213 |
223 *error = kNoWebstoreItemLinkFoundError; | 214 *error = kNoWebstoreItemLinkFoundError; |
224 return false; | 215 return false; |
225 } | 216 } |
226 }; | 217 }; |
227 | 218 |
228 // static | 219 // static |
229 v8::Extension* ChromeWebstoreExtension::Get() { | 220 ChromeV8Extension* ChromeWebstoreExtension::Get( |
230 return new ExtensionImpl(); | 221 ExtensionDispatcher* dispatcher) { |
| 222 return new ExtensionImpl(dispatcher); |
231 } | 223 } |
232 | 224 |
233 // static | 225 // static |
234 void ChromeWebstoreExtension::HandleInstallResponse(int install_id, | 226 void ChromeWebstoreExtension::HandleInstallResponse(int install_id, |
235 bool success, | 227 bool success, |
236 const std::string& error) { | 228 const std::string& error) { |
237 WeakV8FunctionMap& callback_map = | 229 WeakV8FunctionMap& callback_map = |
238 success ? g_success_callbacks.Get() : g_failure_callbacks.Get(); | 230 success ? g_success_callbacks.Get() : g_failure_callbacks.Get(); |
239 | 231 |
240 v8::Persistent<v8::Function> function = callback_map.Remove(install_id); | 232 v8::Persistent<v8::Function> function = callback_map.Remove(install_id); |
241 if (function.IsEmpty()) | 233 if (function.IsEmpty()) |
242 return; | 234 return; |
243 | 235 |
244 v8::HandleScope handle_scope; | 236 v8::HandleScope handle_scope; |
245 v8::Context::Scope context_scope(function->CreationContext()); | 237 v8::Context::Scope context_scope(function->CreationContext()); |
246 v8::Handle<v8::Value> argv[1]; | 238 v8::Handle<v8::Value> argv[1]; |
247 argv[0] = v8::String::New(error.c_str()); | 239 argv[0] = v8::String::New(error.c_str()); |
248 function->Call(v8::Object::New(), arraysize(argv), argv); | 240 function->Call(v8::Object::New(), arraysize(argv), argv); |
249 } | 241 } |
OLD | NEW |