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 |
| 220 // The app associated with the top level frame. |
| 221 const Extension* parent_app = extensions->GetHostedAppByURL( |
| 222 ExtensionURLInfo(parent_frame->document().url())); |
| 223 |
| 224 // The app associated with this frame. |
| 225 const Extension* this_app = extensions->GetHostedAppByURL( |
| 226 ExtensionURLInfo(context_->web_frame()->document().url())); |
| 227 |
| 228 if (!this_app || !parent_app) |
| 229 return v8::String::New(extension_misc::kAppStateCannotRun); |
| 230 |
| 231 const char* state = NULL; |
| 232 if (extension_dispatcher_->IsExtensionActive(parent_app->id())) { |
| 233 if (parent_app == this_app) |
| 234 state = extension_misc::kAppStateRunning; |
| 235 else |
| 236 state = extension_misc::kAppStateCannotRun; |
| 237 } else if (parent_app == this_app) { |
| 238 state = extension_misc::kAppStateReadyToRun; |
| 239 } else { |
| 240 state = extension_misc::kAppStateCannotRun; |
| 241 } |
| 242 |
| 243 return v8::String::New(state); |
| 244 } |
| 245 |
187 bool AppBindings::OnMessageReceived(const IPC::Message& message) { | 246 bool AppBindings::OnMessageReceived(const IPC::Message& message) { |
188 IPC_BEGIN_MESSAGE_MAP(AppBindings, message) | 247 IPC_BEGIN_MESSAGE_MAP(AppBindings, message) |
189 IPC_MESSAGE_HANDLER(ExtensionMsg_GetAppNotifyChannelResponse, | 248 IPC_MESSAGE_HANDLER(ExtensionMsg_GetAppNotifyChannelResponse, |
190 OnGetAppNotifyChannelResponse) | 249 OnGetAppNotifyChannelResponse) |
| 250 IPC_MESSAGE_HANDLER(ExtensionMsg_GetAppInstallStateResponse, |
| 251 OnAppInstallStateResponse) |
191 IPC_MESSAGE_UNHANDLED(CHECK(false) << "Unhandled IPC message") | 252 IPC_MESSAGE_UNHANDLED(CHECK(false) << "Unhandled IPC message") |
192 IPC_END_MESSAGE_MAP() | 253 IPC_END_MESSAGE_MAP() |
193 return true; | 254 return true; |
194 } | 255 } |
195 | 256 |
196 void AppBindings::OnGetAppNotifyChannelResponse( | 257 void AppBindings::OnGetAppNotifyChannelResponse( |
197 const std::string& channel_id, const std::string& error, int callback_id) { | 258 const std::string& channel_id, const std::string& error, int callback_id) { |
198 v8::HandleScope handle_scope; | 259 v8::HandleScope handle_scope; |
199 v8::Context::Scope context_scope(context_->v8_context()); | 260 v8::Context::Scope context_scope(context_->v8_context()); |
200 v8::Handle<v8::Value> argv[3]; | 261 v8::Handle<v8::Value> argv[3]; |
201 argv[0] = v8::String::New(channel_id.c_str()); | 262 argv[0] = v8::String::New(channel_id.c_str()); |
202 argv[1] = v8::String::New(error.c_str()); | 263 argv[1] = v8::String::New(error.c_str()); |
203 argv[2] = v8::Integer::New(callback_id); | 264 argv[2] = v8::Integer::New(callback_id); |
204 CHECK(context_->CallChromeHiddenMethod("app.onGetAppNotifyChannelResponse", | 265 CHECK(context_->CallChromeHiddenMethod("app.onGetAppNotifyChannelResponse", |
205 arraysize(argv), argv, NULL)); | 266 arraysize(argv), argv, NULL)); |
206 } | 267 } |
| 268 |
| 269 void AppBindings::OnAppInstallStateResponse( |
| 270 const std::string& state, int callback_id) { |
| 271 v8::HandleScope handle_scope; |
| 272 v8::Context::Scope context_scope(context_->v8_context()); |
| 273 v8::Handle<v8::Value> argv[2]; |
| 274 argv[0] = v8::String::New(state.c_str()); |
| 275 argv[1] = v8::Integer::New(callback_id); |
| 276 CHECK(context_->CallChromeHiddenMethod("app.onInstallStateResponse", |
| 277 arraysize(argv), argv, NULL)); |
| 278 } |
OLD | NEW |