Chromium Code Reviews| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 50 } | 50 } |
| 51 | 51 |
| 52 return true; | 52 return true; |
| 53 } | 53 } |
| 54 | 54 |
| 55 const char* kInvalidCallbackIdError = "Invalid callbackId"; | 55 const char* kInvalidCallbackIdError = "Invalid callbackId"; |
| 56 | 56 |
| 57 } // namespace | 57 } // namespace |
| 58 | 58 |
| 59 AppBindings::AppBindings(Dispatcher* dispatcher, ChromeV8Context* context) | 59 AppBindings::AppBindings(Dispatcher* dispatcher, ChromeV8Context* context) |
| 60 : ChromeV8Extension(dispatcher, context->v8_context()), | 60 : ChromeV8Extension(dispatcher, context), |
| 61 ChromeV8ExtensionHandler(context) { | 61 ChromeV8ExtensionHandler(context) { |
| 62 RouteFunction("GetIsInstalled", | 62 RouteFunction("GetIsInstalled", |
| 63 base::Bind(&AppBindings::GetIsInstalled, base::Unretained(this))); | 63 base::Bind(&AppBindings::GetIsInstalled, base::Unretained(this))); |
| 64 RouteFunction("GetDetails", | 64 RouteFunction("GetDetails", |
| 65 base::Bind(&AppBindings::GetDetails, base::Unretained(this))); | 65 base::Bind(&AppBindings::GetDetails, base::Unretained(this))); |
| 66 RouteFunction("GetDetailsForFrame", | 66 RouteFunction("GetDetailsForFrame", |
| 67 base::Bind(&AppBindings::GetDetailsForFrame, base::Unretained(this))); | 67 base::Bind(&AppBindings::GetDetailsForFrame, base::Unretained(this))); |
| 68 RouteFunction("GetInstallState", | 68 RouteFunction("GetInstallState", |
| 69 base::Bind(&AppBindings::GetInstallState, base::Unretained(this))); | 69 base::Bind(&AppBindings::GetInstallState, base::Unretained(this))); |
| 70 RouteFunction("GetRunningState", | 70 RouteFunction("GetRunningState", |
| 71 base::Bind(&AppBindings::GetRunningState, base::Unretained(this))); | 71 base::Bind(&AppBindings::GetRunningState, base::Unretained(this))); |
| 72 } | 72 } |
| 73 | 73 |
| 74 v8::Handle<v8::Value> AppBindings::GetIsInstalled( | 74 v8::Handle<v8::Value> AppBindings::GetIsInstalled( |
| 75 const v8::Arguments& args) { | 75 const v8::Arguments& args) { |
| 76 const Extension* extension = context_->extension(); | 76 const Extension* extension = ChromeV8ExtensionHandler::context_->extension(); |
|
not at google - send to devlin
2013/05/29 15:55:13
see comment in ObjectBackedNativeHandler: could yo
marja
2013/05/29 16:56:33
Done.
| |
| 77 | 77 |
| 78 // TODO(aa): Why only hosted app? | 78 // TODO(aa): Why only hosted app? |
| 79 bool result = extension && extension->is_hosted_app() && | 79 bool result = extension && extension->is_hosted_app() && |
| 80 dispatcher_->IsExtensionActive(extension->id()); | 80 dispatcher_->IsExtensionActive(extension->id()); |
| 81 return v8::Boolean::New(result); | 81 return v8::Boolean::New(result); |
| 82 } | 82 } |
| 83 | 83 |
| 84 v8::Handle<v8::Value> AppBindings::GetDetails( | 84 v8::Handle<v8::Value> AppBindings::GetDetails( |
| 85 const v8::Arguments& args) { | 85 const v8::Arguments& args) { |
| 86 CHECK(context_->web_frame()); | 86 CHECK(ChromeV8ExtensionHandler::context_->web_frame()); |
| 87 return GetDetailsForFrameImpl(context_->web_frame()); | 87 return GetDetailsForFrameImpl( |
| 88 ChromeV8ExtensionHandler::context_->web_frame()); | |
| 88 } | 89 } |
| 89 | 90 |
| 90 v8::Handle<v8::Value> AppBindings::GetDetailsForFrame( | 91 v8::Handle<v8::Value> AppBindings::GetDetailsForFrame( |
| 91 const v8::Arguments& args) { | 92 const v8::Arguments& args) { |
| 92 CHECK(context_->web_frame()); | 93 CHECK(ChromeV8ExtensionHandler::context_->web_frame()); |
| 93 if (!CheckAccessToAppDetails(context_->web_frame())) | 94 if (!CheckAccessToAppDetails(ChromeV8ExtensionHandler::context_->web_frame())) |
| 94 return v8::Undefined(); | 95 return v8::Undefined(); |
| 95 | 96 |
| 96 if (args.Length() < 0) | 97 if (args.Length() < 0) |
| 97 return v8::ThrowException(v8::String::New("Not enough arguments.")); | 98 return v8::ThrowException(v8::String::New("Not enough arguments.")); |
| 98 | 99 |
| 99 if (!args[0]->IsObject()) { | 100 if (!args[0]->IsObject()) { |
| 100 return v8::ThrowException( | 101 return v8::ThrowException( |
| 101 v8::String::New("Argument 0 must be an object.")); | 102 v8::String::New("Argument 0 must be an object.")); |
| 102 } | 103 } |
| 103 | 104 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 136 // Get the callbackId. | 137 // Get the callbackId. |
| 137 int callback_id = 0; | 138 int callback_id = 0; |
| 138 if (args.Length() == 1) { | 139 if (args.Length() == 1) { |
| 139 if (!args[0]->IsInt32()) { | 140 if (!args[0]->IsInt32()) { |
| 140 v8::ThrowException(v8::String::New(kInvalidCallbackIdError)); | 141 v8::ThrowException(v8::String::New(kInvalidCallbackIdError)); |
| 141 return v8::Undefined(); | 142 return v8::Undefined(); |
| 142 } | 143 } |
| 143 callback_id = args[0]->Int32Value(); | 144 callback_id = args[0]->Int32Value(); |
| 144 } | 145 } |
| 145 | 146 |
| 146 content::RenderView* render_view = context_->GetRenderView(); | 147 content::RenderView* render_view = |
| 148 ChromeV8ExtensionHandler::context_->GetRenderView(); | |
| 147 CHECK(render_view); | 149 CHECK(render_view); |
| 148 | 150 |
| 149 Send(new ExtensionHostMsg_GetAppInstallState( | 151 Send(new ExtensionHostMsg_GetAppInstallState( |
| 150 render_view->GetRoutingID(), context_->web_frame()->document().url(), | 152 render_view->GetRoutingID(), |
| 151 GetRoutingID(), callback_id)); | 153 ChromeV8ExtensionHandler::context_->web_frame()->document().url(), |
| 154 GetRoutingID(), | |
| 155 callback_id)); | |
| 152 return v8::Undefined(); | 156 return v8::Undefined(); |
| 153 } | 157 } |
| 154 | 158 |
| 155 v8::Handle<v8::Value> AppBindings::GetRunningState(const v8::Arguments& args) { | 159 v8::Handle<v8::Value> AppBindings::GetRunningState(const v8::Arguments& args) { |
| 156 // To distinguish between ready_to_run and cannot_run states, we need the top | 160 // To distinguish between ready_to_run and cannot_run states, we need the top |
| 157 // level frame. | 161 // level frame. |
| 158 const WebFrame* parent_frame = context_->web_frame(); | 162 const WebFrame* parent_frame = |
| 163 ChromeV8ExtensionHandler::context_->web_frame(); | |
| 159 while (parent_frame->parent()) | 164 while (parent_frame->parent()) |
| 160 parent_frame = parent_frame->parent(); | 165 parent_frame = parent_frame->parent(); |
| 161 | 166 |
| 162 const ExtensionSet* extensions = dispatcher_->extensions(); | 167 const ExtensionSet* extensions = dispatcher_->extensions(); |
| 163 | 168 |
| 164 // The app associated with the top level frame. | 169 // The app associated with the top level frame. |
| 165 const Extension* parent_app = extensions->GetHostedAppByURL( | 170 const Extension* parent_app = extensions->GetHostedAppByURL( |
| 166 ExtensionURLInfo(parent_frame->document().url())); | 171 ExtensionURLInfo(parent_frame->document().url())); |
| 167 | 172 |
| 168 // The app associated with this frame. | 173 // The app associated with this frame. |
| 169 const Extension* this_app = extensions->GetHostedAppByURL( | 174 const Extension* this_app = extensions->GetHostedAppByURL(ExtensionURLInfo( |
| 170 ExtensionURLInfo(context_->web_frame()->document().url())); | 175 ChromeV8ExtensionHandler::context_->web_frame()->document().url())); |
| 171 | 176 |
| 172 if (!this_app || !parent_app) | 177 if (!this_app || !parent_app) |
| 173 return v8::String::New(extension_misc::kAppStateCannotRun); | 178 return v8::String::New(extension_misc::kAppStateCannotRun); |
| 174 | 179 |
| 175 const char* state = NULL; | 180 const char* state = NULL; |
| 176 if (dispatcher_->IsExtensionActive(parent_app->id())) { | 181 if (dispatcher_->IsExtensionActive(parent_app->id())) { |
| 177 if (parent_app == this_app) | 182 if (parent_app == this_app) |
| 178 state = extension_misc::kAppStateRunning; | 183 state = extension_misc::kAppStateRunning; |
| 179 else | 184 else |
| 180 state = extension_misc::kAppStateCannotRun; | 185 state = extension_misc::kAppStateCannotRun; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 192 IPC_MESSAGE_HANDLER(ExtensionMsg_GetAppInstallStateResponse, | 197 IPC_MESSAGE_HANDLER(ExtensionMsg_GetAppInstallStateResponse, |
| 193 OnAppInstallStateResponse) | 198 OnAppInstallStateResponse) |
| 194 IPC_MESSAGE_UNHANDLED(CHECK(false) << "Unhandled IPC message") | 199 IPC_MESSAGE_UNHANDLED(CHECK(false) << "Unhandled IPC message") |
| 195 IPC_END_MESSAGE_MAP() | 200 IPC_END_MESSAGE_MAP() |
| 196 return true; | 201 return true; |
| 197 } | 202 } |
| 198 | 203 |
| 199 void AppBindings::OnAppInstallStateResponse( | 204 void AppBindings::OnAppInstallStateResponse( |
| 200 const std::string& state, int callback_id) { | 205 const std::string& state, int callback_id) { |
| 201 v8::HandleScope handle_scope; | 206 v8::HandleScope handle_scope; |
| 202 v8::Context::Scope context_scope(context_->v8_context()); | 207 v8::Context::Scope context_scope( |
| 208 ChromeV8ExtensionHandler::context_->v8_context()); | |
| 203 v8::Handle<v8::Value> argv[2]; | 209 v8::Handle<v8::Value> argv[2]; |
| 204 argv[0] = v8::String::New(state.c_str()); | 210 argv[0] = v8::String::New(state.c_str()); |
| 205 argv[1] = v8::Integer::New(callback_id); | 211 argv[1] = v8::Integer::New(callback_id); |
| 206 CHECK(context_->CallChromeHiddenMethod("app.onInstallStateResponse", | 212 CHECK(ChromeV8ExtensionHandler::context_->CallChromeHiddenMethod( |
| 207 arraysize(argv), argv, NULL)); | 213 "app.onInstallStateResponse", arraysize(argv), argv, NULL)); |
| 208 } | 214 } |
| 209 | 215 |
| 210 } // namespace extensions | 216 } // namespace extensions |
| OLD | NEW |