Mac Video Capture: Prefer MJPEG above certain resolution for AVFoundation
[chromium-blink-merge.git] / apps / app_load_service.cc
blobc4d308ecccb3e8baf155d63d09157da8c054372c
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/app_restore_service.h"
9 #include "apps/launcher.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/unpacked_installer.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "content/public/browser/notification_details.h"
14 #include "content/public/browser/notification_service.h"
15 #include "content/public/browser/notification_types.h"
16 #include "extensions/browser/app_window/app_window_registry.h"
17 #include "extensions/browser/extension_host.h"
18 #include "extensions/browser/extension_prefs.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/browser/extension_system.h"
21 #include "extensions/browser/notification_types.h"
22 #include "extensions/common/extension.h"
24 using extensions::Extension;
25 using extensions::ExtensionPrefs;
26 using extensions::ExtensionSystem;
28 namespace apps {
30 AppLoadService::PostReloadAction::PostReloadAction()
31 : action_type(LAUNCH_FOR_RELOAD),
32 command_line(base::CommandLine::NO_PROGRAM) {
35 AppLoadService::AppLoadService(Profile* profile)
36 : profile_(profile) {
37 registrar_.Add(this,
38 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
39 content::NotificationService::AllSources());
40 extensions::ExtensionRegistry::Get(profile_)->AddObserver(this);
43 AppLoadService::~AppLoadService() {
44 extensions::ExtensionRegistry::Get(profile_)->RemoveObserver(this);
47 void AppLoadService::RestartApplication(const std::string& extension_id) {
48 post_reload_actions_[extension_id].action_type = RESTART;
49 ExtensionService* service = extensions::ExtensionSystem::Get(profile_)->
50 extension_service();
51 DCHECK(service);
52 service->ReloadExtension(extension_id);
55 void AppLoadService::RestartApplicationIfRunning(
56 const std::string& extension_id) {
57 if (apps::AppRestoreService::Get(profile_)->IsAppRestorable(extension_id))
58 RestartApplication(extension_id);
61 bool AppLoadService::LoadAndLaunch(const base::FilePath& extension_path,
62 const base::CommandLine& command_line,
63 const base::FilePath& current_dir) {
64 ExtensionService* extension_service =
65 ExtensionSystem::Get(profile_)->extension_service();
66 std::string extension_id;
67 if (!extensions::UnpackedInstaller::Create(extension_service)->
68 LoadFromCommandLine(base::FilePath(extension_path), &extension_id)) {
69 return false;
72 // Schedule the app to be launched once loaded.
73 PostReloadAction& action = post_reload_actions_[extension_id];
74 action.action_type = LAUNCH_FOR_LOAD_AND_LAUNCH;
75 action.command_line = command_line;
76 action.current_dir = current_dir;
77 return true;
80 bool AppLoadService::Load(const base::FilePath& extension_path) {
81 ExtensionService* extension_service =
82 ExtensionSystem::Get(profile_)->extension_service();
83 std::string extension_id;
84 return extensions::UnpackedInstaller::Create(extension_service)->
85 LoadFromCommandLine(base::FilePath(extension_path), &extension_id);
88 // static
89 AppLoadService* AppLoadService::Get(Profile* profile) {
90 return apps::AppLoadServiceFactory::GetForProfile(profile);
93 void AppLoadService::Observe(int type,
94 const content::NotificationSource& source,
95 const content::NotificationDetails& details) {
96 DCHECK_EQ(type, extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING);
97 extensions::ExtensionHost* host =
98 content::Details<extensions::ExtensionHost>(details).ptr();
99 const Extension* extension = host->extension();
100 // It is possible for an extension to be unloaded before it stops loading.
101 if (!extension)
102 return;
103 std::map<std::string, PostReloadAction>::iterator it =
104 post_reload_actions_.find(extension->id());
105 if (it == post_reload_actions_.end())
106 return;
108 switch (it->second.action_type) {
109 case LAUNCH_FOR_RELOAD:
110 LaunchPlatformApp(profile_, extension, extensions::SOURCE_RELOAD);
111 break;
112 case RESTART:
113 RestartPlatformApp(profile_, extension);
114 break;
115 case LAUNCH_FOR_LOAD_AND_LAUNCH:
116 LaunchPlatformAppWithCommandLine(profile_,
117 extension,
118 it->second.command_line,
119 it->second.current_dir,
120 extensions::SOURCE_LOAD_AND_LAUNCH);
121 break;
122 default:
123 NOTREACHED();
126 post_reload_actions_.erase(it);
129 void AppLoadService::OnExtensionUnloaded(
130 content::BrowserContext* browser_context,
131 const Extension* extension,
132 extensions::UnloadedExtensionInfo::Reason reason) {
133 if (!extension->is_platform_app())
134 return;
136 extensions::ExtensionPrefs* extension_prefs =
137 extensions::ExtensionPrefs::Get(browser_context);
138 if (WasUnloadedForReload(extension->id(), reason) &&
139 extension_prefs->IsActive(extension->id()) &&
140 !HasPostReloadAction(extension->id())) {
141 post_reload_actions_[extension->id()].action_type = LAUNCH_FOR_RELOAD;
145 bool AppLoadService::WasUnloadedForReload(
146 const extensions::ExtensionId& extension_id,
147 const extensions::UnloadedExtensionInfo::Reason reason) {
148 if (reason == extensions::UnloadedExtensionInfo::REASON_DISABLE) {
149 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_);
150 return (prefs->GetDisableReasons(extension_id) &
151 Extension::DISABLE_RELOAD) != 0;
153 return false;
156 bool AppLoadService::HasPostReloadAction(const std::string& extension_id) {
157 return post_reload_actions_.find(extension_id) != post_reload_actions_.end();
160 } // namespace apps