[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] | dbb03fb | 2014-02-15 05:36:33 | [diff] [blame] | 9 | #include "apps/app_window_registry.h" |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 10 | #include "apps/launcher.h" |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 11 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 12 | #include "chrome/browser/extensions/unpacked_installer.h" |
| 13 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 14 | #include "content/public/browser/notification_details.h" |
| 15 | #include "content/public/browser/notification_service.h" |
| 16 | #include "content/public/browser/notification_types.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" |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 19 | #include "extensions/browser/extension_system.h" |
[email protected] | adf5a10 | 2014-07-31 12:44:06 | [diff] [blame^] | 20 | #include "extensions/browser/notification_types.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 21 | #include "extensions/common/extension.h" |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 22 | |
| 23 | using extensions::Extension; |
| 24 | using extensions::ExtensionPrefs; |
[email protected] | 1f56ac15 | 2013-12-04 06:06:06 | [diff] [blame] | 25 | using extensions::ExtensionSystem; |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 26 | |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 27 | namespace apps { |
| 28 | |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 29 | AppLoadService::PostReloadAction::PostReloadAction() |
[email protected] | 6231684 | 2013-10-23 02:40:13 | [diff] [blame] | 30 | : action_type(LAUNCH), |
| 31 | command_line(CommandLine::NO_PROGRAM) { |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 32 | } |
| 33 | |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 34 | AppLoadService::AppLoadService(Profile* profile) |
| 35 | : profile_(profile) { |
[email protected] | adf5a10 | 2014-07-31 12:44:06 | [diff] [blame^] | 36 | registrar_.Add(this, |
| 37 | extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, |
| 38 | content::NotificationService::AllSources()); |
| 39 | registrar_.Add(this, |
| 40 | extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, |
| 41 | content::NotificationService::AllSources()); |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | AppLoadService::~AppLoadService() {} |
| 45 | |
| 46 | void AppLoadService::RestartApplication(const std::string& extension_id) { |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 47 | post_reload_actions_[extension_id].action_type = RESTART; |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 48 | ExtensionService* service = extensions::ExtensionSystem::Get(profile_)-> |
| 49 | extension_service(); |
| 50 | DCHECK(service); |
| 51 | service->ReloadExtension(extension_id); |
| 52 | } |
| 53 | |
[email protected] | 982ba2cf | 2014-07-24 13:26:30 | [diff] [blame] | 54 | void AppLoadService::RestartApplicationIfRunning( |
| 55 | const std::string& extension_id) { |
| 56 | if (apps::AppRestoreService::Get(profile_)->IsAppRestorable(extension_id)) |
| 57 | RestartApplication(extension_id); |
| 58 | } |
| 59 | |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 60 | bool AppLoadService::LoadAndLaunch(const base::FilePath& extension_path, |
| 61 | const CommandLine& command_line, |
| 62 | const base::FilePath& current_dir) { |
[email protected] | 1f56ac15 | 2013-12-04 06:06:06 | [diff] [blame] | 63 | ExtensionService* extension_service = |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 64 | ExtensionSystem::Get(profile_)->extension_service(); |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 65 | std::string extension_id; |
[email protected] | 1f56ac15 | 2013-12-04 06:06:06 | [diff] [blame] | 66 | if (!extensions::UnpackedInstaller::Create(extension_service)-> |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 67 | LoadFromCommandLine(base::FilePath(extension_path), &extension_id)) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | // Schedule the app to be launched once loaded. |
| 72 | PostReloadAction& action = post_reload_actions_[extension_id]; |
| 73 | action.action_type = LAUNCH_WITH_COMMAND_LINE; |
| 74 | action.command_line = command_line; |
| 75 | action.current_dir = current_dir; |
| 76 | return true; |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // static |
| 80 | AppLoadService* AppLoadService::Get(Profile* profile) { |
| 81 | return apps::AppLoadServiceFactory::GetForProfile(profile); |
| 82 | } |
| 83 | |
| 84 | void AppLoadService::Observe(int type, |
| 85 | const content::NotificationSource& source, |
| 86 | const content::NotificationDetails& details) { |
| 87 | switch (type) { |
[email protected] | adf5a10 | 2014-07-31 12:44:06 | [diff] [blame^] | 88 | case extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: { |
[email protected] | c7c0c92 | 2014-01-09 00:03:36 | [diff] [blame] | 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 | break; |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 95 | std::map<std::string, PostReloadAction>::iterator it = |
| 96 | post_reload_actions_.find(extension->id()); |
| 97 | if (it == post_reload_actions_.end()) |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 98 | break; |
| 99 | |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 100 | switch (it->second.action_type) { |
| 101 | case LAUNCH: |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 102 | LaunchPlatformApp(profile_, extension); |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 103 | break; |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 104 | case RESTART: |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 105 | RestartPlatformApp(profile_, extension); |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 106 | break; |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 107 | case LAUNCH_WITH_COMMAND_LINE: |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 108 | LaunchPlatformAppWithCommandLine( |
[email protected] | 7d501ce | 2013-12-17 03:49:12 | [diff] [blame] | 109 | profile_, extension, it->second.command_line, |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 110 | it->second.current_dir); |
| 111 | break; |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 112 | default: |
| 113 | NOTREACHED(); |
| 114 | } |
| 115 | |
[email protected] | 7b9faeb7 | 2013-06-11 12:20:17 | [diff] [blame] | 116 | post_reload_actions_.erase(it); |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 117 | break; |
| 118 | } |
[email protected] | adf5a10 | 2014-07-31 12:44:06 | [diff] [blame^] | 119 | case extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 120 | const extensions::UnloadedExtensionInfo* unload_info = |
| 121 | content::Details<extensions::UnloadedExtensionInfo>(details).ptr(); |
| 122 | if (!unload_info->extension->is_platform_app()) |
| 123 | break; |
| 124 | |
[email protected] | 7aadf69a4 | 2014-05-15 07:15:50 | [diff] [blame] | 125 | extensions::ExtensionPrefs* extension_prefs = |
| 126 | extensions::ExtensionPrefs::Get(profile_); |
[email protected] | 8002cab | 2013-07-10 09:36:42 | [diff] [blame] | 127 | if (WasUnloadedForReload(*unload_info) && |
[email protected] | 7aadf69a4 | 2014-05-15 07:15:50 | [diff] [blame] | 128 | extension_prefs->IsActive(unload_info->extension->id()) && |
[email protected] | 8002cab | 2013-07-10 09:36:42 | [diff] [blame] | 129 | !HasPostReloadAction(unload_info->extension->id())) { |
| 130 | post_reload_actions_[unload_info->extension->id()].action_type = LAUNCH; |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 131 | } |
| 132 | break; |
| 133 | } |
| 134 | default: |
| 135 | NOTREACHED(); |
| 136 | } |
| 137 | } |
| 138 | |
[email protected] | 8002cab | 2013-07-10 09:36:42 | [diff] [blame] | 139 | bool AppLoadService::WasUnloadedForReload( |
| 140 | const extensions::UnloadedExtensionInfo& unload_info) { |
[email protected] | b0af479 | 2013-10-23 09:12:13 | [diff] [blame] | 141 | if (unload_info.reason == extensions::UnloadedExtensionInfo::REASON_DISABLE) { |
[email protected] | 8002cab | 2013-07-10 09:36:42 | [diff] [blame] | 142 | ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); |
| 143 | return (prefs->GetDisableReasons(unload_info.extension->id()) & |
| 144 | Extension::DISABLE_RELOAD) != 0; |
| 145 | } |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | bool AppLoadService::HasPostReloadAction(const std::string& extension_id) { |
| 150 | return post_reload_actions_.find(extension_id) != post_reload_actions_.end(); |
| 151 | } |
| 152 | |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 153 | } // namespace apps |