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.
5 #include "apps/app_load_service.h"
7 #include "apps/app_load_service_factory.h"
8 #include "apps/launcher.h"
9 #include "apps/shell_window_registry.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/extensions/extension_host.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/extension_system.h"
14 #include "chrome/browser/extensions/unpacked_installer.h"
15 #include "chrome/browser/profiles/profile.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 #include "extensions/browser/extension_prefs.h"
20 #include "extensions/common/extension.h"
22 using extensions::Extension
;
23 using extensions::ExtensionPrefs
;
24 using extensions::ExtensionSystem
;
28 AppLoadService::PostReloadAction::PostReloadAction()
29 : action_type(LAUNCH
),
30 command_line(CommandLine::NO_PROGRAM
) {
33 AppLoadService::AppLoadService(Profile
* profile
)
36 this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING
,
37 content::NotificationService::AllSources());
39 this, chrome::NOTIFICATION_EXTENSION_UNLOADED
,
40 content::NotificationService::AllSources());
43 AppLoadService::~AppLoadService() {}
45 void AppLoadService::RestartApplication(const std::string
& extension_id
) {
46 post_reload_actions_
[extension_id
].action_type
= RESTART
;
47 ExtensionService
* service
= extensions::ExtensionSystem::Get(profile_
)->
50 service
->ReloadExtension(extension_id
);
53 bool AppLoadService::LoadAndLaunch(const base::FilePath
& extension_path
,
54 const CommandLine
& command_line
,
55 const base::FilePath
& current_dir
) {
56 ExtensionService
* extension_service
=
57 ExtensionSystem::GetForBrowserContext(profile_
)->extension_service();
58 std::string extension_id
;
59 if (!extensions::UnpackedInstaller::Create(extension_service
)->
60 LoadFromCommandLine(base::FilePath(extension_path
), &extension_id
)) {
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
;
73 AppLoadService
* AppLoadService::Get(Profile
* profile
) {
74 return apps::AppLoadServiceFactory::GetForProfile(profile
);
77 void AppLoadService::Observe(int type
,
78 const content::NotificationSource
& source
,
79 const content::NotificationDetails
& details
) {
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();
85 // It is possible for an extension to be unloaded before it stops loading.
88 std::map
<std::string
, PostReloadAction
>::iterator it
=
89 post_reload_actions_
.find(extension
->id());
90 if (it
== post_reload_actions_
.end())
93 switch (it
->second
.action_type
) {
95 LaunchPlatformApp(profile_
, extension
);
98 RestartPlatformApp(profile_
, extension
);
100 case LAUNCH_WITH_COMMAND_LINE
:
101 LaunchPlatformAppWithCommandLine(
102 profile_
, extension
, it
->second
.command_line
,
103 it
->second
.current_dir
);
109 post_reload_actions_
.erase(it
);
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())
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
;
130 bool AppLoadService::HasShellWindows(const std::string
& extension_id
) {
131 return !ShellWindowRegistry::Get(profile_
)->
132 GetShellWindowsForApp(extension_id
).empty();
135 bool AppLoadService::WasUnloadedForReload(
136 const extensions::UnloadedExtensionInfo
& unload_info
) {
137 if (unload_info
.reason
== extensions::UnloadedExtensionInfo::REASON_DISABLE
) {
138 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile_
);
139 return (prefs
->GetDisableReasons(unload_info
.extension
->id()) &
140 Extension::DISABLE_RELOAD
) != 0;
145 bool AppLoadService::HasPostReloadAction(const std::string
& extension_id
) {
146 return post_reload_actions_
.find(extension_id
) != post_reload_actions_
.end();