| 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 "chrome/browser/extensions/extension_prefs.h" | 8 #include "chrome/browser/extensions/extension_prefs.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/extensions/extension_system.h" | 10 #include "chrome/browser/extensions/extension_system.h" |
| 11 #include "chrome/browser/extensions/platform_app_launcher.h" | 11 #include "chrome/browser/extensions/platform_app_launcher.h" |
| 12 #include "chrome/browser/extensions/shell_window_registry.h" | 12 #include "chrome/browser/extensions/shell_window_registry.h" |
| 13 #include "chrome/browser/extensions/unpacked_installer.h" |
| 14 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
| 15 #include "chrome/common/extensions/extension_constants.h" | 17 #include "chrome/common/extensions/extension_constants.h" |
| 16 #include "content/public/browser/notification_details.h" | 18 #include "content/public/browser/notification_details.h" |
| 17 #include "content/public/browser/notification_service.h" | 19 #include "content/public/browser/notification_service.h" |
| 18 #include "content/public/browser/notification_types.h" | 20 #include "content/public/browser/notification_types.h" |
| 19 | 21 |
| 20 using extensions::Extension; | 22 using extensions::Extension; |
| 21 using extensions::ExtensionPrefs; | 23 using extensions::ExtensionPrefs; |
| 22 | 24 |
| 23 namespace { | 25 namespace apps { |
| 24 | 26 |
| 25 enum PostReloadAction { | 27 AppLoadService::PostReloadAction::PostReloadAction() |
| 26 RELOAD_ACTION_LAUNCH, | 28 : command_line(CommandLine::NO_PROGRAM) { |
| 27 RELOAD_ACTION_RESTART, | 29 } |
| 28 }; | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 namespace apps { | |
| 33 | 30 |
| 34 AppLoadService::AppLoadService(Profile* profile) | 31 AppLoadService::AppLoadService(Profile* profile) |
| 35 : profile_(profile) { | 32 : profile_(profile) { |
| 36 registrar_.Add( | 33 registrar_.Add( |
| 37 this, chrome::NOTIFICATION_EXTENSION_LOADED, | 34 this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 38 content::NotificationService::AllSources()); | 35 content::NotificationService::AllSources()); |
| 39 registrar_.Add( | 36 registrar_.Add( |
| 40 this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 37 this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 41 content::NotificationService::AllSources()); | 38 content::NotificationService::AllSources()); |
| 42 } | 39 } |
| 43 | 40 |
| 44 AppLoadService::~AppLoadService() {} | 41 AppLoadService::~AppLoadService() {} |
| 45 | 42 |
| 46 void AppLoadService::RestartApplication(const std::string& extension_id) { | 43 void AppLoadService::RestartApplication(const std::string& extension_id) { |
| 47 reload_actions_[extension_id] = RELOAD_ACTION_RESTART; | 44 reload_actions_[extension_id].action_type = RELOAD_RESTART; |
| 48 ExtensionService* service = extensions::ExtensionSystem::Get(profile_)-> | 45 ExtensionService* service = extensions::ExtensionSystem::Get(profile_)-> |
| 49 extension_service(); | 46 extension_service(); |
| 50 DCHECK(service); | 47 DCHECK(service); |
| 51 service->ReloadExtension(extension_id); | 48 service->ReloadExtension(extension_id); |
| 52 } | 49 } |
| 53 | 50 |
| 54 void AppLoadService::ScheduleLaunchOnLoad(const std::string& extension_id) { | 51 bool AppLoadService::LoadAndLaunch(const base::FilePath& extension_path, |
| 55 reload_actions_[extension_id] = RELOAD_ACTION_LAUNCH; | 52 const CommandLine& command_line, |
| 53 const base::FilePath& current_dir) { |
| 54 std::string extension_id; |
| 55 if (!extensions::UnpackedInstaller::Create(profile_->GetExtensionService())-> |
| 56 LoadFromCommandLine(base::FilePath(extension_path), &extension_id)) { |
| 57 return false; |
| 58 } |
| 59 |
| 60 // Schedule the app to be launched once loaded. |
| 61 PostReloadAction& action = reload_actions_[extension_id]; |
| 62 action.action_type = RELOAD_LAUNCH_WITH_COMMAND_LINE; |
| 63 action.command_line = command_line; |
| 64 action.current_dir = current_dir; |
| 65 return true; |
| 56 } | 66 } |
| 57 | 67 |
| 58 // static | 68 // static |
| 59 AppLoadService* AppLoadService::Get(Profile* profile) { | 69 AppLoadService* AppLoadService::Get(Profile* profile) { |
| 60 return apps::AppLoadServiceFactory::GetForProfile(profile); | 70 return apps::AppLoadServiceFactory::GetForProfile(profile); |
| 61 } | 71 } |
| 62 | 72 |
| 63 void AppLoadService::Observe(int type, | 73 void AppLoadService::Observe(int type, |
| 64 const content::NotificationSource& source, | 74 const content::NotificationSource& source, |
| 65 const content::NotificationDetails& details) { | 75 const content::NotificationDetails& details) { |
| 66 switch (type) { | 76 switch (type) { |
| 67 case chrome::NOTIFICATION_EXTENSION_LOADED: { | 77 case chrome::NOTIFICATION_EXTENSION_LOADED: { |
| 68 const Extension* extension = content::Details<Extension>(details).ptr(); | 78 const Extension* extension = content::Details<Extension>(details).ptr(); |
| 69 std::map<std::string, int>::iterator it = reload_actions_.find( | 79 std::map<std::string, PostReloadAction>::iterator it = |
| 70 extension->id()); | 80 reload_actions_.find(extension->id()); |
| 71 if (it == reload_actions_.end()) | 81 if (it == reload_actions_.end()) |
| 72 break; | 82 break; |
| 73 | 83 |
| 74 switch (it->second) { | 84 switch (it->second.action_type) { |
| 75 case RELOAD_ACTION_LAUNCH: | 85 case RELOAD_LAUNCH: |
| 76 extensions::LaunchPlatformApp(profile_, extension); | 86 extensions::LaunchPlatformApp(profile_, extension); |
| 77 break; | 87 break; |
| 78 case RELOAD_ACTION_RESTART: | 88 case RELOAD_RESTART: |
| 79 extensions::RestartPlatformApp(profile_, extension); | 89 extensions::RestartPlatformApp(profile_, extension); |
| 80 break; | 90 break; |
| 91 case RELOAD_LAUNCH_WITH_COMMAND_LINE: |
| 92 extensions::LaunchPlatformAppWithCommandLine( |
| 93 profile_, extension, &it->second.command_line, |
| 94 it->second.current_dir); |
| 95 break; |
| 81 default: | 96 default: |
| 82 NOTREACHED(); | 97 NOTREACHED(); |
| 83 } | 98 } |
| 84 | 99 |
| 85 reload_actions_.erase(it); | 100 reload_actions_.erase(it); |
| 86 break; | 101 break; |
| 87 } | 102 } |
| 88 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { | 103 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { |
| 89 const extensions::UnloadedExtensionInfo* unload_info = | 104 const extensions::UnloadedExtensionInfo* unload_info = |
| 90 content::Details<extensions::UnloadedExtensionInfo>(details).ptr(); | 105 content::Details<extensions::UnloadedExtensionInfo>(details).ptr(); |
| 91 if (!unload_info->extension->is_platform_app()) | 106 if (!unload_info->extension->is_platform_app()) |
| 92 break; | 107 break; |
| 93 | 108 |
| 94 if (unload_info->reason == extension_misc::UNLOAD_REASON_DISABLE) { | 109 if (unload_info->reason == extension_misc::UNLOAD_REASON_DISABLE) { |
| 95 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); | 110 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); |
| 96 if ((prefs->GetDisableReasons(unload_info->extension->id()) & | 111 if ((prefs->GetDisableReasons(unload_info->extension->id()) & |
| 97 Extension::DISABLE_RELOAD) && | 112 Extension::DISABLE_RELOAD) && |
| 98 !extensions::ShellWindowRegistry::Get(profile_)-> | 113 !extensions::ShellWindowRegistry::Get(profile_)-> |
| 99 GetShellWindowsForApp(unload_info->extension->id()).empty()) { | 114 GetShellWindowsForApp(unload_info->extension->id()).empty()) { |
| 100 reload_actions_[unload_info->extension->id()] = RELOAD_ACTION_LAUNCH; | 115 reload_actions_[unload_info->extension->id()].action_type = |
| 116 RELOAD_LAUNCH; |
| 101 } | 117 } |
| 102 } | 118 } |
| 103 break; | 119 break; |
| 104 } | 120 } |
| 105 default: | 121 default: |
| 106 NOTREACHED(); | 122 NOTREACHED(); |
| 107 } | 123 } |
| 108 } | 124 } |
| 109 | 125 |
| 110 } // namespace apps | 126 } // namespace apps |
| OLD | NEW |