1 // Copyright (c) 2012 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/ui/extensions/application_launch_web_app.h"
9 #include "apps/launcher.h"
10 #include "base/metrics/histogram.h"
11 #include "chrome/browser/app_mode/app_mode_utils.h"
12 #include "chrome/browser/extensions/launch_util.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_commands.h"
16 #include "chrome/browser/ui/browser_finder.h"
17 #include "chrome/browser/ui/browser_tabstrip.h"
18 #include "chrome/browser/ui/browser_window.h"
19 #include "chrome/browser/ui/extensions/app_launch_params.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/browser/web_applications/web_app.h"
22 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
23 #include "content/public/browser/render_view_host.h"
24 #include "content/public/browser/web_contents.h"
25 #include "content/public/common/renderer_preferences.h"
26 #include "extensions/browser/extension_prefs.h"
27 #include "extensions/browser/extension_registry.h"
28 #include "extensions/common/extension.h"
29 #include "ui/base/window_open_disposition.h"
30 #include "ui/gfx/rect.h"
32 #if defined(OS_MACOSX)
33 #include "chrome/browser/ui/browser_commands_mac.h"
36 using content::WebContents;
37 using extensions::Extension;
38 using extensions::ExtensionPrefs;
39 using extensions::ExtensionRegistry;
43 const Extension* GetExtension(const AppLaunchParams& params) {
44 if (params.extension_id.empty())
46 ExtensionRegistry* registry = ExtensionRegistry::Get(params.profile);
47 return registry->GetExtensionById(params.extension_id,
48 ExtensionRegistry::ENABLED |
49 ExtensionRegistry::DISABLED |
50 ExtensionRegistry::TERMINATED);
53 ui::WindowShowState DetermineWindowShowState(
55 extensions::LaunchContainer container,
56 const Extension* extension) {
57 if (!extension || container != extensions::LAUNCH_CONTAINER_WINDOW)
58 return ui::SHOW_STATE_DEFAULT;
60 if (chrome::IsRunningInForcedAppMode())
61 return ui::SHOW_STATE_FULLSCREEN;
64 // In ash, LAUNCH_TYPE_FULLSCREEN launches in a maximized app window and
65 // LAUNCH_TYPE_WINDOW launches in a normal app window.
66 extensions::LaunchType launch_type =
67 extensions::GetLaunchType(ExtensionPrefs::Get(profile), extension);
68 if (launch_type == extensions::LAUNCH_TYPE_FULLSCREEN)
69 return ui::SHOW_STATE_MAXIMIZED;
70 else if (launch_type == extensions::LAUNCH_TYPE_WINDOW)
71 return ui::SHOW_STATE_NORMAL;
74 return ui::SHOW_STATE_DEFAULT;
79 WebContents* OpenWebAppWindow(const AppLaunchParams& params, const GURL& url) {
80 Profile* const profile = params.profile;
81 const Extension* const extension = GetExtension(params);
83 std::string app_name = extension ?
84 web_app::GenerateApplicationNameFromExtensionId(extension->id()) :
85 web_app::GenerateApplicationNameFromURL(url);
87 gfx::Rect initial_bounds;
88 if (!params.override_bounds.IsEmpty()) {
89 initial_bounds = params.override_bounds;
90 } else if (extension) {
91 initial_bounds.set_width(
92 extensions::AppLaunchInfo::GetLaunchWidth(extension));
93 initial_bounds.set_height(
94 extensions::AppLaunchInfo::GetLaunchHeight(extension));
97 Browser::CreateParams browser_params(
98 Browser::CreateParams::CreateForApp(app_name,
99 true /* trusted_source */,
102 params.desktop_type));
104 browser_params.initial_show_state = DetermineWindowShowState(profile,
108 Browser* browser = new Browser(browser_params);
110 WebContents* web_contents = chrome::AddSelectedTabWithURL(
111 browser, url, ui::PAGE_TRANSITION_AUTO_TOPLEVEL);
112 web_contents->GetMutableRendererPrefs()->can_accept_load_drops = false;
113 web_contents->GetRenderViewHost()->SyncRendererPrefs();
115 browser->window()->Show();
117 // TODO(jcampan): http://crbug.com/8123 we should not need to set the initial
119 web_contents->SetInitialFocus();
123 WebContents* OpenWebAppTab(const AppLaunchParams& launch_params,
125 const Extension* extension = GetExtension(launch_params);
127 Profile* const profile = launch_params.profile;
128 WindowOpenDisposition disposition = launch_params.disposition;
130 Browser* browser = chrome::FindTabbedBrowser(profile,
132 launch_params.desktop_type);
133 WebContents* contents = NULL;
135 // No browser for this profile, need to open a new one.
136 browser = new Browser(Browser::CreateParams(Browser::TYPE_TABBED,
138 launch_params.desktop_type));
139 browser->window()->Show();
140 // There's no current tab in this browser window, so add a new one.
141 disposition = NEW_FOREGROUND_TAB;
143 // For existing browser, ensure its window is shown and activated.
144 browser->window()->Show();
145 browser->window()->Activate();
148 extensions::LaunchType launch_type =
149 extensions::GetLaunchType(ExtensionPrefs::Get(profile), extension);
150 UMA_HISTOGRAM_ENUMERATION("Extensions.AppTabLaunchType", launch_type, 100);
152 int add_type = TabStripModel::ADD_ACTIVE;
153 if (launch_type == extensions::LAUNCH_TYPE_PINNED)
154 add_type |= TabStripModel::ADD_PINNED;
156 chrome::NavigateParams params(browser, url,
157 ui::PAGE_TRANSITION_AUTO_TOPLEVEL);
158 params.tabstrip_add_types = add_type;
159 params.disposition = disposition;
161 if (disposition == CURRENT_TAB) {
162 WebContents* existing_tab =
163 browser->tab_strip_model()->GetActiveWebContents();
164 TabStripModel* model = browser->tab_strip_model();
165 int tab_index = model->GetIndexOfWebContents(existing_tab);
167 existing_tab->OpenURL(content::OpenURLParams(
169 content::Referrer(existing_tab->GetURL(),
170 blink::WebReferrerPolicyDefault),
171 disposition, ui::PAGE_TRANSITION_LINK, false));
172 // Reset existing_tab as OpenURL() may have clobbered it.
173 existing_tab = browser->tab_strip_model()->GetActiveWebContents();
174 if (params.tabstrip_add_types & TabStripModel::ADD_PINNED) {
175 model->SetTabPinned(tab_index, true);
176 // Pinning may have moved the tab.
177 tab_index = model->GetIndexOfWebContents(existing_tab);
179 if (params.tabstrip_add_types & TabStripModel::ADD_ACTIVE)
180 model->ActivateTabAt(tab_index, true);
182 contents = existing_tab;
184 chrome::Navigate(¶ms);
185 contents = params.target_contents;
188 // On Chrome OS the host desktop type for a browser window is always set to
189 // HOST_DESKTOP_TYPE_ASH. On Windows 8 it is only the case for Chrome ASH
191 if (browser->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH) {
192 // In ash, LAUNCH_FULLSCREEN launches in the OpenApplicationWindow function
193 // i.e. it should not reach here.
194 DCHECK(launch_type != extensions::LAUNCH_TYPE_FULLSCREEN);
196 // TODO(skerner): If we are already in full screen mode, and the user
197 // set the app to open as a regular or pinned tab, what should happen?
198 // Today we open the tab, but stay in full screen mode. Should we leave
199 // full screen mode in this case?
200 if (launch_type == extensions::LAUNCH_TYPE_FULLSCREEN &&
201 !browser->window()->IsFullscreen()) {
202 #if defined(OS_MACOSX)
203 chrome::ToggleFullscreenWithChromeOrFallback(browser);
205 chrome::ToggleFullscreenMode(browser);