Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(316)

Side by Side Diff: apps/launcher.cc

Issue 2729503007: Remove Profile usage from //apps (Closed)
Patch Set: deps Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « apps/launcher.h ('k') | apps/saved_files_service.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "apps/launcher.h" 5 #include "apps/launcher.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <set> 8 #include <set>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h" 13 #include "base/files/file_util.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "base/memory/ref_counted.h" 16 #include "base/memory/ref_counted.h"
17 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
19 #include "chrome/browser/profiles/profile.h" 19 #include "content/public/browser/browser_context.h"
20 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/render_process_host.h" 21 #include "content/public/browser/render_process_host.h"
22 #include "content/public/browser/web_contents.h" 22 #include "content/public/browser/web_contents.h"
23 #include "content/public/common/content_switches.h" 23 #include "content/public/common/content_switches.h"
24 #include "content/public/common/url_constants.h" 24 #include "content/public/common/url_constants.h"
25 #include "extensions/browser/api/app_runtime/app_runtime_api.h" 25 #include "extensions/browser/api/app_runtime/app_runtime_api.h"
26 #include "extensions/browser/api/file_handlers/app_file_handler_util.h" 26 #include "extensions/browser/api/file_handlers/app_file_handler_util.h"
27 #include "extensions/browser/api/file_handlers/directory_util.h" 27 #include "extensions/browser/api/file_handlers/directory_util.h"
28 #include "extensions/browser/api/file_handlers/mime_util.h" 28 #include "extensions/browser/api/file_handlers/mime_util.h"
29 #include "extensions/browser/entry_info.h" 29 #include "extensions/browser/entry_info.h"
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 } 87 }
88 88
89 // Class to handle launching of platform apps to open specific paths. 89 // Class to handle launching of platform apps to open specific paths.
90 // An instance of this class is created for each launch. The lifetime of these 90 // An instance of this class is created for each launch. The lifetime of these
91 // instances is managed by reference counted pointers. As long as an instance 91 // instances is managed by reference counted pointers. As long as an instance
92 // has outstanding tasks on a message queue it will be retained; once all 92 // has outstanding tasks on a message queue it will be retained; once all
93 // outstanding tasks are completed it will be deleted. 93 // outstanding tasks are completed it will be deleted.
94 class PlatformAppPathLauncher 94 class PlatformAppPathLauncher
95 : public base::RefCountedThreadSafe<PlatformAppPathLauncher> { 95 : public base::RefCountedThreadSafe<PlatformAppPathLauncher> {
96 public: 96 public:
97 PlatformAppPathLauncher(Profile* profile, 97 PlatformAppPathLauncher(content::BrowserContext* context,
98 const Extension* app, 98 const Extension* app,
99 const std::vector<base::FilePath>& entry_paths) 99 const std::vector<base::FilePath>& entry_paths)
100 : profile_(profile), 100 : context_(context),
101 extension_id(app->id()), 101 extension_id(app->id()),
102 entry_paths_(entry_paths), 102 entry_paths_(entry_paths),
103 mime_type_collector_(profile), 103 mime_type_collector_(context),
104 is_directory_collector_(profile) {} 104 is_directory_collector_(context) {}
105 105
106 PlatformAppPathLauncher(Profile* profile, 106 PlatformAppPathLauncher(content::BrowserContext* context,
107 const Extension* app, 107 const Extension* app,
108 const base::FilePath& file_path) 108 const base::FilePath& file_path)
109 : profile_(profile), 109 : context_(context),
110 extension_id(app->id()), 110 extension_id(app->id()),
111 mime_type_collector_(profile), 111 mime_type_collector_(context),
112 is_directory_collector_(profile) { 112 is_directory_collector_(context) {
113 if (!file_path.empty()) 113 if (!file_path.empty())
114 entry_paths_.push_back(file_path); 114 entry_paths_.push_back(file_path);
115 } 115 }
116 116
117 void set_action_data(std::unique_ptr<app_runtime::ActionData> action_data) { 117 void set_action_data(std::unique_ptr<app_runtime::ActionData> action_data) {
118 action_data_ = std::move(action_data); 118 action_data_ = std::move(action_data);
119 } 119 }
120 120
121 void set_launch_source(extensions::AppLaunchSource launch_source) { 121 void set_launch_source(extensions::AppLaunchSource launch_source) {
122 launch_source_ = launch_source; 122 launch_source_ = launch_source;
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 201
202 const Extension* app = GetExtension(); 202 const Extension* app = GetExtension();
203 if (!app) 203 if (!app)
204 return; 204 return;
205 205
206 std::unique_ptr<app_runtime::LaunchData> launch_data = 206 std::unique_ptr<app_runtime::LaunchData> launch_data =
207 base::MakeUnique<app_runtime::LaunchData>(); 207 base::MakeUnique<app_runtime::LaunchData>();
208 launch_data->action_data = std::move(action_data_); 208 launch_data->action_data = std::move(action_data_);
209 209
210 AppRuntimeEventRouter::DispatchOnLaunchedEvent( 210 AppRuntimeEventRouter::DispatchOnLaunchedEvent(
211 profile_, app, launch_source_, std::move(launch_data)); 211 context_, app, launch_source_, std::move(launch_data));
212 } 212 }
213 213
214 void OnAreDirectoriesCollected( 214 void OnAreDirectoriesCollected(
215 bool has_file_system_write_permission, 215 bool has_file_system_write_permission,
216 std::unique_ptr<std::set<base::FilePath>> directory_paths) { 216 std::unique_ptr<std::set<base::FilePath>> directory_paths) {
217 if (has_file_system_write_permission) { 217 if (has_file_system_write_permission) {
218 std::set<base::FilePath>* const directory_paths_ptr = 218 std::set<base::FilePath>* const directory_paths_ptr =
219 directory_paths.get(); 219 directory_paths.get();
220 PrepareFilesForWritableApp( 220 PrepareFilesForWritableApp(
221 entry_paths_, profile_, *directory_paths_ptr, 221 entry_paths_, context_, *directory_paths_ptr,
222 base::Bind(&PlatformAppPathLauncher::OnFilesValid, this, 222 base::Bind(&PlatformAppPathLauncher::OnFilesValid, this,
223 base::Passed(std::move(directory_paths))), 223 base::Passed(std::move(directory_paths))),
224 base::Bind(&PlatformAppPathLauncher::OnFilesInvalid, this)); 224 base::Bind(&PlatformAppPathLauncher::OnFilesInvalid, this));
225 return; 225 return;
226 } 226 }
227 227
228 OnFilesValid(std::move(directory_paths)); 228 OnFilesValid(std::move(directory_paths));
229 } 229 }
230 230
231 void OnAreDirectoriesAndMimeTypesCollected( 231 void OnAreDirectoriesAndMimeTypesCollected(
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
279 279
280 if (handler_id_.empty()) 280 if (handler_id_.empty())
281 handler_id_ = handler->id; 281 handler_id_ = handler->id;
282 282
283 // Access needs to be granted to the file for the process associated with 283 // Access needs to be granted to the file for the process associated with
284 // the extension. To do this the ExtensionHost is needed. This might not be 284 // the extension. To do this the ExtensionHost is needed. This might not be
285 // available, or it might be in the process of being unloaded, in which case 285 // available, or it might be in the process of being unloaded, in which case
286 // the lazy background task queue is used to load the extension and then 286 // the lazy background task queue is used to load the extension and then
287 // call back to us. 287 // call back to us.
288 extensions::LazyBackgroundTaskQueue* const queue = 288 extensions::LazyBackgroundTaskQueue* const queue =
289 extensions::LazyBackgroundTaskQueue::Get(profile_); 289 extensions::LazyBackgroundTaskQueue::Get(context_);
290 if (queue->ShouldEnqueueTask(profile_, app)) { 290 if (queue->ShouldEnqueueTask(context_, app)) {
291 queue->AddPendingTask( 291 queue->AddPendingTask(
292 profile_, extension_id, 292 context_, extension_id,
293 base::Bind(&PlatformAppPathLauncher::GrantAccessToFilesAndLaunch, 293 base::Bind(&PlatformAppPathLauncher::GrantAccessToFilesAndLaunch,
294 this)); 294 this));
295 return; 295 return;
296 } 296 }
297 297
298 extensions::ProcessManager* const process_manager = 298 extensions::ProcessManager* const process_manager =
299 extensions::ProcessManager::Get(profile_); 299 extensions::ProcessManager::Get(context_);
300 ExtensionHost* const host = 300 ExtensionHost* const host =
301 process_manager->GetBackgroundHostForExtension(extension_id); 301 process_manager->GetBackgroundHostForExtension(extension_id);
302 DCHECK(host); 302 DCHECK(host);
303 GrantAccessToFilesAndLaunch(host); 303 GrantAccessToFilesAndLaunch(host);
304 } 304 }
305 305
306 void GrantAccessToFilesAndLaunch(ExtensionHost* host) { 306 void GrantAccessToFilesAndLaunch(ExtensionHost* host) {
307 const Extension* app = GetExtension(); 307 const Extension* app = GetExtension();
308 if (!app) 308 if (!app)
309 return; 309 return;
310 310
311 // If there was an error loading the app page, |host| will be NULL. 311 // If there was an error loading the app page, |host| will be NULL.
312 if (!host) { 312 if (!host) {
313 LOG(ERROR) << "Could not load app page for " << extension_id; 313 LOG(ERROR) << "Could not load app page for " << extension_id;
314 return; 314 return;
315 } 315 }
316 316
317 std::vector<GrantedFileEntry> granted_entries; 317 std::vector<GrantedFileEntry> granted_entries;
318 for (size_t i = 0; i < entry_paths_.size(); ++i) { 318 for (size_t i = 0; i < entry_paths_.size(); ++i) {
319 granted_entries.push_back( 319 granted_entries.push_back(
320 CreateFileEntry(profile_, app, host->render_process_host()->GetID(), 320 CreateFileEntry(context_, app, host->render_process_host()->GetID(),
321 entries_[i].path, entries_[i].is_directory)); 321 entries_[i].path, entries_[i].is_directory));
322 } 322 }
323 323
324 AppRuntimeEventRouter::DispatchOnLaunchedEventWithFileEntries( 324 AppRuntimeEventRouter::DispatchOnLaunchedEventWithFileEntries(
325 profile_, app, launch_source_, handler_id_, entries_, granted_entries, 325 context_, app, launch_source_, handler_id_, entries_, granted_entries,
326 std::move(action_data_)); 326 std::move(action_data_));
327 } 327 }
328 328
329 const Extension* GetExtension() const { 329 const Extension* GetExtension() const {
330 return extensions::ExtensionRegistry::Get(profile_)->GetExtensionById( 330 return extensions::ExtensionRegistry::Get(context_)->GetExtensionById(
331 extension_id, extensions::ExtensionRegistry::EVERYTHING); 331 extension_id, extensions::ExtensionRegistry::EVERYTHING);
332 } 332 }
333 333
334 // The profile the app should be run in. 334 // The browser context the app should be run in.
335 Profile* profile_; 335 content::BrowserContext* context_;
336 // The id of the extension providing the app. A pointer to the extension is 336 // The id of the extension providing the app. A pointer to the extension is
337 // not kept as the extension may be unloaded and deleted during the course of 337 // not kept as the extension may be unloaded and deleted during the course of
338 // the launch. 338 // the launch.
339 const std::string extension_id; 339 const std::string extension_id;
340 extensions::AppLaunchSource launch_source_ = extensions::SOURCE_FILE_HANDLER; 340 extensions::AppLaunchSource launch_source_ = extensions::SOURCE_FILE_HANDLER;
341 std::unique_ptr<app_runtime::ActionData> action_data_; 341 std::unique_ptr<app_runtime::ActionData> action_data_;
342 // A list of files and directories to be passed through to the app. 342 // A list of files and directories to be passed through to the app.
343 std::vector<base::FilePath> entry_paths_; 343 std::vector<base::FilePath> entry_paths_;
344 // A corresponding list with EntryInfo for every base::FilePath in 344 // A corresponding list with EntryInfo for every base::FilePath in
345 // entry_paths_. 345 // entry_paths_.
346 std::vector<extensions::EntryInfo> entries_; 346 std::vector<extensions::EntryInfo> entries_;
347 // The ID of the file handler used to launch the app. 347 // The ID of the file handler used to launch the app.
348 std::string handler_id_; 348 std::string handler_id_;
349 extensions::app_file_handler_util::MimeTypeCollector mime_type_collector_; 349 extensions::app_file_handler_util::MimeTypeCollector mime_type_collector_;
350 extensions::app_file_handler_util::IsDirectoryCollector 350 extensions::app_file_handler_util::IsDirectoryCollector
351 is_directory_collector_; 351 is_directory_collector_;
352 352
353 DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher); 353 DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher);
354 }; 354 };
355 355
356 } // namespace 356 } // namespace
357 357
358 void LaunchPlatformAppWithCommandLine(Profile* profile, 358 void LaunchPlatformAppWithCommandLine(content::BrowserContext* context,
359 const extensions::Extension* app, 359 const extensions::Extension* app,
360 const base::CommandLine& command_line, 360 const base::CommandLine& command_line,
361 const base::FilePath& current_directory, 361 const base::FilePath& current_directory,
362 extensions::AppLaunchSource source, 362 extensions::AppLaunchSource source,
363 PlayStoreStatus play_store_status) { 363 PlayStoreStatus play_store_status) {
364 LaunchPlatformAppWithCommandLineAndLaunchId(profile, app, "", command_line, 364 LaunchPlatformAppWithCommandLineAndLaunchId(context, app, "", command_line,
365 current_directory, source, 365 current_directory, source,
366 play_store_status); 366 play_store_status);
367 } 367 }
368 368
369 void LaunchPlatformAppWithCommandLineAndLaunchId( 369 void LaunchPlatformAppWithCommandLineAndLaunchId(
370 Profile* profile, 370 content::BrowserContext* context,
371 const extensions::Extension* app, 371 const extensions::Extension* app,
372 const std::string& launch_id, 372 const std::string& launch_id,
373 const base::CommandLine& command_line, 373 const base::CommandLine& command_line,
374 const base::FilePath& current_directory, 374 const base::FilePath& current_directory,
375 extensions::AppLaunchSource source, 375 extensions::AppLaunchSource source,
376 PlayStoreStatus play_store_status) { 376 PlayStoreStatus play_store_status) {
377 // An app with "kiosk_only" should not be installed and launched 377 // An app with "kiosk_only" should not be installed and launched
378 // outside of ChromeOS kiosk mode in the first place. This is a defensive 378 // outside of ChromeOS kiosk mode in the first place. This is a defensive
379 // check in case this scenario does occur. 379 // check in case this scenario does occur.
380 if (extensions::KioskModeInfo::IsKioskOnly(app)) { 380 if (extensions::KioskModeInfo::IsKioskOnly(app)) {
(...skipping 22 matching lines...) Expand all
403 // has write access will result in a file 'about' being created, which 403 // has write access will result in a file 'about' being created, which
404 // causes problems on the bots. 404 // causes problems on the bots.
405 if (args.empty() || (command_line.HasSwitch(switches::kTestType) && 405 if (args.empty() || (command_line.HasSwitch(switches::kTestType) &&
406 args[0] == about_blank_url)) { 406 args[0] == about_blank_url)) {
407 std::unique_ptr<app_runtime::LaunchData> launch_data = 407 std::unique_ptr<app_runtime::LaunchData> launch_data =
408 base::MakeUnique<app_runtime::LaunchData>(); 408 base::MakeUnique<app_runtime::LaunchData>();
409 if (play_store_status != PlayStoreStatus::PLAY_STORE_STATUS_UNKNOWN) 409 if (play_store_status != PlayStoreStatus::PLAY_STORE_STATUS_UNKNOWN)
410 launch_data->play_store_status = play_store_status; 410 launch_data->play_store_status = play_store_status;
411 if (!launch_id.empty()) 411 if (!launch_id.empty())
412 launch_data->id.reset(new std::string(launch_id)); 412 launch_data->id.reset(new std::string(launch_id));
413 AppRuntimeEventRouter::DispatchOnLaunchedEvent(profile, app, source, 413 AppRuntimeEventRouter::DispatchOnLaunchedEvent(context, app, source,
414 std::move(launch_data)); 414 std::move(launch_data));
415 return; 415 return;
416 } 416 }
417 417
418 base::FilePath file_path(command_line.GetArgs()[0]); 418 base::FilePath file_path(command_line.GetArgs()[0]);
419 scoped_refptr<PlatformAppPathLauncher> launcher = 419 scoped_refptr<PlatformAppPathLauncher> launcher =
420 new PlatformAppPathLauncher(profile, app, file_path); 420 new PlatformAppPathLauncher(context, app, file_path);
421 launcher->LaunchWithRelativePath(current_directory); 421 launcher->LaunchWithRelativePath(current_directory);
422 } 422 }
423 423
424 void LaunchPlatformAppWithPath(Profile* profile, 424 void LaunchPlatformAppWithPath(content::BrowserContext* context,
425 const Extension* app, 425 const Extension* app,
426 const base::FilePath& file_path) { 426 const base::FilePath& file_path) {
427 scoped_refptr<PlatformAppPathLauncher> launcher = 427 scoped_refptr<PlatformAppPathLauncher> launcher =
428 new PlatformAppPathLauncher(profile, app, file_path); 428 new PlatformAppPathLauncher(context, app, file_path);
429 launcher->Launch(); 429 launcher->Launch();
430 } 430 }
431 431
432 void LaunchPlatformAppWithAction( 432 void LaunchPlatformAppWithAction(
433 Profile* profile, 433 content::BrowserContext* context,
434 const extensions::Extension* app, 434 const extensions::Extension* app,
435 std::unique_ptr<app_runtime::ActionData> action_data, 435 std::unique_ptr<app_runtime::ActionData> action_data,
436 const base::FilePath& file_path) { 436 const base::FilePath& file_path) {
437 scoped_refptr<PlatformAppPathLauncher> launcher = 437 scoped_refptr<PlatformAppPathLauncher> launcher =
438 new PlatformAppPathLauncher(profile, app, file_path); 438 new PlatformAppPathLauncher(context, app, file_path);
439 launcher->set_action_data(std::move(action_data)); 439 launcher->set_action_data(std::move(action_data));
440 launcher->set_launch_source(extensions::AppLaunchSource::SOURCE_UNTRACKED); 440 launcher->set_launch_source(extensions::AppLaunchSource::SOURCE_UNTRACKED);
441 launcher->Launch(); 441 launcher->Launch();
442 } 442 }
443 443
444 void LaunchPlatformApp(Profile* profile, 444 void LaunchPlatformApp(content::BrowserContext* context,
445 const Extension* app, 445 const Extension* app,
446 extensions::AppLaunchSource source) { 446 extensions::AppLaunchSource source) {
447 LaunchPlatformAppWithCommandLine( 447 LaunchPlatformAppWithCommandLine(
448 profile, app, base::CommandLine(base::CommandLine::NO_PROGRAM), 448 context, app, base::CommandLine(base::CommandLine::NO_PROGRAM),
449 base::FilePath(), source); 449 base::FilePath(), source);
450 } 450 }
451 451
452 void LaunchPlatformAppWithFileHandler( 452 void LaunchPlatformAppWithFileHandler(
453 Profile* profile, 453 content::BrowserContext* context,
454 const Extension* app, 454 const Extension* app,
455 const std::string& handler_id, 455 const std::string& handler_id,
456 const std::vector<base::FilePath>& entry_paths) { 456 const std::vector<base::FilePath>& entry_paths) {
457 scoped_refptr<PlatformAppPathLauncher> launcher = 457 scoped_refptr<PlatformAppPathLauncher> launcher =
458 new PlatformAppPathLauncher(profile, app, entry_paths); 458 new PlatformAppPathLauncher(context, app, entry_paths);
459 launcher->LaunchWithHandler(handler_id); 459 launcher->LaunchWithHandler(handler_id);
460 } 460 }
461 461
462 void RestartPlatformApp(Profile* profile, const Extension* app) { 462 void RestartPlatformApp(content::BrowserContext* context,
463 EventRouter* event_router = EventRouter::Get(profile); 463 const Extension* app) {
464 EventRouter* event_router = EventRouter::Get(context);
464 bool listening_to_restart = event_router->ExtensionHasEventListener( 465 bool listening_to_restart = event_router->ExtensionHasEventListener(
465 app->id(), app_runtime::OnRestarted::kEventName); 466 app->id(), app_runtime::OnRestarted::kEventName);
466 467
467 if (listening_to_restart) { 468 if (listening_to_restart) {
468 AppRuntimeEventRouter::DispatchOnRestartedEvent(profile, app); 469 AppRuntimeEventRouter::DispatchOnRestartedEvent(context, app);
469 return; 470 return;
470 } 471 }
471 472
472 extensions::ExtensionPrefs* extension_prefs = 473 extensions::ExtensionPrefs* extension_prefs =
473 extensions::ExtensionPrefs::Get(profile); 474 extensions::ExtensionPrefs::Get(context);
474 bool had_windows = extension_prefs->IsActive(app->id()); 475 bool had_windows = extension_prefs->IsActive(app->id());
475 extension_prefs->SetIsActive(app->id(), false); 476 extension_prefs->SetIsActive(app->id(), false);
476 bool listening_to_launch = event_router->ExtensionHasEventListener( 477 bool listening_to_launch = event_router->ExtensionHasEventListener(
477 app->id(), app_runtime::OnLaunched::kEventName); 478 app->id(), app_runtime::OnLaunched::kEventName);
478 479
479 if (listening_to_launch && had_windows) { 480 if (listening_to_launch && had_windows) {
480 AppRuntimeEventRouter::DispatchOnLaunchedEvent( 481 AppRuntimeEventRouter::DispatchOnLaunchedEvent(
481 profile, app, extensions::SOURCE_RESTART, nullptr); 482 context, app, extensions::SOURCE_RESTART, nullptr);
482 } 483 }
483 } 484 }
484 485
485 void LaunchPlatformAppWithUrl(Profile* profile, 486 void LaunchPlatformAppWithUrl(content::BrowserContext* context,
486 const Extension* app, 487 const Extension* app,
487 const std::string& handler_id, 488 const std::string& handler_id,
488 const GURL& url, 489 const GURL& url,
489 const GURL& referrer_url) { 490 const GURL& referrer_url) {
490 AppRuntimeEventRouter::DispatchOnLaunchedEventWithUrl( 491 AppRuntimeEventRouter::DispatchOnLaunchedEventWithUrl(
491 profile, app, handler_id, url, referrer_url); 492 context, app, handler_id, url, referrer_url);
492 } 493 }
493 494
494 } // namespace apps 495 } // namespace apps
OLDNEW
« no previous file with comments | « apps/launcher.h ('k') | apps/saved_files_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698