| 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/browser/extensions/platform_app_launcher.h" | 5 #include "chrome/browser/extensions/platform_app_launcher.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 // The extension providing the app. | 218 // The extension providing the app. |
| 219 const Extension* extension_; | 219 const Extension* extension_; |
| 220 // The command line to be passed through to the app, or NULL. | 220 // The command line to be passed through to the app, or NULL. |
| 221 const CommandLine* command_line_; | 221 const CommandLine* command_line_; |
| 222 // If non-empty, this is used to expand relative paths. | 222 // If non-empty, this is used to expand relative paths. |
| 223 const FilePath current_directory_; | 223 const FilePath current_directory_; |
| 224 | 224 |
| 225 DISALLOW_COPY_AND_ASSIGN(PlatformAppCommandLineLauncher); | 225 DISALLOW_COPY_AND_ASSIGN(PlatformAppCommandLineLauncher); |
| 226 }; | 226 }; |
| 227 | 227 |
| 228 // Class to handle launching of platform apps with WebIntent data that is being | 228 // Class to handle launching of platform apps with WebIntent data. |
| 229 // passed in a a blob. | |
| 230 // An instance of this class is created for each launch. The lifetime of these | 229 // An instance of this class is created for each launch. The lifetime of these |
| 231 // instances is managed by reference counted pointers. As long as an instance | 230 // instances is managed by reference counted pointers. As long as an instance |
| 232 // has outstanding tasks on a message queue it will be retained; once all | 231 // has outstanding tasks on a message queue it will be retained; once all |
| 233 // outstanding tasks are completed it will be deleted. | 232 // outstanding tasks are completed it will be deleted. |
| 234 class PlatformAppBlobIntentLauncher | 233 class PlatformAppWebIntentLauncher |
| 235 : public base::RefCountedThreadSafe<PlatformAppBlobIntentLauncher> { | 234 : public base::RefCountedThreadSafe<PlatformAppWebIntentLauncher> { |
| 236 public: | 235 public: |
| 237 PlatformAppBlobIntentLauncher(Profile* profile, | 236 PlatformAppWebIntentLauncher(Profile* profile, |
| 238 const Extension* extension, | 237 const Extension* extension, |
| 239 const webkit_glue::WebIntentData& data) | 238 const webkit_glue::WebIntentData& data) |
| 240 : profile_(profile), | 239 : profile_(profile), |
| 241 extension_(extension), | 240 extension_(extension), |
| 242 data_(data) {} | 241 data_(data) {} |
| 243 | 242 |
| 244 void Launch() { | 243 void Launch() { |
| 245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 244 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 245 if (data_.data_type != webkit_glue::WebIntentData::BLOB && |
| 246 data_.data_type != webkit_glue::WebIntentData::FILESYSTEM) { |
| 247 InternalLaunch(); |
| 248 return; |
| 249 } |
| 246 | 250 |
| 247 // Access needs to be granted to the file for the process associated with | 251 // Access needs to be granted to the file or filesystem for the process |
| 248 // the extension. To do this the ExtensionHost is needed. This might not be | 252 // associated with the extension. To do this the ExtensionHost is needed. |
| 249 // available, or it might be in the process of being unloaded, in which case | 253 // This might not be available, or it might be in the process of being |
| 250 // the lazy background task queue is used to load the extension and then | 254 // unloaded, in which case the lazy background task queue is used to load |
| 251 // call back to us. | 255 // he extension and then call back to us. |
| 252 extensions::LazyBackgroundTaskQueue* queue = | 256 extensions::LazyBackgroundTaskQueue* queue = |
| 253 ExtensionSystem::Get(profile_)->lazy_background_task_queue(); | 257 ExtensionSystem::Get(profile_)->lazy_background_task_queue(); |
| 254 if (queue->ShouldEnqueueTask(profile_, extension_)) { | 258 if (queue->ShouldEnqueueTask(profile_, extension_)) { |
| 255 queue->AddPendingTask(profile_, extension_->id(), base::Bind( | 259 queue->AddPendingTask(profile_, extension_->id(), base::Bind( |
| 256 &PlatformAppBlobIntentLauncher::GrantAccessToFileAndLaunch, | 260 &PlatformAppWebIntentLauncher::GrantAccessToFileAndLaunch, |
| 257 this)); | 261 this)); |
| 258 return; | 262 return; |
| 259 } | 263 } |
| 260 | |
| 261 ExtensionProcessManager* process_manager = | 264 ExtensionProcessManager* process_manager = |
| 262 ExtensionSystem::Get(profile_)->process_manager(); | 265 ExtensionSystem::Get(profile_)->process_manager(); |
| 263 extensions::ExtensionHost* host = | 266 extensions::ExtensionHost* host = |
| 264 process_manager->GetBackgroundHostForExtension(extension_->id()); | 267 process_manager->GetBackgroundHostForExtension(extension_->id()); |
| 265 DCHECK(host); | 268 DCHECK(host); |
| 266 GrantAccessToFileAndLaunch(host); | 269 GrantAccessToFileAndLaunch(host); |
| 267 } | 270 } |
| 268 | 271 |
| 269 private: | 272 private: |
| 270 friend class base::RefCountedThreadSafe<PlatformAppBlobIntentLauncher>; | 273 friend class base::RefCountedThreadSafe<PlatformAppWebIntentLauncher>; |
| 271 | 274 |
| 272 virtual ~PlatformAppBlobIntentLauncher() {} | 275 virtual ~PlatformAppWebIntentLauncher() {} |
| 273 | 276 |
| 274 void GrantAccessToFileAndLaunch(extensions::ExtensionHost* host) { | 277 void GrantAccessToFileAndLaunch(extensions::ExtensionHost* host) { |
| 275 // If there was an error loading the app page, |host| will be NULL. | 278 // If there was an error loading the app page, |host| will be NULL. |
| 276 if (!host) { | 279 if (!host) { |
| 277 LOG(ERROR) << "Could not load app page for " << extension_->id(); | 280 LOG(ERROR) << "Could not load app page for " << extension_->id(); |
| 278 return; | 281 return; |
| 279 } | 282 } |
| 280 | 283 |
| 281 content::ChildProcessSecurityPolicy* policy = | 284 content::ChildProcessSecurityPolicy* policy = |
| 282 content::ChildProcessSecurityPolicy::GetInstance(); | 285 content::ChildProcessSecurityPolicy::GetInstance(); |
| 283 int renderer_id = host->render_process_host()->GetID(); | 286 int renderer_id = host->render_process_host()->GetID(); |
| 284 | 287 |
| 285 // Granting read file permission to allow reading file content. | 288 if (data_.data_type == webkit_glue::WebIntentData::BLOB) { |
| 286 // If the renderer already has permission to read these paths, it is not | 289 // Granting read file permission to allow reading file content. |
| 287 // regranted, as this would overwrite any other permissions which the | 290 // If the renderer already has permission to read these paths, it is not |
| 288 // renderer may already have. | 291 // regranted, as this would overwrite any other permissions which the |
| 289 if (!policy->CanReadFile(renderer_id, data_.blob_file)) | 292 // renderer may already have. |
| 290 policy->GrantReadFile(renderer_id, data_.blob_file); | 293 if (!policy->CanReadFile(renderer_id, data_.blob_file)) |
| 294 policy->GrantReadFile(renderer_id, data_.blob_file); |
| 295 } else if (data_.data_type == webkit_glue::WebIntentData::FILESYSTEM) { |
| 296 // Grant read filesystem and read directory permission to allow reading |
| 297 // any part of the specified filesystem. |
| 298 FilePath path; |
| 299 const bool valid = |
| 300 fileapi::IsolatedContext::GetInstance()->GetRegisteredPath( |
| 301 data_.filesystem_id, &path); |
| 302 DCHECK(valid); |
| 303 if (!policy->CanReadFile(renderer_id, path)) |
| 304 policy->GrantReadFile(renderer_id, path); |
| 305 policy->GrantReadFileSystem(renderer_id, data_.filesystem_id); |
| 306 } else { |
| 307 NOTREACHED(); |
| 308 } |
| 309 InternalLaunch(); |
| 310 } |
| 291 | 311 |
| 312 void InternalLaunch() { |
| 292 extensions::AppEventRouter::DispatchOnLaunchedEventWithWebIntent( | 313 extensions::AppEventRouter::DispatchOnLaunchedEventWithWebIntent( |
| 293 profile_, extension_, data_); | 314 profile_, extension_, data_); |
| 294 } | 315 } |
| 295 | 316 |
| 296 // The profile the app should be run in. | 317 // The profile the app should be run in. |
| 297 Profile* profile_; | 318 Profile* profile_; |
| 298 // The extension providing the app. | 319 // The extension providing the app. |
| 299 const Extension* extension_; | 320 const Extension* extension_; |
| 300 // The WebIntent data to be passed through to the app. | 321 // The WebIntent data to be passed through to the app. |
| 301 const webkit_glue::WebIntentData data_; | 322 const webkit_glue::WebIntentData data_; |
| 302 | 323 |
| 303 DISALLOW_COPY_AND_ASSIGN(PlatformAppBlobIntentLauncher); | 324 DISALLOW_COPY_AND_ASSIGN(PlatformAppWebIntentLauncher); |
| 304 }; | 325 }; |
| 305 | 326 |
| 306 } // namespace | 327 } // namespace |
| 307 | 328 |
| 308 void LaunchPlatformApp(Profile* profile, | 329 void LaunchPlatformApp(Profile* profile, |
| 309 const Extension* extension, | 330 const Extension* extension, |
| 310 const CommandLine* command_line, | 331 const CommandLine* command_line, |
| 311 const FilePath& current_directory) { | 332 const FilePath& current_directory) { |
| 312 // launcher will be freed when nothing has a reference to it. The message | 333 // launcher will be freed when nothing has a reference to it. The message |
| 313 // queue will retain a reference for any outstanding task, so when the | 334 // queue will retain a reference for any outstanding task, so when the |
| 314 // launcher has finished it will be freed. | 335 // launcher has finished it will be freed. |
| 315 scoped_refptr<PlatformAppCommandLineLauncher> launcher = | 336 scoped_refptr<PlatformAppCommandLineLauncher> launcher = |
| 316 new PlatformAppCommandLineLauncher(profile, extension, command_line, | 337 new PlatformAppCommandLineLauncher(profile, extension, command_line, |
| 317 current_directory); | 338 current_directory); |
| 318 launcher->Launch(); | 339 launcher->Launch(); |
| 319 } | 340 } |
| 320 | 341 |
| 321 void LaunchPlatformAppWithWebIntent( | 342 void LaunchPlatformAppWithWebIntent( |
| 322 Profile* profile, | 343 Profile* profile, |
| 323 const Extension* extension, | 344 const Extension* extension, |
| 324 const webkit_glue::WebIntentData& web_intent_data) { | 345 const webkit_glue::WebIntentData& web_intent_data) { |
| 325 if (web_intent_data.data_type == webkit_glue::WebIntentData::BLOB) { | 346 scoped_refptr<PlatformAppWebIntentLauncher> launcher = |
| 326 scoped_refptr<PlatformAppBlobIntentLauncher> launcher = | 347 new PlatformAppWebIntentLauncher(profile, extension, web_intent_data); |
| 327 new PlatformAppBlobIntentLauncher(profile, extension, web_intent_data); | 348 launcher->Launch(); |
| 328 launcher->Launch(); | |
| 329 return; | |
| 330 } | |
| 331 | |
| 332 extensions::AppEventRouter::DispatchOnLaunchedEventWithWebIntent( | |
| 333 profile, extension, web_intent_data); | |
| 334 } | 349 } |
| 335 | 350 |
| 336 } // namespace extensions | 351 } // namespace extensions |
| OLD | NEW |