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