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(
38 pref_names::kBookmarkAppCreationLaunchType
,
40 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
43 } // namespace launch_util
45 LaunchType
GetLaunchType(const ExtensionPrefs
* prefs
,
46 const Extension
* extension
) {
47 LaunchType result
= LAUNCH_TYPE_DEFAULT
;
49 int value
= GetLaunchTypePrefValue(prefs
, extension
->id());
50 if (value
>= LAUNCH_TYPE_FIRST
&& value
< NUM_LAUNCH_TYPES
)
51 result
= static_cast<LaunchType
>(value
);
53 #if defined(OS_MACOSX)
54 // On Mac, opening in a window is only supported if bookmark apps are enabled.
55 if (!extensions::util::IsNewBookmarkAppsEnabled() &&
56 !extension
->is_platform_app() && result
== LAUNCH_TYPE_WINDOW
)
57 result
= LAUNCH_TYPE_REGULAR
;
63 LaunchType
GetLaunchTypePrefValue(const ExtensionPrefs
* prefs
,
64 const std::string
& extension_id
) {
65 int value
= LAUNCH_TYPE_INVALID
;
66 return prefs
->ReadPrefAsInteger(extension_id
, kPrefLaunchType
, &value
)
67 ? static_cast<LaunchType
>(value
) : LAUNCH_TYPE_INVALID
;
70 void SetLaunchType(content::BrowserContext
* context
,
71 const std::string
& extension_id
,
72 LaunchType launch_type
) {
73 DCHECK(launch_type
>= LAUNCH_TYPE_FIRST
&& launch_type
< NUM_LAUNCH_TYPES
);
75 ExtensionPrefs::Get(context
)->UpdateExtensionPref(
76 extension_id
, kPrefLaunchType
,
77 new base::FundamentalValue(static_cast<int>(launch_type
)));
79 // Sync the launch type.
80 const Extension
* extension
=
81 ExtensionRegistry::Get(context
)
82 ->GetExtensionById(extension_id
, ExtensionRegistry::EVERYTHING
);
84 ExtensionSyncService::Get(context
)->SyncExtensionChangeIfNeeded(*extension
);
87 LaunchContainer
GetLaunchContainer(const ExtensionPrefs
* prefs
,
88 const Extension
* extension
) {
89 LaunchContainer manifest_launch_container
=
90 AppLaunchInfo::GetLaunchContainer(extension
);
92 const LaunchContainer kInvalidLaunchContainer
=
93 static_cast<LaunchContainer
>(-1);
95 LaunchContainer result
= kInvalidLaunchContainer
;
97 if (manifest_launch_container
== LAUNCH_CONTAINER_PANEL
) {
98 // Apps with app.launch.container = 'panel' should always respect the
100 result
= manifest_launch_container
;
101 } else if (manifest_launch_container
== LAUNCH_CONTAINER_TAB
) {
102 // Look for prefs that indicate the user's choice of launch container. The
103 // app's menu on the NTP provides a UI to set this preference.
104 LaunchType prefs_launch_type
= GetLaunchType(prefs
, extension
);
106 if (prefs_launch_type
== LAUNCH_TYPE_WINDOW
) {
107 // If the pref is set to launch a window (or no pref is set, and
108 // window opening is the default), make the container a window.
109 result
= LAUNCH_CONTAINER_WINDOW
;
111 } else if (prefs_launch_type
== LAUNCH_TYPE_FULLSCREEN
&&
112 chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_ASH
) {
113 // LAUNCH_TYPE_FULLSCREEN launches in a maximized app window in ash.
114 // For desktop chrome AURA on all platforms we should open the
115 // application in full screen mode in the current tab, on the same
116 // lines as non AURA chrome.
117 result
= LAUNCH_CONTAINER_WINDOW
;
120 // All other launch types (tab, pinned, fullscreen) are
121 // implemented as tabs in a window.
122 result
= LAUNCH_CONTAINER_TAB
;
125 // If a new value for app.launch.container is added, logic for it should be
126 // added here. LAUNCH_CONTAINER_WINDOW is not present because there is no
127 // way to set it in a manifest.
128 NOTREACHED() << manifest_launch_container
;
131 // All paths should set |result|.
132 if (result
== kInvalidLaunchContainer
) {
133 DLOG(FATAL
) << "Failed to set a launch container.";
134 result
= LAUNCH_CONTAINER_TAB
;
140 bool HasPreferredLaunchContainer(const ExtensionPrefs
* prefs
,
141 const Extension
* extension
) {
143 LaunchContainer manifest_launch_container
=
144 AppLaunchInfo::GetLaunchContainer(extension
);
145 return manifest_launch_container
== LAUNCH_CONTAINER_TAB
&&
146 prefs
->ReadPrefAsInteger(extension
->id(), kPrefLaunchType
, &value
);
149 } // namespace extensions