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" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 RouteFunction("GetIsInstalled", | 62 RouteFunction("GetIsInstalled", |
63 base::Bind(&AppBindings::GetIsInstalled, base::Unretained(this))); | 63 base::Bind(&AppBindings::GetIsInstalled, base::Unretained(this))); |
64 RouteFunction("Install", | 64 RouteFunction("Install", |
65 base::Bind(&AppBindings::Install, base::Unretained(this))); | 65 base::Bind(&AppBindings::Install, base::Unretained(this))); |
66 RouteFunction("GetDetails", | 66 RouteFunction("GetDetails", |
67 base::Bind(&AppBindings::GetDetails, base::Unretained(this))); | 67 base::Bind(&AppBindings::GetDetails, base::Unretained(this))); |
68 RouteFunction("GetDetailsForFrame", | 68 RouteFunction("GetDetailsForFrame", |
69 base::Bind(&AppBindings::GetDetailsForFrame, base::Unretained(this))); | 69 base::Bind(&AppBindings::GetDetailsForFrame, base::Unretained(this))); |
70 RouteFunction("GetAppNotifyChannel", | 70 RouteFunction("GetAppNotifyChannel", |
71 base::Bind(&AppBindings::GetAppNotifyChannel, base::Unretained(this))); | 71 base::Bind(&AppBindings::GetAppNotifyChannel, base::Unretained(this))); |
72 RouteFunction("GetInstallState", | |
73 base::Bind(&AppBindings::GetInstallState, base::Unretained(this))); | |
74 RouteFunction("GetRunningState", | |
75 base::Bind(&AppBindings::GetRunningState, base::Unretained(this))); | |
72 } | 76 } |
73 | 77 |
74 v8::Handle<v8::Value> AppBindings::GetIsInstalled( | 78 v8::Handle<v8::Value> AppBindings::GetIsInstalled( |
75 const v8::Arguments& args) { | 79 const v8::Arguments& args) { |
76 const Extension* extension = context_->extension(); | 80 const Extension* extension = context_->extension(); |
77 | 81 |
78 // TODO(aa): Why only hosted app? | 82 // TODO(aa): Why only hosted app? |
79 bool result = extension && extension->is_hosted_app() && | 83 bool result = extension && extension->is_hosted_app() && |
80 extension_dispatcher_->IsExtensionActive(extension->id()); | 84 extension_dispatcher_->IsExtensionActive(extension->id()); |
81 return v8::Boolean::New(result); | 85 return v8::Boolean::New(result); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 | 181 |
178 content::RenderView* render_view = context_->GetRenderView(); | 182 content::RenderView* render_view = context_->GetRenderView(); |
179 CHECK(render_view); | 183 CHECK(render_view); |
180 | 184 |
181 Send(new ExtensionHostMsg_GetAppNotifyChannel( | 185 Send(new ExtensionHostMsg_GetAppNotifyChannel( |
182 render_view->GetRoutingID(), context_->web_frame()->document().url(), | 186 render_view->GetRoutingID(), context_->web_frame()->document().url(), |
183 client_id, GetRoutingID(), callback_id)); | 187 client_id, GetRoutingID(), callback_id)); |
184 return v8::Undefined(); | 188 return v8::Undefined(); |
185 } | 189 } |
186 | 190 |
191 v8::Handle<v8::Value> AppBindings::GetInstallState(const v8::Arguments& args) { | |
192 // Get the callbackId. | |
193 int callback_id = 0; | |
194 if (args.Length() == 1) { | |
195 if (!args[0]->IsInt32()) { | |
196 v8::ThrowException(v8::String::New(kInvalidCallbackIdError)); | |
197 return v8::Undefined(); | |
198 } | |
199 callback_id = args[0]->Int32Value(); | |
200 } | |
201 | |
202 content::RenderView* render_view = context_->GetRenderView(); | |
203 CHECK(render_view); | |
204 | |
205 Send(new ExtensionHostMsg_GetAppInstallState( | |
206 render_view->GetRoutingID(), context_->web_frame()->document().url(), | |
207 GetRoutingID(), callback_id)); | |
208 return v8::Undefined(); | |
209 } | |
210 | |
211 v8::Handle<v8::Value> AppBindings::GetRunningState(const v8::Arguments& args) { | |
212 // To distinguish between ready_to_run and cannot_run states, we need the top | |
213 // level frame. | |
214 const WebFrame* parent_frame = context_->web_frame(); | |
215 while (parent_frame->parent()) | |
216 parent_frame = parent_frame->parent(); | |
217 | |
218 const ExtensionSet* extensions = extension_dispatcher_->extensions(); | |
219 const char* state = NULL; | |
220 | |
221 // The app associated with the top level frame. | |
222 const Extension* parent_app = extensions->GetHostedAppByURL( | |
223 ExtensionURLInfo(parent_frame->document().url())); | |
224 | |
225 // The app associated with this frame. | |
226 const Extension* this_app = extensions->GetHostedAppByURL( | |
227 ExtensionURLInfo(context_->web_frame()->document().url())); | |
228 | |
229 if (!this_app || !parent_app) { | |
230 state = extension_misc::kAppStateCannotRun; | |
231 return v8::String::New(state); | |
miket_OOO
2012/04/24 00:04:48
Just for clarity, can you move state's declaration
jstritar
2012/04/24 00:11:59
Done.
| |
232 } | |
233 | |
234 if (extension_dispatcher_->IsExtensionActive(parent_app->id())) { | |
235 if (parent_app == this_app) | |
236 state = extension_misc::kAppStateRunning; | |
237 else | |
238 state = extension_misc::kAppStateCannotRun; | |
239 } else if (parent_app == this_app) { | |
240 state = extension_misc::kAppStateReadyToRun; | |
241 } else { | |
242 state = extension_misc::kAppStateCannotRun; | |
243 } | |
244 | |
245 return v8::String::New(state); | |
246 } | |
247 | |
187 bool AppBindings::OnMessageReceived(const IPC::Message& message) { | 248 bool AppBindings::OnMessageReceived(const IPC::Message& message) { |
188 IPC_BEGIN_MESSAGE_MAP(AppBindings, message) | 249 IPC_BEGIN_MESSAGE_MAP(AppBindings, message) |
189 IPC_MESSAGE_HANDLER(ExtensionMsg_GetAppNotifyChannelResponse, | 250 IPC_MESSAGE_HANDLER(ExtensionMsg_GetAppNotifyChannelResponse, |
190 OnGetAppNotifyChannelResponse) | 251 OnGetAppNotifyChannelResponse) |
252 IPC_MESSAGE_HANDLER(ExtensionMsg_GetAppInstallStateResponse, | |
253 OnAppInstallStateResponse) | |
191 IPC_MESSAGE_UNHANDLED(CHECK(false) << "Unhandled IPC message") | 254 IPC_MESSAGE_UNHANDLED(CHECK(false) << "Unhandled IPC message") |
192 IPC_END_MESSAGE_MAP() | 255 IPC_END_MESSAGE_MAP() |
193 return true; | 256 return true; |
194 } | 257 } |
195 | 258 |
196 void AppBindings::OnGetAppNotifyChannelResponse( | 259 void AppBindings::OnGetAppNotifyChannelResponse( |
197 const std::string& channel_id, const std::string& error, int callback_id) { | 260 const std::string& channel_id, const std::string& error, int callback_id) { |
198 v8::HandleScope handle_scope; | 261 v8::HandleScope handle_scope; |
199 v8::Context::Scope context_scope(context_->v8_context()); | 262 v8::Context::Scope context_scope(context_->v8_context()); |
200 v8::Handle<v8::Value> argv[3]; | 263 v8::Handle<v8::Value> argv[3]; |
201 argv[0] = v8::String::New(channel_id.c_str()); | 264 argv[0] = v8::String::New(channel_id.c_str()); |
202 argv[1] = v8::String::New(error.c_str()); | 265 argv[1] = v8::String::New(error.c_str()); |
203 argv[2] = v8::Integer::New(callback_id); | 266 argv[2] = v8::Integer::New(callback_id); |
204 CHECK(context_->CallChromeHiddenMethod("app.onGetAppNotifyChannelResponse", | 267 CHECK(context_->CallChromeHiddenMethod("app.onGetAppNotifyChannelResponse", |
205 arraysize(argv), argv, NULL)); | 268 arraysize(argv), argv, NULL)); |
206 } | 269 } |
270 | |
271 void AppBindings::OnAppInstallStateResponse( | |
272 const std::string& state, int callback_id) { | |
273 v8::HandleScope handle_scope; | |
274 v8::Context::Scope context_scope(context_->v8_context()); | |
275 v8::Handle<v8::Value> argv[2]; | |
276 argv[0] = v8::String::New(state.c_str()); | |
277 argv[1] = v8::Integer::New(callback_id); | |
278 CHECK(context_->CallChromeHiddenMethod("app.onInstallStateResponse", | |
279 arraysize(argv), argv, NULL)); | |
280 } | |
OLD | NEW |