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