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/app_bindings.h" | 5 #include "chrome/renderer/extensions/app_bindings.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/string16.h" | 8 #include "base/string16.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
13 #include "chrome/common/extensions/extension_messages.h" | 13 #include "chrome/common/extensions/extension_messages.h" |
14 #include "chrome/common/extensions/extension_set.h" | 14 #include "chrome/common/extensions/extension_set.h" |
15 #include "chrome/common/extensions/manifest.h" | 15 #include "chrome/common/extensions/manifest.h" |
16 #include "chrome/renderer/extensions/chrome_v8_context.h" | 16 #include "chrome/renderer/extensions/chrome_v8_context.h" |
17 #include "chrome/renderer/extensions/extension_dispatcher.h" | 17 #include "chrome/renderer/extensions/dispatcher.h" |
18 #include "chrome/renderer/extensions/extension_helper.h" | 18 #include "chrome/renderer/extensions/extension_helper.h" |
19 #include "content/public/renderer/v8_value_converter.h" | 19 #include "content/public/renderer/v8_value_converter.h" |
20 #include "content/public/renderer/render_view.h" | 20 #include "content/public/renderer/render_view.h" |
21 #include "grit/renderer_resources.h" | 21 #include "grit/renderer_resources.h" |
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
24 #include "v8/include/v8.h" | 24 #include "v8/include/v8.h" |
25 | 25 |
26 using WebKit::WebFrame; | 26 using WebKit::WebFrame; |
27 using content::V8ValueConverter; | 27 using content::V8ValueConverter; |
28 using extensions::Extension; | 28 |
| 29 namespace extensions { |
29 | 30 |
30 namespace { | 31 namespace { |
31 | 32 |
32 bool IsCheckoutURL(const std::string& url_spec) { | 33 bool IsCheckoutURL(const std::string& url_spec) { |
33 std::string checkout_url_prefix = | 34 std::string checkout_url_prefix = |
34 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 35 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
35 switches::kAppsCheckoutURL); | 36 switches::kAppsCheckoutURL); |
36 if (checkout_url_prefix.empty()) | 37 if (checkout_url_prefix.empty()) |
37 checkout_url_prefix = "https://checkout.google.com/"; | 38 checkout_url_prefix = "https://checkout.google.com/"; |
38 | 39 |
(...skipping 10 matching lines...) Expand all Loading... |
49 | 50 |
50 return true; | 51 return true; |
51 } | 52 } |
52 | 53 |
53 const char* kMissingClientIdError = "Missing clientId parameter"; | 54 const char* kMissingClientIdError = "Missing clientId parameter"; |
54 const char* kInvalidClientIdError = "Invalid clientId"; | 55 const char* kInvalidClientIdError = "Invalid clientId"; |
55 const char* kInvalidCallbackIdError = "Invalid callbackId"; | 56 const char* kInvalidCallbackIdError = "Invalid callbackId"; |
56 | 57 |
57 } // namespace | 58 } // namespace |
58 | 59 |
59 AppBindings::AppBindings(ExtensionDispatcher* dispatcher, | 60 AppBindings::AppBindings(Dispatcher* dispatcher, |
60 ChromeV8Context* context) | 61 ChromeV8Context* context) |
61 : ChromeV8Extension(dispatcher), | 62 : ChromeV8Extension(dispatcher), |
62 ChromeV8ExtensionHandler(context) { | 63 ChromeV8ExtensionHandler(context) { |
63 RouteFunction("GetIsInstalled", | 64 RouteFunction("GetIsInstalled", |
64 base::Bind(&AppBindings::GetIsInstalled, base::Unretained(this))); | 65 base::Bind(&AppBindings::GetIsInstalled, base::Unretained(this))); |
65 RouteFunction("Install", | 66 RouteFunction("Install", |
66 base::Bind(&AppBindings::Install, base::Unretained(this))); | 67 base::Bind(&AppBindings::Install, base::Unretained(this))); |
67 RouteFunction("GetDetails", | 68 RouteFunction("GetDetails", |
68 base::Bind(&AppBindings::GetDetails, base::Unretained(this))); | 69 base::Bind(&AppBindings::GetDetails, base::Unretained(this))); |
69 RouteFunction("GetDetailsForFrame", | 70 RouteFunction("GetDetailsForFrame", |
70 base::Bind(&AppBindings::GetDetailsForFrame, base::Unretained(this))); | 71 base::Bind(&AppBindings::GetDetailsForFrame, base::Unretained(this))); |
71 RouteFunction("GetAppNotifyChannel", | 72 RouteFunction("GetAppNotifyChannel", |
72 base::Bind(&AppBindings::GetAppNotifyChannel, base::Unretained(this))); | 73 base::Bind(&AppBindings::GetAppNotifyChannel, base::Unretained(this))); |
73 RouteFunction("GetInstallState", | 74 RouteFunction("GetInstallState", |
74 base::Bind(&AppBindings::GetInstallState, base::Unretained(this))); | 75 base::Bind(&AppBindings::GetInstallState, base::Unretained(this))); |
75 RouteFunction("GetRunningState", | 76 RouteFunction("GetRunningState", |
76 base::Bind(&AppBindings::GetRunningState, base::Unretained(this))); | 77 base::Bind(&AppBindings::GetRunningState, base::Unretained(this))); |
77 } | 78 } |
78 | 79 |
79 v8::Handle<v8::Value> AppBindings::GetIsInstalled( | 80 v8::Handle<v8::Value> AppBindings::GetIsInstalled( |
80 const v8::Arguments& args) { | 81 const v8::Arguments& args) { |
81 const extensions::Extension* extension = context_->extension(); | 82 const Extension* extension = context_->extension(); |
82 | 83 |
83 // TODO(aa): Why only hosted app? | 84 // TODO(aa): Why only hosted app? |
84 bool result = extension && extension->is_hosted_app() && | 85 bool result = extension && extension->is_hosted_app() && |
85 extension_dispatcher_->IsExtensionActive(extension->id()); | 86 dispatcher_->IsExtensionActive(extension->id()); |
86 return v8::Boolean::New(result); | 87 return v8::Boolean::New(result); |
87 } | 88 } |
88 | 89 |
89 v8::Handle<v8::Value> AppBindings::Install(const v8::Arguments& args) { | 90 v8::Handle<v8::Value> AppBindings::Install(const v8::Arguments& args) { |
90 content::RenderView* render_view = context_->GetRenderView(); | 91 content::RenderView* render_view = context_->GetRenderView(); |
91 CHECK(render_view); | 92 CHECK(render_view); |
92 | 93 |
93 string16 error; | 94 string16 error; |
94 ExtensionHelper* helper = ExtensionHelper::Get(render_view); | 95 ExtensionHelper* helper = ExtensionHelper::Get(render_view); |
95 if (!helper->InstallWebApplicationUsingDefinitionFile( | 96 if (!helper->InstallWebApplicationUsingDefinitionFile( |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 if (!target_frame) { | 129 if (!target_frame) { |
129 return v8::ThrowException( | 130 return v8::ThrowException( |
130 v8::String::New("Could not find frame for specified object.")); | 131 v8::String::New("Could not find frame for specified object.")); |
131 } | 132 } |
132 | 133 |
133 return GetDetailsForFrameImpl(target_frame); | 134 return GetDetailsForFrameImpl(target_frame); |
134 } | 135 } |
135 | 136 |
136 v8::Handle<v8::Value> AppBindings::GetDetailsForFrameImpl( | 137 v8::Handle<v8::Value> AppBindings::GetDetailsForFrameImpl( |
137 WebFrame* frame) { | 138 WebFrame* frame) { |
138 const extensions::Extension* extension = | 139 const Extension* extension = |
139 extension_dispatcher_->extensions()->GetExtensionOrAppByURL( | 140 dispatcher_->extensions()->GetExtensionOrAppByURL( |
140 ExtensionURLInfo(frame->document().securityOrigin(), | 141 ExtensionURLInfo(frame->document().securityOrigin(), |
141 frame->document().url())); | 142 frame->document().url())); |
142 if (!extension) | 143 if (!extension) |
143 return v8::Null(); | 144 return v8::Null(); |
144 | 145 |
145 scoped_ptr<DictionaryValue> manifest_copy( | 146 scoped_ptr<DictionaryValue> manifest_copy( |
146 extension->manifest()->value()->DeepCopy()); | 147 extension->manifest()->value()->DeepCopy()); |
147 manifest_copy->SetString("id", extension->id()); | 148 manifest_copy->SetString("id", extension->id()); |
148 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | 149 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
149 return converter->ToV8Value(manifest_copy.get(), | 150 return converter->ToV8Value(manifest_copy.get(), |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 return v8::Undefined(); | 210 return v8::Undefined(); |
210 } | 211 } |
211 | 212 |
212 v8::Handle<v8::Value> AppBindings::GetRunningState(const v8::Arguments& args) { | 213 v8::Handle<v8::Value> AppBindings::GetRunningState(const v8::Arguments& args) { |
213 // To distinguish between ready_to_run and cannot_run states, we need the top | 214 // To distinguish between ready_to_run and cannot_run states, we need the top |
214 // level frame. | 215 // level frame. |
215 const WebFrame* parent_frame = context_->web_frame(); | 216 const WebFrame* parent_frame = context_->web_frame(); |
216 while (parent_frame->parent()) | 217 while (parent_frame->parent()) |
217 parent_frame = parent_frame->parent(); | 218 parent_frame = parent_frame->parent(); |
218 | 219 |
219 const ExtensionSet* extensions = extension_dispatcher_->extensions(); | 220 const ExtensionSet* extensions = dispatcher_->extensions(); |
220 | 221 |
221 // The app associated with the top level frame. | 222 // The app associated with the top level frame. |
222 const Extension* parent_app = extensions->GetHostedAppByURL( | 223 const Extension* parent_app = extensions->GetHostedAppByURL( |
223 ExtensionURLInfo(parent_frame->document().url())); | 224 ExtensionURLInfo(parent_frame->document().url())); |
224 | 225 |
225 // The app associated with this frame. | 226 // The app associated with this frame. |
226 const Extension* this_app = extensions->GetHostedAppByURL( | 227 const Extension* this_app = extensions->GetHostedAppByURL( |
227 ExtensionURLInfo(context_->web_frame()->document().url())); | 228 ExtensionURLInfo(context_->web_frame()->document().url())); |
228 | 229 |
229 if (!this_app || !parent_app) | 230 if (!this_app || !parent_app) |
230 return v8::String::New(extension_misc::kAppStateCannotRun); | 231 return v8::String::New(extension_misc::kAppStateCannotRun); |
231 | 232 |
232 const char* state = NULL; | 233 const char* state = NULL; |
233 if (extension_dispatcher_->IsExtensionActive(parent_app->id())) { | 234 if (dispatcher_->IsExtensionActive(parent_app->id())) { |
234 if (parent_app == this_app) | 235 if (parent_app == this_app) |
235 state = extension_misc::kAppStateRunning; | 236 state = extension_misc::kAppStateRunning; |
236 else | 237 else |
237 state = extension_misc::kAppStateCannotRun; | 238 state = extension_misc::kAppStateCannotRun; |
238 } else if (parent_app == this_app) { | 239 } else if (parent_app == this_app) { |
239 state = extension_misc::kAppStateReadyToRun; | 240 state = extension_misc::kAppStateReadyToRun; |
240 } else { | 241 } else { |
241 state = extension_misc::kAppStateCannotRun; | 242 state = extension_misc::kAppStateCannotRun; |
242 } | 243 } |
243 | 244 |
(...skipping 26 matching lines...) Expand all Loading... |
270 void AppBindings::OnAppInstallStateResponse( | 271 void AppBindings::OnAppInstallStateResponse( |
271 const std::string& state, int callback_id) { | 272 const std::string& state, int callback_id) { |
272 v8::HandleScope handle_scope; | 273 v8::HandleScope handle_scope; |
273 v8::Context::Scope context_scope(context_->v8_context()); | 274 v8::Context::Scope context_scope(context_->v8_context()); |
274 v8::Handle<v8::Value> argv[2]; | 275 v8::Handle<v8::Value> argv[2]; |
275 argv[0] = v8::String::New(state.c_str()); | 276 argv[0] = v8::String::New(state.c_str()); |
276 argv[1] = v8::Integer::New(callback_id); | 277 argv[1] = v8::Integer::New(callback_id); |
277 CHECK(context_->CallChromeHiddenMethod("app.onInstallStateResponse", | 278 CHECK(context_->CallChromeHiddenMethod("app.onInstallStateResponse", |
278 arraysize(argv), argv, NULL)); | 279 arraysize(argv), argv, NULL)); |
279 } | 280 } |
| 281 |
| 282 } // namespace extensions |
OLD | NEW |