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.h"
9 #include "apps/launcher.h"
10 #include "base/command_line.h"
11 #include "base/metrics/field_trial.h"
12 #include "base/metrics/histogram.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/app_mode/app_mode_utils.h"
15 #include "chrome/browser/apps/per_app_settings_service.h"
16 #include "chrome/browser/apps/per_app_settings_service_factory.h"
17 #include "chrome/browser/extensions/extension_service.h"
18 #include "chrome/browser/extensions/launch_util.h"
19 #include "chrome/browser/extensions/tab_helper.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/signin/signin_manager_factory.h"
22 #include "chrome/browser/ui/app_list/app_list_service.h"
23 #include "chrome/browser/ui/browser.h"
24 #include "chrome/browser/ui/browser_commands.h"
25 #include "chrome/browser/ui/browser_finder.h"
26 #include "chrome/browser/ui/browser_tabstrip.h"
27 #include "chrome/browser/ui/browser_window.h"
28 #include "chrome/browser/ui/extensions/extension_enable_flow.h"
29 #include "chrome/browser/ui/extensions/extension_enable_flow_delegate.h"
30 #include "chrome/browser/ui/tabs/tab_strip_model.h"
31 #include "chrome/browser/web_applications/web_app.h"
32 #include "chrome/common/chrome_switches.h"
33 #include "chrome/common/extensions/extension_constants.h"
34 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
35 #include "chrome/common/extensions/manifest_url_handler.h"
36 #include "chrome/common/url_constants.h"
37 #include "components/signin/core/browser/signin_manager.h"
38 #include "content/public/browser/render_view_host.h"
39 #include "content/public/browser/web_contents.h"
40 #include "content/public/browser/web_contents_view.h"
41 #include "content/public/common/renderer_preferences.h"
42 #include "extensions/browser/extension_prefs.h"
43 #include "extensions/browser/extension_registry.h"
44 #include "extensions/browser/extension_system.h"
45 #include "extensions/common/constants.h"
46 #include "extensions/common/extension.h"
47 #include "grit/generated_resources.h"
48 #include "ui/base/l10n/l10n_util.h"
49 #include "ui/base/window_open_disposition.h"
50 #include "ui/gfx/rect.h"
52 #if defined(OS_MACOSX)
53 #include "chrome/browser/ui/browser_commands_mac.h"
57 #include "win8/util/win8_util.h"
60 using content::WebContents
;
61 using extensions::Extension
;
62 using extensions::ExtensionPrefs
;
63 using extensions::ExtensionRegistry
;
67 // Attempts to launch a packaged app, prompting the user to enable it if
68 // necessary. If a prompt is required it will be shown inside the AppList.
69 // This class manages its own lifetime.
70 class EnableViaAppListFlow
: public ExtensionEnableFlowDelegate
{
72 EnableViaAppListFlow(ExtensionService
* service
,
74 chrome::HostDesktopType desktop_type
,
75 const std::string
& extension_id
,
76 const base::Closure
& callback
)
79 desktop_type_(desktop_type
),
80 extension_id_(extension_id
),
84 virtual ~EnableViaAppListFlow() {
88 DCHECK(!service_
->IsExtensionEnabled(extension_id_
));
89 flow_
.reset(new ExtensionEnableFlow(profile_
, extension_id_
, this));
90 flow_
->StartForCurrentlyNonexistentWindow(
91 base::Bind(&EnableViaAppListFlow::ShowAppList
, base::Unretained(this)));
95 gfx::NativeWindow
ShowAppList() {
96 AppListService
* app_list_service
= AppListService::Get(desktop_type_
);
97 app_list_service
->Show();
98 return app_list_service
->GetAppListWindow();
101 // ExtensionEnableFlowDelegate overrides.
102 virtual void ExtensionEnableFlowFinished() OVERRIDE
{
103 const Extension
* extension
=
104 service_
->GetExtensionById(extension_id_
, false);
111 virtual void ExtensionEnableFlowAborted(bool user_initiated
) OVERRIDE
{
115 ExtensionService
* service_
;
117 chrome::HostDesktopType desktop_type_
;
118 std::string extension_id_
;
119 base::Closure callback_
;
120 scoped_ptr
<ExtensionEnableFlow
> flow_
;
122 DISALLOW_COPY_AND_ASSIGN(EnableViaAppListFlow
);
125 const Extension
* GetExtension(const AppLaunchParams
& params
) {
126 if (params
.extension_id
.empty())
128 ExtensionRegistry
* registry
= ExtensionRegistry::Get(params
.profile
);
129 return registry
->GetExtensionById(params
.extension_id
,
130 ExtensionRegistry::ENABLED
|
131 ExtensionRegistry::DISABLED
|
132 ExtensionRegistry::TERMINATED
);
135 // Get the launch URL for a given extension, with optional override/fallback.
136 // |override_url|, if non-empty, will be preferred over the extension's
138 GURL
UrlForExtension(const Extension
* extension
,
139 const GURL
& override_url
) {
144 if (!override_url
.is_empty()) {
145 DCHECK(extension
->web_extent().MatchesURL(override_url
) ||
146 override_url
.GetOrigin() == extension
->url());
149 url
= extensions::AppLaunchInfo::GetFullLaunchURL(extension
);
152 // For extensions lacking launch urls, determine a reasonable fallback.
153 if (!url
.is_valid()) {
154 url
= extensions::ManifestURL::GetOptionsPage(extension
);
156 url
= GURL(chrome::kChromeUIExtensionsURL
);
162 ui::WindowShowState
DetermineWindowShowState(
164 extensions::LaunchContainer container
,
165 const Extension
* extension
) {
166 if (!extension
|| container
!= extensions::LAUNCH_CONTAINER_WINDOW
)
167 return ui::SHOW_STATE_DEFAULT
;
169 if (chrome::IsRunningInForcedAppMode())
170 return ui::SHOW_STATE_FULLSCREEN
;
173 // In ash, LAUNCH_TYPE_FULLSCREEN launches in a maximized app window and
174 // LAUNCH_TYPE_WINDOW launches in a normal app window.
175 extensions::LaunchType launch_type
=
176 extensions::GetLaunchType(ExtensionPrefs::Get(profile
), extension
);
177 if (launch_type
== extensions::LAUNCH_TYPE_FULLSCREEN
)
178 return ui::SHOW_STATE_MAXIMIZED
;
179 else if (launch_type
== extensions::LAUNCH_TYPE_WINDOW
)
180 return ui::SHOW_STATE_NORMAL
;
183 return ui::SHOW_STATE_DEFAULT
;
186 WebContents
* OpenApplicationWindow(const AppLaunchParams
& params
) {
187 Profile
* const profile
= params
.profile
;
188 const Extension
* const extension
= GetExtension(params
);
189 const GURL url_input
= params
.override_url
;
191 DCHECK(!url_input
.is_empty() || extension
);
192 GURL url
= UrlForExtension(extension
, url_input
);
193 Browser::CreateParams
browser_params(
194 Browser::TYPE_POPUP
, profile
, params
.desktop_type
);
196 browser_params
.app_name
= extension
?
197 web_app::GenerateApplicationNameFromExtensionId(extension
->id()) :
198 web_app::GenerateApplicationNameFromURL(url
);
200 if (!params
.override_bounds
.IsEmpty()) {
201 browser_params
.initial_bounds
= params
.override_bounds
;
202 } else if (extension
) {
203 browser_params
.initial_bounds
.set_width(
204 extensions::AppLaunchInfo::GetLaunchWidth(extension
));
205 browser_params
.initial_bounds
.set_height(
206 extensions::AppLaunchInfo::GetLaunchHeight(extension
));
209 browser_params
.initial_show_state
= DetermineWindowShowState(profile
,
213 Browser
* browser
= NULL
;
215 // On Windows 8's single window Metro mode we don't allow multiple Chrome
216 // windows to be created. We instead attempt to reuse an existing Browser
218 if (win8::IsSingleWindowMetroMode())
219 browser
= chrome::FindBrowserWithProfile(profile
, params
.desktop_type
);
223 browser
= new Browser(browser_params
);
225 WebContents
* web_contents
= chrome::AddSelectedTabWithURL(
226 browser
, url
, content::PAGE_TRANSITION_AUTO_TOPLEVEL
);
227 web_contents
->GetMutableRendererPrefs()->can_accept_load_drops
= false;
228 web_contents
->GetRenderViewHost()->SyncRendererPrefs();
230 browser
->window()->Show();
232 // TODO(jcampan): http://crbug.com/8123 we should not need to set the initial
234 web_contents
->GetView()->SetInitialFocus();
238 WebContents
* OpenApplicationTab(const AppLaunchParams
& launch_params
) {
239 const Extension
* extension
= GetExtension(launch_params
);
241 Profile
* const profile
= launch_params
.profile
;
242 WindowOpenDisposition disposition
= launch_params
.disposition
;
244 Browser
* browser
= chrome::FindTabbedBrowser(profile
,
246 launch_params
.desktop_type
);
247 WebContents
* contents
= NULL
;
249 // No browser for this profile, need to open a new one.
250 browser
= new Browser(Browser::CreateParams(Browser::TYPE_TABBED
,
252 launch_params
.desktop_type
));
253 browser
->window()->Show();
254 // There's no current tab in this browser window, so add a new one.
255 disposition
= NEW_FOREGROUND_TAB
;
257 // For existing browser, ensure its window is shown and activated.
258 browser
->window()->Show();
259 browser
->window()->Activate();
262 extensions::LaunchType launch_type
=
263 extensions::GetLaunchType(ExtensionPrefs::Get(profile
), extension
);
264 UMA_HISTOGRAM_ENUMERATION("Extensions.AppTabLaunchType", launch_type
, 100);
266 int add_type
= TabStripModel::ADD_ACTIVE
;
267 if (launch_type
== extensions::LAUNCH_TYPE_PINNED
)
268 add_type
|= TabStripModel::ADD_PINNED
;
270 GURL extension_url
= UrlForExtension(extension
, launch_params
.override_url
);
271 chrome::NavigateParams
params(browser
, extension_url
,
272 content::PAGE_TRANSITION_AUTO_TOPLEVEL
);
273 params
.tabstrip_add_types
= add_type
;
274 params
.disposition
= disposition
;
276 if (disposition
== CURRENT_TAB
) {
277 WebContents
* existing_tab
=
278 browser
->tab_strip_model()->GetActiveWebContents();
279 TabStripModel
* model
= browser
->tab_strip_model();
280 int tab_index
= model
->GetIndexOfWebContents(existing_tab
);
282 existing_tab
->OpenURL(content::OpenURLParams(
284 content::Referrer(existing_tab
->GetURL(),
285 blink::WebReferrerPolicyDefault
),
286 disposition
, content::PAGE_TRANSITION_LINK
, false));
287 // Reset existing_tab as OpenURL() may have clobbered it.
288 existing_tab
= browser
->tab_strip_model()->GetActiveWebContents();
289 if (params
.tabstrip_add_types
& TabStripModel::ADD_PINNED
) {
290 model
->SetTabPinned(tab_index
, true);
291 // Pinning may have moved the tab.
292 tab_index
= model
->GetIndexOfWebContents(existing_tab
);
294 if (params
.tabstrip_add_types
& TabStripModel::ADD_ACTIVE
)
295 model
->ActivateTabAt(tab_index
, true);
297 contents
= existing_tab
;
299 chrome::Navigate(¶ms
);
300 contents
= params
.target_contents
;
303 // On Chrome OS the host desktop type for a browser window is always set to
304 // HOST_DESKTOP_TYPE_ASH. On Windows 8 it is only the case for Chrome ASH
306 if (browser
->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH
) {
307 // In ash, LAUNCH_FULLSCREEN launches in the OpenApplicationWindow function
308 // i.e. it should not reach here.
309 DCHECK(launch_type
!= extensions::LAUNCH_TYPE_FULLSCREEN
);
311 // TODO(skerner): If we are already in full screen mode, and the user
312 // set the app to open as a regular or pinned tab, what should happen?
313 // Today we open the tab, but stay in full screen mode. Should we leave
314 // full screen mode in this case?
315 if (launch_type
== extensions::LAUNCH_TYPE_FULLSCREEN
&&
316 !browser
->window()->IsFullscreen()) {
317 #if defined(OS_MACOSX)
318 chrome::ToggleFullscreenWithChromeOrFallback(browser
);
320 chrome::ToggleFullscreenMode(browser
);
327 WebContents
* OpenEnabledApplication(const AppLaunchParams
& params
) {
328 const Extension
* extension
= GetExtension(params
);
331 Profile
* profile
= params
.profile
;
333 WebContents
* tab
= NULL
;
334 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile
);
335 prefs
->SetActiveBit(extension
->id(), true);
337 UMA_HISTOGRAM_ENUMERATION(
338 "Extensions.AppLaunchContainer", params
.container
, 100);
340 if (extension
->is_platform_app()) {
341 // Remember what desktop the launch happened on so that when the app opens a
342 // window we can open them on the right desktop.
343 PerAppSettingsServiceFactory::GetForBrowserContext(profile
)->
344 SetDesktopLastLaunchedFrom(extension
->id(), params
.desktop_type
);
345 #if !defined(OS_CHROMEOS)
346 SigninManager
* signin_manager
=
347 SigninManagerFactory::GetForProfile(profile
);
348 if (extension
->id() != extension_misc::kSettingsAppId
&&
349 signin_manager
&& signin_manager
->GetAuthenticatedUsername().empty()) {
350 const char kEnforceSigninToUseAppsFieldTrial
[] = "EnforceSigninToUseApps";
352 std::string field_trial_value
=
353 base::FieldTrialList::FindFullName(kEnforceSigninToUseAppsFieldTrial
);
355 // Only enforce signin if the field trial is set.
356 if (!field_trial_value
.empty()) {
357 GURL
gurl(l10n_util::GetStringFUTF8(
358 IDS_APP_LAUNCH_NOT_SIGNED_IN_LINK
,
359 base::UTF8ToUTF16(extension
->id())));
360 chrome::NavigateParams
navigate_params(profile
, gurl
,
361 content::PAGE_TRANSITION_LINK
);
362 navigate_params
.host_desktop_type
= params
.desktop_type
;
363 chrome::Navigate(&navigate_params
);
369 apps::LaunchPlatformAppWithCommandLine(
370 profile
, extension
, params
.command_line
, params
.current_directory
);
374 // Record v1 app launch. Platform app launch is recorded when dispatching
375 // the onLaunched event.
376 prefs
->SetLastLaunchTime(extension
->id(), base::Time::Now());
378 switch (params
.container
) {
379 case extensions::LAUNCH_CONTAINER_NONE
: {
383 case extensions::LAUNCH_CONTAINER_PANEL
:
384 case extensions::LAUNCH_CONTAINER_WINDOW
:
385 tab
= OpenApplicationWindow(params
);
387 case extensions::LAUNCH_CONTAINER_TAB
: {
388 tab
= OpenApplicationTab(params
);
400 AppLaunchParams::AppLaunchParams(Profile
* profile
,
401 const extensions::Extension
* extension
,
402 extensions::LaunchContainer container
,
403 WindowOpenDisposition disposition
)
405 extension_id(extension
? extension
->id() : std::string()),
406 container(container
),
407 disposition(disposition
),
408 desktop_type(chrome::GetActiveDesktop()),
411 command_line(CommandLine::NO_PROGRAM
) {}
413 AppLaunchParams::AppLaunchParams(Profile
* profile
,
414 const extensions::Extension
* extension
,
415 WindowOpenDisposition disposition
)
417 extension_id(extension
? extension
->id() : std::string()),
418 container(extensions::LAUNCH_CONTAINER_NONE
),
419 disposition(disposition
),
420 desktop_type(chrome::GetActiveDesktop()),
423 command_line(CommandLine::NO_PROGRAM
) {
424 // Look up the app preference to find out the right launch container. Default
425 // is to launch as a regular tab.
427 extensions::GetLaunchContainer(ExtensionPrefs::Get(profile
), extension
);
430 AppLaunchParams::AppLaunchParams(Profile
* profile
,
431 const extensions::Extension
* extension
,
433 chrome::HostDesktopType desktop_type
)
435 extension_id(extension
? extension
->id() : std::string()),
436 container(extensions::LAUNCH_CONTAINER_NONE
),
437 disposition(ui::DispositionFromEventFlags(event_flags
)),
438 desktop_type(desktop_type
),
441 command_line(CommandLine::NO_PROGRAM
) {
442 if (disposition
== NEW_FOREGROUND_TAB
|| disposition
== NEW_BACKGROUND_TAB
) {
443 container
= extensions::LAUNCH_CONTAINER_TAB
;
444 } else if (disposition
== NEW_WINDOW
) {
445 container
= extensions::LAUNCH_CONTAINER_WINDOW
;
447 // Look at preference to find the right launch container. If no preference
448 // is set, launch as a regular tab.
450 extensions::GetLaunchContainer(ExtensionPrefs::Get(profile
), extension
);
451 disposition
= NEW_FOREGROUND_TAB
;
455 AppLaunchParams::~AppLaunchParams() {
458 WebContents
* OpenApplication(const AppLaunchParams
& params
) {
459 return OpenEnabledApplication(params
);
462 void OpenApplicationWithReenablePrompt(const AppLaunchParams
& params
) {
463 const Extension
* extension
= GetExtension(params
);
466 Profile
* profile
= params
.profile
;
468 ExtensionService
* service
=
469 extensions::ExtensionSystem::Get(profile
)->extension_service();
470 if (!service
->IsExtensionEnabled(extension
->id()) ||
471 extensions::ExtensionRegistry::Get(profile
)->GetExtensionById(
472 extension
->id(), extensions::ExtensionRegistry::TERMINATED
)) {
473 (new EnableViaAppListFlow(
474 service
, profile
, params
.desktop_type
, extension
->id(),
475 base::Bind(base::IgnoreResult(OpenEnabledApplication
), params
)))->Run();
479 OpenEnabledApplication(params
);
482 WebContents
* OpenAppShortcutWindow(Profile
* profile
,
484 AppLaunchParams
launch_params(
486 NULL
, // this is a URL app. No extension.
487 extensions::LAUNCH_CONTAINER_WINDOW
,
489 launch_params
.override_url
= url
;
491 WebContents
* tab
= OpenApplicationWindow(launch_params
);
496 // Set UPDATE_SHORTCUT as the pending web app action. This action is picked
497 // up in LoadingStateChanged to schedule a GetApplicationInfo. And when
498 // the web app info is available, extensions::TabHelper notifies Browser via
499 // OnDidGetApplicationInfo, which calls
500 // web_app::UpdateShortcutForTabContents when it sees UPDATE_SHORTCUT as
501 // pending web app action.
502 extensions::TabHelper::FromWebContents(tab
)->set_pending_web_app_action(
503 extensions::TabHelper::UPDATE_SHORTCUT
);