[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 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 |
| 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] | 982ba2cf | 2014-07-24 13:26:30 | [diff] [blame] | 8 | #include "apps/app_restore_service.h" |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 9 | #include "apps/launcher.h" |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 10 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 11 | #include "chrome/browser/extensions/unpacked_installer.h" |
| 12 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 13 | #include "content/public/browser/notification_details.h" |
| 14 | #include "content/public/browser/notification_service.h" |
| 15 | #include "content/public/browser/notification_types.h" |
hashimoto | ad3c687 | 2014-08-29 09:46:57 | [diff] [blame] | 16 | #include "extensions/browser/app_window/app_window_registry.h" |
[email protected] | 22401dc | 2014-03-21 01:38:57 | [diff] [blame] | 17 | #include "extensions/browser/extension_host.h" |
[email protected] | 489db084 | 2014-01-22 18:20:03 | [diff] [blame] | 18 | #include "extensions/browser/extension_prefs.h" |
limasdf | d3791758 | 2014-09-17 00:21:43 | [diff] [blame] | 19 | #include "extensions/browser/extension_registry.h" |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 20 | #include "extensions/browser/extension_system.h" |
[email protected] | adf5a10 | 2014-07-31 12:44:06 | [diff] [blame] | 21 | #include "extensions/browser/notification_types.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 22 | #include "extensions/common/extension.h" |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 23 | |
| 24 | using extensions::Extension; |
| 25 | using extensions::ExtensionPrefs; |
[email protected] | 1f56ac15 | 2013-12-04 06:06:06 | [diff] [blame] | 26 | using extensions::ExtensionSystem; |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 27 | |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 28 | namespace apps { |
| 29 | |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 30 | AppLoadService::PostReloadAction::PostReloadAction() |
cylee | 988a9bb5 | 2014-11-04 16:39:16 | [diff] [blame] | 31 | : action_type(LAUNCH_FOR_RELOAD), |
pgal.u-szeged | 214274b | 2014-10-28 11:59:48 | [diff] [blame] | 32 | command_line(base::CommandLine::NO_PROGRAM) { |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 33 | } |
| 34 | |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 35 | AppLoadService::AppLoadService(Profile* profile) |
| 36 | : profile_(profile) { |
[email protected] | adf5a10 | 2014-07-31 12:44:06 | [diff] [blame] | 37 | registrar_.Add(this, |
kalman | fd474fa | 2015-03-16 22:30:57 | [diff] [blame] | 38 | extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD, |
[email protected] | adf5a10 | 2014-07-31 12:44:06 | [diff] [blame] | 39 | content::NotificationService::AllSources()); |
limasdf | d3791758 | 2014-09-17 00:21:43 | [diff] [blame] | 40 | extensions::ExtensionRegistry::Get(profile_)->AddObserver(this); |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 41 | } |
| 42 | |
limasdf | d3791758 | 2014-09-17 00:21:43 | [diff] [blame] | 43 | AppLoadService::~AppLoadService() { |
| 44 | extensions::ExtensionRegistry::Get(profile_)->RemoveObserver(this); |
| 45 | } |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 46 | |
| 47 | void AppLoadService::RestartApplication(const std::string& extension_id) { |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 48 | post_reload_actions_[extension_id].action_type = RESTART; |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 49 | ExtensionService* service = extensions::ExtensionSystem::Get(profile_)-> |
| 50 | extension_service(); |
| 51 | DCHECK(service); |
| 52 | service->ReloadExtension(extension_id); |
| 53 | } |
| 54 | |
[email protected] | 982ba2cf | 2014-07-24 13:26:30 | [diff] [blame] | 55 | void 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] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 61 | bool AppLoadService::LoadAndLaunch(const base::FilePath& extension_path, |
pgal.u-szeged | 214274b | 2014-10-28 11:59:48 | [diff] [blame] | 62 | const base::CommandLine& command_line, |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 63 | const base::FilePath& current_dir) { |
[email protected] | 1f56ac15 | 2013-12-04 06:06:06 | [diff] [blame] | 64 | ExtensionService* extension_service = |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 65 | ExtensionSystem::Get(profile_)->extension_service(); |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 66 | std::string extension_id; |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 67 | if (!extensions::UnpackedInstaller::Create(extension_service) |
| 68 | ->LoadFromCommandLine(base::FilePath(extension_path), &extension_id, |
| 69 | true /* only_allow_apps */)) { |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 70 | return false; |
| 71 | } |
| 72 | |
| 73 | // Schedule the app to be launched once loaded. |
| 74 | PostReloadAction& action = post_reload_actions_[extension_id]; |
cylee | 988a9bb5 | 2014-11-04 16:39:16 | [diff] [blame] | 75 | action.action_type = LAUNCH_FOR_LOAD_AND_LAUNCH; |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 76 | action.command_line = command_line; |
| 77 | action.current_dir = current_dir; |
| 78 | return true; |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 79 | } |
| 80 | |
zork | 3ea6ffc5 | 2014-11-25 20:31:58 | [diff] [blame] | 81 | bool AppLoadService::Load(const base::FilePath& extension_path) { |
| 82 | ExtensionService* extension_service = |
| 83 | ExtensionSystem::Get(profile_)->extension_service(); |
| 84 | std::string extension_id; |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 85 | return extensions::UnpackedInstaller::Create(extension_service) |
| 86 | ->LoadFromCommandLine(base::FilePath(extension_path), &extension_id, |
| 87 | true /* only_allow_apps */); |
zork | 3ea6ffc5 | 2014-11-25 20:31:58 | [diff] [blame] | 88 | } |
| 89 | |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 90 | // static |
| 91 | AppLoadService* AppLoadService::Get(Profile* profile) { |
| 92 | return apps::AppLoadServiceFactory::GetForProfile(profile); |
| 93 | } |
| 94 | |
| 95 | void AppLoadService::Observe(int type, |
| 96 | const content::NotificationSource& source, |
| 97 | const content::NotificationDetails& details) { |
kalman | fd474fa | 2015-03-16 22:30:57 | [diff] [blame] | 98 | DCHECK_EQ(type, extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD); |
limasdf | d3791758 | 2014-09-17 00:21:43 | [diff] [blame] | 99 | extensions::ExtensionHost* host = |
| 100 | content::Details<extensions::ExtensionHost>(details).ptr(); |
| 101 | const Extension* extension = host->extension(); |
| 102 | // It is possible for an extension to be unloaded before it stops loading. |
| 103 | if (!extension) |
| 104 | return; |
| 105 | std::map<std::string, PostReloadAction>::iterator it = |
| 106 | post_reload_actions_.find(extension->id()); |
| 107 | if (it == post_reload_actions_.end()) |
| 108 | return; |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 109 | |
limasdf | d3791758 | 2014-09-17 00:21:43 | [diff] [blame] | 110 | switch (it->second.action_type) { |
cylee | 988a9bb5 | 2014-11-04 16:39:16 | [diff] [blame] | 111 | case LAUNCH_FOR_RELOAD: |
| 112 | LaunchPlatformApp(profile_, extension, extensions::SOURCE_RELOAD); |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 113 | break; |
limasdf | d3791758 | 2014-09-17 00:21:43 | [diff] [blame] | 114 | case RESTART: |
| 115 | RestartPlatformApp(profile_, extension); |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 116 | break; |
cylee | 988a9bb5 | 2014-11-04 16:39:16 | [diff] [blame] | 117 | case LAUNCH_FOR_LOAD_AND_LAUNCH: |
| 118 | LaunchPlatformAppWithCommandLine(profile_, |
| 119 | extension, |
| 120 | it->second.command_line, |
| 121 | it->second.current_dir, |
| 122 | extensions::SOURCE_LOAD_AND_LAUNCH); |
limasdf | d3791758 | 2014-09-17 00:21:43 | [diff] [blame] | 123 | break; |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 124 | default: |
| 125 | NOTREACHED(); |
| 126 | } |
limasdf | d3791758 | 2014-09-17 00:21:43 | [diff] [blame] | 127 | |
| 128 | post_reload_actions_.erase(it); |
| 129 | } |
| 130 | |
| 131 | void AppLoadService::OnExtensionUnloaded( |
| 132 | content::BrowserContext* browser_context, |
| 133 | const Extension* extension, |
| 134 | extensions::UnloadedExtensionInfo::Reason reason) { |
| 135 | if (!extension->is_platform_app()) |
| 136 | return; |
| 137 | |
| 138 | extensions::ExtensionPrefs* extension_prefs = |
| 139 | extensions::ExtensionPrefs::Get(browser_context); |
| 140 | if (WasUnloadedForReload(extension->id(), reason) && |
| 141 | extension_prefs->IsActive(extension->id()) && |
| 142 | !HasPostReloadAction(extension->id())) { |
cylee | 988a9bb5 | 2014-11-04 16:39:16 | [diff] [blame] | 143 | post_reload_actions_[extension->id()].action_type = LAUNCH_FOR_RELOAD; |
limasdf | d3791758 | 2014-09-17 00:21:43 | [diff] [blame] | 144 | } |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | 8002cab | 2013-07-10 09:36:42 | [diff] [blame] | 147 | bool AppLoadService::WasUnloadedForReload( |
limasdf | d3791758 | 2014-09-17 00:21:43 | [diff] [blame] | 148 | const extensions::ExtensionId& extension_id, |
| 149 | const extensions::UnloadedExtensionInfo::Reason reason) { |
| 150 | if (reason == extensions::UnloadedExtensionInfo::REASON_DISABLE) { |
[email protected] | 8002cab | 2013-07-10 09:36:42 | [diff] [blame] | 151 | ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); |
limasdf | d3791758 | 2014-09-17 00:21:43 | [diff] [blame] | 152 | return (prefs->GetDisableReasons(extension_id) & |
| 153 | Extension::DISABLE_RELOAD) != 0; |
[email protected] | 8002cab | 2013-07-10 09:36:42 | [diff] [blame] | 154 | } |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | bool AppLoadService::HasPostReloadAction(const std::string& extension_id) { |
| 159 | return post_reload_actions_.find(extension_id) != post_reload_actions_.end(); |
| 160 | } |
| 161 | |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 162 | } // namespace apps |