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