Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / extensions / launch_util.cc
blobe1f4d6fd7a0fd6ed67413da1456a58433a361293
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/command_line.h"
8 #include "base/values.h"
9 #include "chrome/browser/extensions/extension_prefs.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/ui/host_desktop.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/common/extensions/extension_constants.h"
14 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
15 #include "extensions/common/extension.h"
17 #if defined(OS_WIN)
18 #include "win8/util/win8_util.h"
19 #endif
21 #if defined(USE_ASH)
22 #include "ash/shell.h"
23 #endif
25 namespace extensions {
26 namespace {
28 // A preference set by the the NTP to persist the desired launch container type
29 // used for apps.
30 const char kPrefLaunchType[] = "launchType";
32 } // namespace
34 LaunchType GetLaunchType(const ExtensionPrefs* prefs,
35 const Extension* extension) {
36 LaunchType result = LAUNCH_TYPE_DEFAULT;
38 // Launch hosted apps as windows by default for streamlined hosted apps.
39 if (CommandLine::ForCurrentProcess()->
40 HasSwitch(switches::kEnableStreamlinedHostedApps) &&
41 extension->id() != extension_misc::kChromeAppId) {
42 result = LAUNCH_TYPE_WINDOW;
45 int value = GetLaunchTypePrefValue(prefs, extension->id());
46 if (value >= LAUNCH_TYPE_FIRST && value < NUM_LAUNCH_TYPES)
47 result = static_cast<LaunchType>(value);
49 #if defined(OS_MACOSX)
50 // App windows are not yet supported on mac. Pref sync could make
51 // the launch type LAUNCH_TYPE_WINDOW, even if there is no UI to set it
52 // on mac.
53 if (!extension->is_platform_app() && result == LAUNCH_TYPE_WINDOW)
54 result = LAUNCH_TYPE_REGULAR;
55 #endif
57 #if defined(OS_WIN)
58 // We don't support app windows in Windows 8 single window Metro mode.
59 if (win8::IsSingleWindowMetroMode() && result == LAUNCH_TYPE_WINDOW)
60 result = LAUNCH_TYPE_REGULAR;
61 #endif // OS_WIN
63 return result;
66 LaunchType GetLaunchTypePrefValue(const ExtensionPrefs* prefs,
67 const std::string& extension_id) {
68 int value = LAUNCH_TYPE_INVALID;
69 return prefs->ReadPrefAsInteger(extension_id, kPrefLaunchType, &value)
70 ? static_cast<LaunchType>(value) : LAUNCH_TYPE_INVALID;
73 void SetLaunchType(ExtensionService* service,
74 const std::string& extension_id,
75 LaunchType launch_type) {
76 DCHECK(launch_type >= LAUNCH_TYPE_FIRST && launch_type < NUM_LAUNCH_TYPES);
78 service->extension_prefs()->UpdateExtensionPref(extension_id, kPrefLaunchType,
79 new base::FundamentalValue(static_cast<int>(launch_type)));
81 // Sync the launch type.
82 const Extension* extension = service->GetInstalledExtension(extension_id);
83 if (extension) {
84 ExtensionSyncService::Get(service->profile())->
85 SyncExtensionChangeIfNeeded(*extension);
89 LaunchContainer GetLaunchContainer(const ExtensionPrefs* prefs,
90 const Extension* extension) {
91 LaunchContainer manifest_launch_container =
92 AppLaunchInfo::GetLaunchContainer(extension);
94 const LaunchContainer kInvalidLaunchContainer =
95 static_cast<LaunchContainer>(-1);
97 LaunchContainer result = kInvalidLaunchContainer;
99 if (manifest_launch_container == LAUNCH_CONTAINER_PANEL) {
100 // Apps with app.launch.container = 'panel' should always respect the
101 // manifest setting.
102 result = manifest_launch_container;
103 } else if (manifest_launch_container == LAUNCH_CONTAINER_TAB) {
104 // Look for prefs that indicate the user's choice of launch container. The
105 // app's menu on the NTP provides a UI to set this preference.
106 LaunchType prefs_launch_type = GetLaunchType(prefs, extension);
108 if (prefs_launch_type == LAUNCH_TYPE_WINDOW) {
109 // If the pref is set to launch a window (or no pref is set, and
110 // window opening is the default), make the container a window.
111 result = LAUNCH_CONTAINER_WINDOW;
112 #if defined(USE_ASH)
113 } else if (prefs_launch_type == LAUNCH_TYPE_FULLSCREEN &&
114 chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_ASH) {
115 // LAUNCH_TYPE_FULLSCREEN launches in a maximized app window in ash.
116 // For desktop chrome AURA on all platforms we should open the
117 // application in full screen mode in the current tab, on the same
118 // lines as non AURA chrome.
119 result = LAUNCH_CONTAINER_WINDOW;
120 #endif
121 } else {
122 // All other launch types (tab, pinned, fullscreen) are
123 // implemented as tabs in a window.
124 result = LAUNCH_CONTAINER_TAB;
126 } else {
127 // If a new value for app.launch.container is added, logic for it should be
128 // added here. LAUNCH_CONTAINER_WINDOW is not present because there is no
129 // way to set it in a manifest.
130 NOTREACHED() << manifest_launch_container;
133 // All paths should set |result|.
134 if (result == kInvalidLaunchContainer) {
135 DLOG(FATAL) << "Failed to set a launch container.";
136 result = LAUNCH_CONTAINER_TAB;
139 return result;
142 bool HasPreferredLaunchContainer(const ExtensionPrefs* prefs,
143 const Extension* extension) {
144 int value = -1;
145 LaunchContainer manifest_launch_container =
146 AppLaunchInfo::GetLaunchContainer(extension);
147 return manifest_launch_container == LAUNCH_CONTAINER_TAB &&
148 prefs->ReadPrefAsInteger(extension->id(), kPrefLaunchType, &value);
151 } // namespace extensions