1 // Copyright (c) 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 "chrome/browser/extensions/launch_util.h"
7 #include "base/values.h"
8 #include "chrome/browser/extensions/extension_sync_service.h"
9 #include "chrome/browser/extensions/extension_util.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/host_desktop.h"
12 #include "chrome/common/extensions/extension_constants.h"
13 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
14 #include "components/pref_registry/pref_registry_syncable.h"
15 #include "extensions/browser/extension_prefs.h"
16 #include "extensions/browser/extension_registry.h"
17 #include "extensions/browser/pref_names.h"
18 #include "extensions/common/extension.h"
21 #include "ash/shell.h"
24 namespace extensions
{
27 // A preference set by the the NTP to persist the desired launch container type
29 const char kPrefLaunchType
[] = "launchType";
33 namespace launch_util
{
36 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
) {
37 registry
->RegisterIntegerPref(pref_names::kBookmarkAppCreationLaunchType
,
41 } // namespace launch_util
43 LaunchType
GetLaunchType(const ExtensionPrefs
* prefs
,
44 const Extension
* extension
) {
45 LaunchType result
= LAUNCH_TYPE_DEFAULT
;
47 int value
= GetLaunchTypePrefValue(prefs
, extension
->id());
48 if (value
>= LAUNCH_TYPE_FIRST
&& value
< NUM_LAUNCH_TYPES
)
49 result
= static_cast<LaunchType
>(value
);
51 #if defined(OS_MACOSX)
52 // On Mac, opening in a window is only supported if bookmark apps are enabled.
53 if (!extensions::util::IsNewBookmarkAppsEnabled() &&
54 !extension
->is_platform_app() && result
== LAUNCH_TYPE_WINDOW
)
55 result
= LAUNCH_TYPE_REGULAR
;
58 if (extensions::util::IsNewBookmarkAppsEnabled()) {
59 if (result
== LAUNCH_TYPE_PINNED
)
60 result
= LAUNCH_TYPE_REGULAR
;
61 if (result
== LAUNCH_TYPE_FULLSCREEN
)
62 result
= LAUNCH_TYPE_WINDOW
;
68 LaunchType
GetLaunchTypePrefValue(const ExtensionPrefs
* prefs
,
69 const std::string
& extension_id
) {
70 int value
= LAUNCH_TYPE_INVALID
;
71 return prefs
->ReadPrefAsInteger(extension_id
, kPrefLaunchType
, &value
)
72 ? static_cast<LaunchType
>(value
) : LAUNCH_TYPE_INVALID
;
75 void SetLaunchType(content::BrowserContext
* context
,
76 const std::string
& extension_id
,
77 LaunchType launch_type
) {
78 DCHECK(launch_type
>= LAUNCH_TYPE_FIRST
&& launch_type
< NUM_LAUNCH_TYPES
);
80 ExtensionPrefs::Get(context
)->UpdateExtensionPref(
81 extension_id
, kPrefLaunchType
,
82 new base::FundamentalValue(static_cast<int>(launch_type
)));
84 // Sync the launch type.
85 const Extension
* extension
=
86 ExtensionRegistry::Get(context
)
87 ->GetExtensionById(extension_id
, ExtensionRegistry::EVERYTHING
);
89 ExtensionSyncService::Get(context
)->SyncExtensionChangeIfNeeded(*extension
);
92 LaunchContainer
GetLaunchContainer(const ExtensionPrefs
* prefs
,
93 const Extension
* extension
) {
94 LaunchContainer manifest_launch_container
=
95 AppLaunchInfo::GetLaunchContainer(extension
);
97 const LaunchContainer kInvalidLaunchContainer
=
98 static_cast<LaunchContainer
>(-1);
100 LaunchContainer result
= kInvalidLaunchContainer
;
102 if (manifest_launch_container
== LAUNCH_CONTAINER_PANEL
) {
103 // Apps with app.launch.container = 'panel' should always respect the
105 result
= manifest_launch_container
;
106 } else if (manifest_launch_container
== LAUNCH_CONTAINER_TAB
) {
107 // Look for prefs that indicate the user's choice of launch container. The
108 // app's menu on the NTP provides a UI to set this preference.
109 LaunchType prefs_launch_type
= GetLaunchType(prefs
, extension
);
111 if (prefs_launch_type
== LAUNCH_TYPE_WINDOW
) {
112 // If the pref is set to launch a window (or no pref is set, and
113 // window opening is the default), make the container a window.
114 result
= LAUNCH_CONTAINER_WINDOW
;
116 } else if (prefs_launch_type
== LAUNCH_TYPE_FULLSCREEN
&&
117 chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_ASH
) {
118 // LAUNCH_TYPE_FULLSCREEN launches in a maximized app window in ash.
119 // For desktop chrome AURA on all platforms we should open the
120 // application in full screen mode in the current tab, on the same
121 // lines as non AURA chrome.
122 result
= LAUNCH_CONTAINER_WINDOW
;
125 // All other launch types (tab, pinned, fullscreen) are
126 // implemented as tabs in a window.
127 result
= LAUNCH_CONTAINER_TAB
;
130 // If a new value for app.launch.container is added, logic for it should be
131 // added here. LAUNCH_CONTAINER_WINDOW is not present because there is no
132 // way to set it in a manifest.
133 NOTREACHED() << manifest_launch_container
;
136 // All paths should set |result|.
137 if (result
== kInvalidLaunchContainer
) {
138 DLOG(FATAL
) << "Failed to set a launch container.";
139 result
= LAUNCH_CONTAINER_TAB
;
145 bool HasPreferredLaunchContainer(const ExtensionPrefs
* prefs
,
146 const Extension
* extension
) {
148 LaunchContainer manifest_launch_container
=
149 AppLaunchInfo::GetLaunchContainer(extension
);
150 return manifest_launch_container
== LAUNCH_CONTAINER_TAB
&&
151 prefs
->ReadPrefAsInteger(extension
->id(), kPrefLaunchType
, &value
);
154 bool LaunchesInWindow(content::BrowserContext
* context
,
155 const Extension
* extension
) {
156 return GetLaunchType(ExtensionPrefs::Get(context
), extension
) ==
160 } // namespace extensions