Disable the ASan SEGV handler for NaCl processes on Mac.
[chromium-blink-merge.git] / apps / app_load_service.cc
blobafe99f835d6f232d9d459a8ffba06d78574d53af
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_prefs.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/extensions/extension_system.h"
15 #include "chrome/browser/extensions/unpacked_installer.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "content/public/browser/notification_details.h"
18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/notification_types.h"
20 #include "extensions/common/extension.h"
22 using extensions::Extension;
23 using extensions::ExtensionPrefs;
25 namespace apps {
27 AppLoadService::PostReloadAction::PostReloadAction()
28 : action_type(LAUNCH),
29 command_line(CommandLine::NO_PROGRAM) {
32 AppLoadService::AppLoadService(Profile* profile)
33 : profile_(profile) {
34 registrar_.Add(
35 this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
36 content::NotificationService::AllSources());
37 registrar_.Add(
38 this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
39 content::NotificationService::AllSources());
42 AppLoadService::~AppLoadService() {}
44 void AppLoadService::RestartApplication(const std::string& extension_id) {
45 post_reload_actions_[extension_id].action_type = RESTART;
46 ExtensionService* service = extensions::ExtensionSystem::Get(profile_)->
47 extension_service();
48 DCHECK(service);
49 service->ReloadExtension(extension_id);
52 bool AppLoadService::LoadAndLaunch(const base::FilePath& extension_path,
53 const CommandLine& command_line,
54 const base::FilePath& current_dir) {
55 std::string extension_id;
56 if (!extensions::UnpackedInstaller::Create(profile_->GetExtensionService())->
57 LoadFromCommandLine(base::FilePath(extension_path), &extension_id)) {
58 return false;
61 // Schedule the app to be launched once loaded.
62 PostReloadAction& action = post_reload_actions_[extension_id];
63 action.action_type = LAUNCH_WITH_COMMAND_LINE;
64 action.command_line = command_line;
65 action.current_dir = current_dir;
66 return true;
69 // static
70 AppLoadService* AppLoadService::Get(Profile* profile) {
71 return apps::AppLoadServiceFactory::GetForProfile(profile);
74 void AppLoadService::Observe(int type,
75 const content::NotificationSource& source,
76 const content::NotificationDetails& details) {
77 switch (type) {
78 case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: {
79 extensions::ExtensionHost* host =
80 content::Details<extensions::ExtensionHost>(details).ptr();
81 const Extension* extension = host->extension();
82 // It is possible for an extension to be unloaded before it stops loading.
83 if (!extension)
84 break;
85 std::map<std::string, PostReloadAction>::iterator it =
86 post_reload_actions_.find(extension->id());
87 if (it == post_reload_actions_.end())
88 break;
90 switch (it->second.action_type) {
91 case LAUNCH:
92 LaunchPlatformApp(profile_, extension);
93 break;
94 case RESTART:
95 RestartPlatformApp(profile_, extension);
96 break;
97 case LAUNCH_WITH_COMMAND_LINE:
98 LaunchPlatformAppWithCommandLine(
99 profile_, extension, &it->second.command_line,
100 it->second.current_dir);
101 break;
102 default:
103 NOTREACHED();
106 post_reload_actions_.erase(it);
107 break;
109 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
110 const extensions::UnloadedExtensionInfo* unload_info =
111 content::Details<extensions::UnloadedExtensionInfo>(details).ptr();
112 if (!unload_info->extension->is_platform_app())
113 break;
115 if (WasUnloadedForReload(*unload_info) &&
116 HasShellWindows(unload_info->extension->id()) &&
117 !HasPostReloadAction(unload_info->extension->id())) {
118 post_reload_actions_[unload_info->extension->id()].action_type = LAUNCH;
120 break;
122 default:
123 NOTREACHED();
127 bool AppLoadService::HasShellWindows(const std::string& extension_id) {
128 return !ShellWindowRegistry::Get(profile_)->
129 GetShellWindowsForApp(extension_id).empty();
132 bool AppLoadService::WasUnloadedForReload(
133 const extensions::UnloadedExtensionInfo& unload_info) {
134 if (unload_info.reason == extensions::UnloadedExtensionInfo::REASON_DISABLE) {
135 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_);
136 return (prefs->GetDisableReasons(unload_info.extension->id()) &
137 Extension::DISABLE_RELOAD) != 0;
139 return false;
142 bool AppLoadService::HasPostReloadAction(const std::string& extension_id) {
143 return post_reload_actions_.find(extension_id) != post_reload_actions_.end();
146 } // namespace apps