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