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 = context()->extension(); |
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(context()->web_frame()); |
87 return GetDetailsForFrameImpl(context_->web_frame()); | 87 return GetDetailsForFrameImpl(context()->web_frame()); |
88 } | 88 } |
89 | 89 |
90 v8::Handle<v8::Value> AppBindings::GetDetailsForFrame( | 90 v8::Handle<v8::Value> AppBindings::GetDetailsForFrame( |
91 const v8::Arguments& args) { | 91 const v8::Arguments& args) { |
92 CHECK(context_->web_frame()); | 92 CHECK(context()->web_frame()); |
93 if (!CheckAccessToAppDetails(context_->web_frame())) | 93 if (!CheckAccessToAppDetails(context()->web_frame())) |
94 return v8::Undefined(); | 94 return v8::Undefined(); |
95 | 95 |
96 if (args.Length() < 0) | 96 if (args.Length() < 0) |
97 return v8::ThrowException(v8::String::New("Not enough arguments.")); | 97 return v8::ThrowException(v8::String::New("Not enough arguments.")); |
98 | 98 |
99 if (!args[0]->IsObject()) { | 99 if (!args[0]->IsObject()) { |
100 return v8::ThrowException( | 100 return v8::ThrowException( |
101 v8::String::New("Argument 0 must be an object.")); | 101 v8::String::New("Argument 0 must be an object.")); |
102 } | 102 } |
103 | 103 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 // Get the callbackId. | 136 // Get the callbackId. |
137 int callback_id = 0; | 137 int callback_id = 0; |
138 if (args.Length() == 1) { | 138 if (args.Length() == 1) { |
139 if (!args[0]->IsInt32()) { | 139 if (!args[0]->IsInt32()) { |
140 v8::ThrowException(v8::String::New(kInvalidCallbackIdError)); | 140 v8::ThrowException(v8::String::New(kInvalidCallbackIdError)); |
141 return v8::Undefined(); | 141 return v8::Undefined(); |
142 } | 142 } |
143 callback_id = args[0]->Int32Value(); | 143 callback_id = args[0]->Int32Value(); |
144 } | 144 } |
145 | 145 |
146 content::RenderView* render_view = context_->GetRenderView(); | 146 content::RenderView* render_view = context()->GetRenderView(); |
147 CHECK(render_view); | 147 CHECK(render_view); |
148 | 148 |
149 Send(new ExtensionHostMsg_GetAppInstallState( | 149 Send(new ExtensionHostMsg_GetAppInstallState( |
150 render_view->GetRoutingID(), context_->web_frame()->document().url(), | 150 render_view->GetRoutingID(), context()->web_frame()->document().url(), |
151 GetRoutingID(), callback_id)); | 151 GetRoutingID(), callback_id)); |
152 return v8::Undefined(); | 152 return v8::Undefined(); |
153 } | 153 } |
154 | 154 |
155 v8::Handle<v8::Value> AppBindings::GetRunningState(const v8::Arguments& args) { | 155 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 | 156 // To distinguish between ready_to_run and cannot_run states, we need the top |
157 // level frame. | 157 // level frame. |
158 const WebFrame* parent_frame = context_->web_frame(); | 158 const WebFrame* parent_frame = context()->web_frame(); |
159 while (parent_frame->parent()) | 159 while (parent_frame->parent()) |
160 parent_frame = parent_frame->parent(); | 160 parent_frame = parent_frame->parent(); |
161 | 161 |
162 const ExtensionSet* extensions = dispatcher_->extensions(); | 162 const ExtensionSet* extensions = dispatcher_->extensions(); |
163 | 163 |
164 // The app associated with the top level frame. | 164 // The app associated with the top level frame. |
165 const Extension* parent_app = extensions->GetHostedAppByURL( | 165 const Extension* parent_app = extensions->GetHostedAppByURL( |
166 ExtensionURLInfo(parent_frame->document().url())); | 166 ExtensionURLInfo(parent_frame->document().url())); |
167 | 167 |
168 // The app associated with this frame. | 168 // The app associated with this frame. |
169 const Extension* this_app = extensions->GetHostedAppByURL( | 169 const Extension* this_app = extensions->GetHostedAppByURL(ExtensionURLInfo( |
170 ExtensionURLInfo(context_->web_frame()->document().url())); | 170 context()->web_frame()->document().url())); |
171 | 171 |
172 if (!this_app || !parent_app) | 172 if (!this_app || !parent_app) |
173 return v8::String::New(extension_misc::kAppStateCannotRun); | 173 return v8::String::New(extension_misc::kAppStateCannotRun); |
174 | 174 |
175 const char* state = NULL; | 175 const char* state = NULL; |
176 if (dispatcher_->IsExtensionActive(parent_app->id())) { | 176 if (dispatcher_->IsExtensionActive(parent_app->id())) { |
177 if (parent_app == this_app) | 177 if (parent_app == this_app) |
178 state = extension_misc::kAppStateRunning; | 178 state = extension_misc::kAppStateRunning; |
179 else | 179 else |
180 state = extension_misc::kAppStateCannotRun; | 180 state = extension_misc::kAppStateCannotRun; |
(...skipping 11 matching lines...) Expand all Loading... |
192 IPC_MESSAGE_HANDLER(ExtensionMsg_GetAppInstallStateResponse, | 192 IPC_MESSAGE_HANDLER(ExtensionMsg_GetAppInstallStateResponse, |
193 OnAppInstallStateResponse) | 193 OnAppInstallStateResponse) |
194 IPC_MESSAGE_UNHANDLED(CHECK(false) << "Unhandled IPC message") | 194 IPC_MESSAGE_UNHANDLED(CHECK(false) << "Unhandled IPC message") |
195 IPC_END_MESSAGE_MAP() | 195 IPC_END_MESSAGE_MAP() |
196 return true; | 196 return true; |
197 } | 197 } |
198 | 198 |
199 void AppBindings::OnAppInstallStateResponse( | 199 void AppBindings::OnAppInstallStateResponse( |
200 const std::string& state, int callback_id) { | 200 const std::string& state, int callback_id) { |
201 v8::HandleScope handle_scope; | 201 v8::HandleScope handle_scope; |
202 v8::Context::Scope context_scope(context_->v8_context()); | 202 v8::Context::Scope context_scope(context()->v8_context()); |
203 v8::Handle<v8::Value> argv[2]; | 203 v8::Handle<v8::Value> argv[2]; |
204 argv[0] = v8::String::New(state.c_str()); | 204 argv[0] = v8::String::New(state.c_str()); |
205 argv[1] = v8::Integer::New(callback_id); | 205 argv[1] = v8::Integer::New(callback_id); |
206 CHECK(context_->CallChromeHiddenMethod("app.onInstallStateResponse", | 206 CHECK(context()->CallChromeHiddenMethod( |
207 arraysize(argv), argv, NULL)); | 207 "app.onInstallStateResponse", arraysize(argv), argv, NULL)); |
208 } | 208 } |
209 | 209 |
210 } // namespace extensions | 210 } // namespace extensions |
OLD | NEW |