blob: fe224c5e8ebf7b64a0d2a419ca001be383bc6eaf [file] [log] [blame]
[email protected]2a69b942013-05-31 09:37:531// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "apps/app_load_service.h"
6
7#include "apps/app_load_service_factory.h"
[email protected]982ba2cf2014-07-24 13:26:308#include "apps/app_restore_service.h"
[email protected]24c81d692013-08-07 14:09:489#include "apps/launcher.h"
[email protected]2a69b942013-05-31 09:37:5310#include "chrome/browser/extensions/extension_service.h"
[email protected]7b9faeb72013-06-11 12:20:1711#include "chrome/browser/extensions/unpacked_installer.h"
12#include "chrome/browser/profiles/profile.h"
[email protected]2a69b942013-05-31 09:37:5313#include "content/public/browser/notification_details.h"
14#include "content/public/browser/notification_service.h"
15#include "content/public/browser/notification_types.h"
hashimotoad3c6872014-08-29 09:46:5716#include "extensions/browser/app_window/app_window_registry.h"
[email protected]22401dc2014-03-21 01:38:5717#include "extensions/browser/extension_host.h"
[email protected]489db0842014-01-22 18:20:0318#include "extensions/browser/extension_prefs.h"
limasdfd37917582014-09-17 00:21:4319#include "extensions/browser/extension_registry.h"
[email protected]59b0e602014-01-30 00:41:2420#include "extensions/browser/extension_system.h"
[email protected]adf5a102014-07-31 12:44:0621#include "extensions/browser/notification_types.h"
[email protected]e4452d32013-11-15 23:07:4122#include "extensions/common/extension.h"
[email protected]2a69b942013-05-31 09:37:5323
24using extensions::Extension;
25using extensions::ExtensionPrefs;
[email protected]1f56ac152013-12-04 06:06:0626using extensions::ExtensionSystem;
[email protected]2a69b942013-05-31 09:37:5327
[email protected]2a69b942013-05-31 09:37:5328namespace apps {
29
[email protected]7b9faeb72013-06-11 12:20:1730AppLoadService::PostReloadAction::PostReloadAction()
[email protected]62316842013-10-23 02:40:1331 : action_type(LAUNCH),
32 command_line(CommandLine::NO_PROGRAM) {
[email protected]7b9faeb72013-06-11 12:20:1733}
34
[email protected]2a69b942013-05-31 09:37:5335AppLoadService::AppLoadService(Profile* profile)
36 : profile_(profile) {
[email protected]adf5a102014-07-31 12:44:0637 registrar_.Add(this,
38 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
39 content::NotificationService::AllSources());
limasdfd37917582014-09-17 00:21:4340 extensions::ExtensionRegistry::Get(profile_)->AddObserver(this);
[email protected]2a69b942013-05-31 09:37:5341}
42
limasdfd37917582014-09-17 00:21:4343AppLoadService::~AppLoadService() {
44 extensions::ExtensionRegistry::Get(profile_)->RemoveObserver(this);
45}
[email protected]2a69b942013-05-31 09:37:5346
47void AppLoadService::RestartApplication(const std::string& extension_id) {
[email protected]7b9faeb72013-06-11 12:20:1748 post_reload_actions_[extension_id].action_type = RESTART;
[email protected]2a69b942013-05-31 09:37:5349 ExtensionService* service = extensions::ExtensionSystem::Get(profile_)->
50 extension_service();
51 DCHECK(service);
52 service->ReloadExtension(extension_id);
53}
54
[email protected]982ba2cf2014-07-24 13:26:3055void AppLoadService::RestartApplicationIfRunning(
56 const std::string& extension_id) {
57 if (apps::AppRestoreService::Get(profile_)->IsAppRestorable(extension_id))
58 RestartApplication(extension_id);
59}
60
[email protected]7b9faeb72013-06-11 12:20:1761bool AppLoadService::LoadAndLaunch(const base::FilePath& extension_path,
62 const CommandLine& command_line,
63 const base::FilePath& current_dir) {
[email protected]1f56ac152013-12-04 06:06:0664 ExtensionService* extension_service =
[email protected]59b0e602014-01-30 00:41:2465 ExtensionSystem::Get(profile_)->extension_service();
[email protected]7b9faeb72013-06-11 12:20:1766 std::string extension_id;
[email protected]1f56ac152013-12-04 06:06:0667 if (!extensions::UnpackedInstaller::Create(extension_service)->
[email protected]7b9faeb72013-06-11 12:20:1768 LoadFromCommandLine(base::FilePath(extension_path), &extension_id)) {
69 return false;
70 }
71
72 // Schedule the app to be launched once loaded.
73 PostReloadAction& action = post_reload_actions_[extension_id];
74 action.action_type = LAUNCH_WITH_COMMAND_LINE;
75 action.command_line = command_line;
76 action.current_dir = current_dir;
77 return true;
[email protected]2a69b942013-05-31 09:37:5378}
79
80// static
81AppLoadService* AppLoadService::Get(Profile* profile) {
82 return apps::AppLoadServiceFactory::GetForProfile(profile);
83}
84
85void AppLoadService::Observe(int type,
86 const content::NotificationSource& source,
87 const content::NotificationDetails& details) {
limasdfd37917582014-09-17 00:21:4388 DCHECK_EQ(type, extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING);
89 extensions::ExtensionHost* host =
90 content::Details<extensions::ExtensionHost>(details).ptr();
91 const Extension* extension = host->extension();
92 // It is possible for an extension to be unloaded before it stops loading.
93 if (!extension)
94 return;
95 std::map<std::string, PostReloadAction>::iterator it =
96 post_reload_actions_.find(extension->id());
97 if (it == post_reload_actions_.end())
98 return;
[email protected]2a69b942013-05-31 09:37:5399
limasdfd37917582014-09-17 00:21:43100 switch (it->second.action_type) {
101 case LAUNCH:
102 LaunchPlatformApp(profile_, extension);
[email protected]2a69b942013-05-31 09:37:53103 break;
limasdfd37917582014-09-17 00:21:43104 case RESTART:
105 RestartPlatformApp(profile_, extension);
[email protected]2a69b942013-05-31 09:37:53106 break;
limasdfd37917582014-09-17 00:21:43107 case LAUNCH_WITH_COMMAND_LINE:
108 LaunchPlatformAppWithCommandLine(
109 profile_, extension, it->second.command_line, it->second.current_dir);
110 break;
[email protected]2a69b942013-05-31 09:37:53111 default:
112 NOTREACHED();
113 }
limasdfd37917582014-09-17 00:21:43114
115 post_reload_actions_.erase(it);
116}
117
118void AppLoadService::OnExtensionUnloaded(
119 content::BrowserContext* browser_context,
120 const Extension* extension,
121 extensions::UnloadedExtensionInfo::Reason reason) {
122 if (!extension->is_platform_app())
123 return;
124
125 extensions::ExtensionPrefs* extension_prefs =
126 extensions::ExtensionPrefs::Get(browser_context);
127 if (WasUnloadedForReload(extension->id(), reason) &&
128 extension_prefs->IsActive(extension->id()) &&
129 !HasPostReloadAction(extension->id())) {
130 post_reload_actions_[extension->id()].action_type = LAUNCH;
131 }
[email protected]2a69b942013-05-31 09:37:53132}
133
[email protected]8002cab2013-07-10 09:36:42134bool AppLoadService::WasUnloadedForReload(
limasdfd37917582014-09-17 00:21:43135 const extensions::ExtensionId& extension_id,
136 const extensions::UnloadedExtensionInfo::Reason reason) {
137 if (reason == extensions::UnloadedExtensionInfo::REASON_DISABLE) {
[email protected]8002cab2013-07-10 09:36:42138 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_);
limasdfd37917582014-09-17 00:21:43139 return (prefs->GetDisableReasons(extension_id) &
140 Extension::DISABLE_RELOAD) != 0;
[email protected]8002cab2013-07-10 09:36:42141 }
142 return false;
143}
144
145bool AppLoadService::HasPostReloadAction(const std::string& extension_id) {
146 return post_reload_actions_.find(extension_id) != post_reload_actions_.end();
147}
148
[email protected]2a69b942013-05-31 09:37:53149} // namespace apps