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 "chrome/browser/profiles/profile.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_system.h" | 20 #include "extensions/browser/extension_system.h" |
20 #include "extensions/browser/notification_types.h" | 21 #include "extensions/browser/notification_types.h" |
21 #include "extensions/common/extension.h" | 22 #include "extensions/common/extension.h" |
22 | 23 |
23 using extensions::Extension; | 24 using extensions::Extension; |
24 using extensions::ExtensionPrefs; | 25 using extensions::ExtensionPrefs; |
25 using extensions::ExtensionSystem; | 26 using extensions::ExtensionSystem; |
26 | 27 |
27 namespace apps { | 28 namespace apps { |
28 | 29 |
29 AppLoadService::PostReloadAction::PostReloadAction() | 30 AppLoadService::PostReloadAction::PostReloadAction() |
30 : action_type(LAUNCH), | 31 : action_type(LAUNCH), |
31 command_line(CommandLine::NO_PROGRAM) { | 32 command_line(CommandLine::NO_PROGRAM) { |
32 } | 33 } |
33 | 34 |
34 AppLoadService::AppLoadService(Profile* profile) | 35 AppLoadService::AppLoadService(Profile* profile) |
35 : profile_(profile) { | 36 : profile_(profile) { |
36 registrar_.Add(this, | 37 registrar_.Add(this, |
37 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, | 38 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, |
38 content::NotificationService::AllSources()); | 39 content::NotificationService::AllSources()); |
39 registrar_.Add(this, | 40 extensions::ExtensionRegistry::Get(profile_)->AddObserver(this); |
40 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | |
41 content::NotificationService::AllSources()); | |
42 } | 41 } |
43 | 42 |
44 AppLoadService::~AppLoadService() {} | 43 AppLoadService::~AppLoadService() { |
| 44 extensions::ExtensionRegistry::Get(profile_)->RemoveObserver(this); |
| 45 } |
45 | 46 |
46 void AppLoadService::RestartApplication(const std::string& extension_id) { | 47 void AppLoadService::RestartApplication(const std::string& extension_id) { |
47 post_reload_actions_[extension_id].action_type = RESTART; | 48 post_reload_actions_[extension_id].action_type = RESTART; |
48 ExtensionService* service = extensions::ExtensionSystem::Get(profile_)-> | 49 ExtensionService* service = extensions::ExtensionSystem::Get(profile_)-> |
49 extension_service(); | 50 extension_service(); |
50 DCHECK(service); | 51 DCHECK(service); |
51 service->ReloadExtension(extension_id); | 52 service->ReloadExtension(extension_id); |
52 } | 53 } |
53 | 54 |
54 void AppLoadService::RestartApplicationIfRunning( | 55 void AppLoadService::RestartApplicationIfRunning( |
(...skipping 22 matching lines...) Expand all Loading... |
77 } | 78 } |
78 | 79 |
79 // static | 80 // static |
80 AppLoadService* AppLoadService::Get(Profile* profile) { | 81 AppLoadService* AppLoadService::Get(Profile* profile) { |
81 return apps::AppLoadServiceFactory::GetForProfile(profile); | 82 return apps::AppLoadServiceFactory::GetForProfile(profile); |
82 } | 83 } |
83 | 84 |
84 void AppLoadService::Observe(int type, | 85 void AppLoadService::Observe(int type, |
85 const content::NotificationSource& source, | 86 const content::NotificationSource& source, |
86 const content::NotificationDetails& details) { | 87 const content::NotificationDetails& details) { |
87 switch (type) { | 88 DCHECK_EQ(type, extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING); |
88 case extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: { | 89 extensions::ExtensionHost* host = |
89 extensions::ExtensionHost* host = | 90 content::Details<extensions::ExtensionHost>(details).ptr(); |
90 content::Details<extensions::ExtensionHost>(details).ptr(); | 91 const Extension* extension = host->extension(); |
91 const Extension* extension = host->extension(); | 92 // It is possible for an extension to be unloaded before it stops loading. |
92 // It is possible for an extension to be unloaded before it stops loading. | 93 if (!extension) |
93 if (!extension) | 94 return; |
94 break; | 95 std::map<std::string, PostReloadAction>::iterator it = |
95 std::map<std::string, PostReloadAction>::iterator it = | 96 post_reload_actions_.find(extension->id()); |
96 post_reload_actions_.find(extension->id()); | 97 if (it == post_reload_actions_.end()) |
97 if (it == post_reload_actions_.end()) | 98 return; |
98 break; | |
99 | 99 |
100 switch (it->second.action_type) { | 100 switch (it->second.action_type) { |
101 case LAUNCH: | 101 case LAUNCH: |
102 LaunchPlatformApp(profile_, extension); | 102 LaunchPlatformApp(profile_, extension); |
103 break; | |
104 case RESTART: | |
105 RestartPlatformApp(profile_, extension); | |
106 break; | |
107 case LAUNCH_WITH_COMMAND_LINE: | |
108 LaunchPlatformAppWithCommandLine( | |
109 profile_, extension, it->second.command_line, | |
110 it->second.current_dir); | |
111 break; | |
112 default: | |
113 NOTREACHED(); | |
114 } | |
115 | |
116 post_reload_actions_.erase(it); | |
117 break; | 103 break; |
118 } | 104 case RESTART: |
119 case extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { | 105 RestartPlatformApp(profile_, extension); |
120 const extensions::UnloadedExtensionInfo* unload_info = | |
121 content::Details<extensions::UnloadedExtensionInfo>(details).ptr(); | |
122 if (!unload_info->extension->is_platform_app()) | |
123 break; | |
124 | |
125 extensions::ExtensionPrefs* extension_prefs = | |
126 extensions::ExtensionPrefs::Get(profile_); | |
127 if (WasUnloadedForReload(*unload_info) && | |
128 extension_prefs->IsActive(unload_info->extension->id()) && | |
129 !HasPostReloadAction(unload_info->extension->id())) { | |
130 post_reload_actions_[unload_info->extension->id()].action_type = LAUNCH; | |
131 } | |
132 break; | 106 break; |
133 } | 107 case LAUNCH_WITH_COMMAND_LINE: |
| 108 LaunchPlatformAppWithCommandLine( |
| 109 profile_, extension, it->second.command_line, it->second.current_dir); |
| 110 break; |
134 default: | 111 default: |
135 NOTREACHED(); | 112 NOTREACHED(); |
136 } | 113 } |
| 114 |
| 115 post_reload_actions_.erase(it); |
| 116 } |
| 117 |
| 118 void AppLoadService::OnExtensionUnloaded( |
| 119 content::BrowserContext* browser_context, |
| 120 const Extension* extension, |
| 121 extensions::UnloadedExtensionInfo::Reason reason) { |
| 122 if (!extension->is_platform_app()) |
| 123 return; |
| 124 |
| 125 extensions::ExtensionPrefs* extension_prefs = |
| 126 extensions::ExtensionPrefs::Get(browser_context); |
| 127 if (WasUnloadedForReload(extension->id(), reason) && |
| 128 extension_prefs->IsActive(extension->id()) && |
| 129 !HasPostReloadAction(extension->id())) { |
| 130 post_reload_actions_[extension->id()].action_type = LAUNCH; |
| 131 } |
137 } | 132 } |
138 | 133 |
139 bool AppLoadService::WasUnloadedForReload( | 134 bool AppLoadService::WasUnloadedForReload( |
140 const extensions::UnloadedExtensionInfo& unload_info) { | 135 const extensions::ExtensionId& extension_id, |
141 if (unload_info.reason == extensions::UnloadedExtensionInfo::REASON_DISABLE) { | 136 const extensions::UnloadedExtensionInfo::Reason reason) { |
| 137 if (reason == extensions::UnloadedExtensionInfo::REASON_DISABLE) { |
142 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); | 138 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); |
143 return (prefs->GetDisableReasons(unload_info.extension->id()) & | 139 return (prefs->GetDisableReasons(extension_id) & |
144 Extension::DISABLE_RELOAD) != 0; | 140 Extension::DISABLE_RELOAD) != 0; |
145 } | 141 } |
146 return false; | 142 return false; |
147 } | 143 } |
148 | 144 |
149 bool AppLoadService::HasPostReloadAction(const std::string& extension_id) { | 145 bool AppLoadService::HasPostReloadAction(const std::string& extension_id) { |
150 return post_reload_actions_.find(extension_id) != post_reload_actions_.end(); | 146 return post_reload_actions_.find(extension_id) != post_reload_actions_.end(); |
151 } | 147 } |
152 | 148 |
153 } // namespace apps | 149 } // namespace apps |
OLD | NEW |