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/profiles/profile.h"
10 #include "chrome/browser/ui/host_desktop.h"
11 #include "chrome/common/extensions/extension_constants.h"
12 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
13 #include "components/pref_registry/pref_registry_syncable.h"
14 #include "extensions/browser/extension_prefs.h"
15 #include "extensions/browser/extension_registry.h"
16 #include "extensions/browser/pref_names.h"
17 #include "extensions/common/extension.h"
20 #include "ash/shell.h"
23 namespace extensions
{
26 // A preference set by the the NTP to persist the desired launch container type
28 const char kPrefLaunchType
[] = "launchType";
32 namespace launch_util
{
35 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
) {
36 registry
->RegisterIntegerPref(
37 pref_names::kBookmarkAppCreationLaunchType
,
39 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
42 } // namespace launch_util
44 LaunchType
GetLaunchType(const ExtensionPrefs
* prefs
,
45 const Extension
* extension
) {
46 LaunchType result
= LAUNCH_TYPE_DEFAULT
;
48 int value
= GetLaunchTypePrefValue(prefs
, extension
->id());
49 if (value
>= LAUNCH_TYPE_FIRST
&& value
< NUM_LAUNCH_TYPES
)
50 result
= static_cast<LaunchType
>(value
);
55 LaunchType
GetLaunchTypePrefValue(const ExtensionPrefs
* prefs
,
56 const std::string
& extension_id
) {
57 int value
= LAUNCH_TYPE_INVALID
;
58 return prefs
->ReadPrefAsInteger(extension_id
, kPrefLaunchType
, &value
)
59 ? static_cast<LaunchType
>(value
) : LAUNCH_TYPE_INVALID
;
62 void SetLaunchType(content::BrowserContext
* context
,
63 const std::string
& extension_id
,
64 LaunchType launch_type
) {
65 DCHECK(launch_type
>= LAUNCH_TYPE_FIRST
&& launch_type
< NUM_LAUNCH_TYPES
);
67 ExtensionPrefs::Get(context
)->UpdateExtensionPref(
68 extension_id
, kPrefLaunchType
,
69 new base::FundamentalValue(static_cast<int>(launch_type
)));
71 // Sync the launch type.
72 const Extension
* extension
=
73 ExtensionRegistry::Get(context
)
74 ->GetExtensionById(extension_id
, ExtensionRegistry::EVERYTHING
);
76 ExtensionSyncService::Get(context
)->SyncExtensionChangeIfNeeded(*extension
);
79 LaunchContainer
GetLaunchContainer(const ExtensionPrefs
* prefs
,
80 const Extension
* extension
) {
81 LaunchContainer manifest_launch_container
=
82 AppLaunchInfo::GetLaunchContainer(extension
);
84 const LaunchContainer kInvalidLaunchContainer
=
85 static_cast<LaunchContainer
>(-1);
87 LaunchContainer result
= kInvalidLaunchContainer
;
89 if (manifest_launch_container
== LAUNCH_CONTAINER_PANEL
) {
90 // Apps with app.launch.container = 'panel' should always respect the
92 result
= manifest_launch_container
;
93 } else if (manifest_launch_container
== LAUNCH_CONTAINER_TAB
) {
94 // Look for prefs that indicate the user's choice of launch container. The
95 // app's menu on the NTP provides a UI to set this preference.
96 LaunchType prefs_launch_type
= GetLaunchType(prefs
, extension
);
98 if (prefs_launch_type
== LAUNCH_TYPE_WINDOW
) {
99 // If the pref is set to launch a window (or no pref is set, and
100 // window opening is the default), make the container a window.
101 result
= LAUNCH_CONTAINER_WINDOW
;
103 } else if (prefs_launch_type
== LAUNCH_TYPE_FULLSCREEN
&&
104 chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_ASH
) {
105 // LAUNCH_TYPE_FULLSCREEN launches in a maximized app window in ash.
106 // For desktop chrome AURA on all platforms we should open the
107 // application in full screen mode in the current tab, on the same
108 // lines as non AURA chrome.
109 result
= LAUNCH_CONTAINER_WINDOW
;
112 // All other launch types (tab, pinned, fullscreen) are
113 // implemented as tabs in a window.
114 result
= LAUNCH_CONTAINER_TAB
;
117 // If a new value for app.launch.container is added, logic for it should be
118 // added here. LAUNCH_CONTAINER_WINDOW is not present because there is no
119 // way to set it in a manifest.
120 NOTREACHED() << manifest_launch_container
;
123 // All paths should set |result|.
124 if (result
== kInvalidLaunchContainer
) {
125 DLOG(FATAL
) << "Failed to set a launch container.";
126 result
= LAUNCH_CONTAINER_TAB
;
132 bool HasPreferredLaunchContainer(const ExtensionPrefs
* prefs
,
133 const Extension
* extension
) {
135 LaunchContainer manifest_launch_container
=
136 AppLaunchInfo::GetLaunchContainer(extension
);
137 return manifest_launch_container
== LAUNCH_CONTAINER_TAB
&&
138 prefs
->ReadPrefAsInteger(extension
->id(), kPrefLaunchType
, &value
);
141 } // namespace extensions