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/histogram.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/app_mode/app_mode_utils.h"
14 #include "chrome/browser/apps/per_app_settings_service.h"
15 #include "chrome/browser/apps/per_app_settings_service_factory.h"
16 #include "chrome/browser/extensions/extension_service.h"
17 #include "chrome/browser/extensions/launch_util.h"
18 #include "chrome/browser/extensions/tab_helper.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/ui/app_list/app_list_service.h"
21 #include "chrome/browser/ui/browser.h"
22 #include "chrome/browser/ui/browser_commands.h"
23 #include "chrome/browser/ui/browser_finder.h"
24 #include "chrome/browser/ui/browser_tabstrip.h"
25 #include "chrome/browser/ui/browser_window.h"
26 #include "chrome/browser/ui/extensions/extension_enable_flow.h"
27 #include "chrome/browser/ui/extensions/extension_enable_flow_delegate.h"
28 #include "chrome/browser/ui/tabs/tab_strip_model.h"
29 #include "chrome/browser/web_applications/web_app.h"
30 #include "chrome/common/chrome_switches.h"
31 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
32 #include "chrome/common/extensions/manifest_url_handler.h"
33 #include "chrome/common/url_constants.h"
34 #include "content/public/browser/render_view_host.h"
35 #include "content/public/browser/web_contents.h"
36 #include "content/public/common/renderer_preferences.h"
37 #include "extensions/browser/extension_prefs.h"
38 #include "extensions/browser/extension_registry.h"
39 #include "extensions/browser/extension_system.h"
40 #include "extensions/common/constants.h"
41 #include "extensions/common/extension.h"
42 #include "extensions/common/features/feature.h"
43 #include "extensions/common/features/feature_provider.h"
44 #include "grit/generated_resources.h"
45 #include "ui/base/window_open_disposition.h"
46 #include "ui/gfx/rect.h"
48 #if defined(OS_MACOSX)
49 #include "chrome/browser/ui/browser_commands_mac.h"
52 using content::WebContents
;
53 using extensions::Extension
;
54 using extensions::ExtensionPrefs
;
55 using extensions::ExtensionRegistry
;
59 // Attempts to launch a packaged app, prompting the user to enable it if
60 // necessary. If a prompt is required it will be shown inside the AppList.
61 // This class manages its own lifetime.
62 class EnableViaAppListFlow
: public ExtensionEnableFlowDelegate
{
64 EnableViaAppListFlow(ExtensionService
* service
,
66 chrome::HostDesktopType desktop_type
,
67 const std::string
& extension_id
,
68 const base::Closure
& callback
)
71 desktop_type_(desktop_type
),
72 extension_id_(extension_id
),
76 virtual ~EnableViaAppListFlow() {
80 DCHECK(!service_
->IsExtensionEnabled(extension_id_
));
81 flow_
.reset(new ExtensionEnableFlow(profile_
, extension_id_
, this));
82 flow_
->StartForCurrentlyNonexistentWindow(
83 base::Bind(&EnableViaAppListFlow::ShowAppList
, base::Unretained(this)));
87 gfx::NativeWindow
ShowAppList() {
88 AppListService
* app_list_service
= AppListService::Get(desktop_type_
);
89 app_list_service
->Show();
90 return app_list_service
->GetAppListWindow();
93 // ExtensionEnableFlowDelegate overrides.
94 virtual void ExtensionEnableFlowFinished() OVERRIDE
{
95 const Extension
* extension
=
96 service_
->GetExtensionById(extension_id_
, false);
103 virtual void ExtensionEnableFlowAborted(bool user_initiated
) OVERRIDE
{
107 ExtensionService
* service_
;
109 chrome::HostDesktopType desktop_type_
;
110 std::string extension_id_
;
111 base::Closure callback_
;
112 scoped_ptr
<ExtensionEnableFlow
> flow_
;
114 DISALLOW_COPY_AND_ASSIGN(EnableViaAppListFlow
);
117 const Extension
* GetExtension(const AppLaunchParams
& params
) {
118 if (params
.extension_id
.empty())
120 ExtensionRegistry
* registry
= ExtensionRegistry::Get(params
.profile
);
121 return registry
->GetExtensionById(params
.extension_id
,
122 ExtensionRegistry::ENABLED
|
123 ExtensionRegistry::DISABLED
|
124 ExtensionRegistry::TERMINATED
);
127 // Get the launch URL for a given extension, with optional override/fallback.
128 // |override_url|, if non-empty, will be preferred over the extension's
130 GURL
UrlForExtension(const Extension
* extension
,
131 const GURL
& override_url
) {
136 if (!override_url
.is_empty()) {
137 DCHECK(extension
->web_extent().MatchesURL(override_url
) ||
138 override_url
.GetOrigin() == extension
->url());
141 url
= extensions::AppLaunchInfo::GetFullLaunchURL(extension
);
144 // For extensions lacking launch urls, determine a reasonable fallback.
145 if (!url
.is_valid()) {
146 url
= extensions::ManifestURL::GetOptionsPage(extension
);
148 url
= GURL(chrome::kChromeUIExtensionsURL
);
154 ui::WindowShowState
DetermineWindowShowState(
156 extensions::LaunchContainer container
,
157 const Extension
* extension
) {
158 if (!extension
|| container
!= extensions::LAUNCH_CONTAINER_WINDOW
)
159 return ui::SHOW_STATE_DEFAULT
;
161 if (chrome::IsRunningInForcedAppMode())
162 return ui::SHOW_STATE_FULLSCREEN
;
165 // In ash, LAUNCH_TYPE_FULLSCREEN launches in a maximized app window and
166 // LAUNCH_TYPE_WINDOW launches in a normal app window.
167 extensions::LaunchType launch_type
=
168 extensions::GetLaunchType(ExtensionPrefs::Get(profile
), extension
);
169 if (launch_type
== extensions::LAUNCH_TYPE_FULLSCREEN
)
170 return ui::SHOW_STATE_MAXIMIZED
;
171 else if (launch_type
== extensions::LAUNCH_TYPE_WINDOW
)
172 return ui::SHOW_STATE_NORMAL
;
175 return ui::SHOW_STATE_DEFAULT
;
178 WebContents
* OpenApplicationWindow(const AppLaunchParams
& params
) {
179 Profile
* const profile
= params
.profile
;
180 const Extension
* const extension
= GetExtension(params
);
181 const GURL url_input
= params
.override_url
;
183 DCHECK(!url_input
.is_empty() || extension
);
184 GURL url
= UrlForExtension(extension
, url_input
);
185 std::string app_name
= extension
?
186 web_app::GenerateApplicationNameFromExtensionId(extension
->id()) :
187 web_app::GenerateApplicationNameFromURL(url
);
189 gfx::Rect initial_bounds
;
190 if (!params
.override_bounds
.IsEmpty()) {
191 initial_bounds
= params
.override_bounds
;
192 } else if (extension
) {
193 initial_bounds
.set_width(
194 extensions::AppLaunchInfo::GetLaunchWidth(extension
));
195 initial_bounds
.set_height(
196 extensions::AppLaunchInfo::GetLaunchHeight(extension
));
199 Browser::CreateParams
browser_params(
200 Browser::CreateParams::CreateForApp(app_name
,
201 true /* trusted_source */,
204 params
.desktop_type
));
206 browser_params
.initial_show_state
= DetermineWindowShowState(profile
,
210 Browser
* browser
= new Browser(browser_params
);
212 WebContents
* web_contents
= chrome::AddSelectedTabWithURL(
213 browser
, url
, content::PAGE_TRANSITION_AUTO_TOPLEVEL
);
214 web_contents
->GetMutableRendererPrefs()->can_accept_load_drops
= false;
215 web_contents
->GetRenderViewHost()->SyncRendererPrefs();
217 browser
->window()->Show();
219 // TODO(jcampan): http://crbug.com/8123 we should not need to set the initial
221 web_contents
->SetInitialFocus();
225 WebContents
* OpenApplicationTab(const AppLaunchParams
& launch_params
) {
226 const Extension
* extension
= GetExtension(launch_params
);
228 Profile
* const profile
= launch_params
.profile
;
229 WindowOpenDisposition disposition
= launch_params
.disposition
;
231 Browser
* browser
= chrome::FindTabbedBrowser(profile
,
233 launch_params
.desktop_type
);
234 WebContents
* contents
= NULL
;
236 // No browser for this profile, need to open a new one.
237 browser
= new Browser(Browser::CreateParams(Browser::TYPE_TABBED
,
239 launch_params
.desktop_type
));
240 browser
->window()->Show();
241 // There's no current tab in this browser window, so add a new one.
242 disposition
= NEW_FOREGROUND_TAB
;
244 // For existing browser, ensure its window is shown and activated.
245 browser
->window()->Show();
246 browser
->window()->Activate();
249 extensions::LaunchType launch_type
=
250 extensions::GetLaunchType(ExtensionPrefs::Get(profile
), extension
);
251 UMA_HISTOGRAM_ENUMERATION("Extensions.AppTabLaunchType", launch_type
, 100);
253 int add_type
= TabStripModel::ADD_ACTIVE
;
254 if (launch_type
== extensions::LAUNCH_TYPE_PINNED
)
255 add_type
|= TabStripModel::ADD_PINNED
;
257 GURL extension_url
= UrlForExtension(extension
, launch_params
.override_url
);
258 chrome::NavigateParams
params(browser
, extension_url
,
259 content::PAGE_TRANSITION_AUTO_TOPLEVEL
);
260 params
.tabstrip_add_types
= add_type
;
261 params
.disposition
= disposition
;
263 if (disposition
== CURRENT_TAB
) {
264 WebContents
* existing_tab
=
265 browser
->tab_strip_model()->GetActiveWebContents();
266 TabStripModel
* model
= browser
->tab_strip_model();
267 int tab_index
= model
->GetIndexOfWebContents(existing_tab
);
269 existing_tab
->OpenURL(content::OpenURLParams(
271 content::Referrer(existing_tab
->GetURL(),
272 blink::WebReferrerPolicyDefault
),
273 disposition
, content::PAGE_TRANSITION_LINK
, false));
274 // Reset existing_tab as OpenURL() may have clobbered it.
275 existing_tab
= browser
->tab_strip_model()->GetActiveWebContents();
276 if (params
.tabstrip_add_types
& TabStripModel::ADD_PINNED
) {
277 model
->SetTabPinned(tab_index
, true);
278 // Pinning may have moved the tab.
279 tab_index
= model
->GetIndexOfWebContents(existing_tab
);
281 if (params
.tabstrip_add_types
& TabStripModel::ADD_ACTIVE
)
282 model
->ActivateTabAt(tab_index
, true);
284 contents
= existing_tab
;
286 chrome::Navigate(¶ms
);
287 contents
= params
.target_contents
;
290 // On Chrome OS the host desktop type for a browser window is always set to
291 // HOST_DESKTOP_TYPE_ASH. On Windows 8 it is only the case for Chrome ASH
293 if (browser
->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH
) {
294 // In ash, LAUNCH_FULLSCREEN launches in the OpenApplicationWindow function
295 // i.e. it should not reach here.
296 DCHECK(launch_type
!= extensions::LAUNCH_TYPE_FULLSCREEN
);
298 // TODO(skerner): If we are already in full screen mode, and the user
299 // set the app to open as a regular or pinned tab, what should happen?
300 // Today we open the tab, but stay in full screen mode. Should we leave
301 // full screen mode in this case?
302 if (launch_type
== extensions::LAUNCH_TYPE_FULLSCREEN
&&
303 !browser
->window()->IsFullscreen()) {
304 #if defined(OS_MACOSX)
305 chrome::ToggleFullscreenWithChromeOrFallback(browser
);
307 chrome::ToggleFullscreenMode(browser
);
314 WebContents
* OpenEnabledApplication(const AppLaunchParams
& params
) {
315 const Extension
* extension
= GetExtension(params
);
318 Profile
* profile
= params
.profile
;
320 WebContents
* tab
= NULL
;
321 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile
);
322 prefs
->SetActiveBit(extension
->id(), true);
324 UMA_HISTOGRAM_ENUMERATION(
325 "Extensions.AppLaunchContainer", params
.container
, 100);
327 if (CanLaunchViaEvent(extension
)) {
328 // Remember what desktop the launch happened on so that when the app opens a
329 // window we can open them on the right desktop.
330 PerAppSettingsServiceFactory::GetForBrowserContext(profile
)->
331 SetDesktopLastLaunchedFrom(extension
->id(), params
.desktop_type
);
333 apps::LaunchPlatformAppWithCommandLine(
334 profile
, extension
, params
.command_line
, params
.current_directory
);
338 // Record v1 app launch. Platform app launch is recorded when dispatching
339 // the onLaunched event.
340 prefs
->SetLastLaunchTime(extension
->id(), base::Time::Now());
342 switch (params
.container
) {
343 case extensions::LAUNCH_CONTAINER_NONE
: {
347 case extensions::LAUNCH_CONTAINER_PANEL
:
348 case extensions::LAUNCH_CONTAINER_WINDOW
:
349 tab
= OpenApplicationWindow(params
);
351 case extensions::LAUNCH_CONTAINER_TAB
: {
352 tab
= OpenApplicationTab(params
);
364 AppLaunchParams::AppLaunchParams(Profile
* profile
,
365 const extensions::Extension
* extension
,
366 extensions::LaunchContainer container
,
367 WindowOpenDisposition disposition
)
369 extension_id(extension
? extension
->id() : std::string()),
370 container(container
),
371 disposition(disposition
),
372 desktop_type(chrome::GetActiveDesktop()),
375 command_line(CommandLine::NO_PROGRAM
) {}
377 AppLaunchParams::AppLaunchParams(Profile
* profile
,
378 const extensions::Extension
* extension
,
379 WindowOpenDisposition disposition
)
381 extension_id(extension
? extension
->id() : std::string()),
382 container(extensions::LAUNCH_CONTAINER_NONE
),
383 disposition(disposition
),
384 desktop_type(chrome::GetActiveDesktop()),
387 command_line(CommandLine::NO_PROGRAM
) {
388 // Look up the app preference to find out the right launch container. Default
389 // is to launch as a regular tab.
391 extensions::GetLaunchContainer(ExtensionPrefs::Get(profile
), extension
);
394 AppLaunchParams::AppLaunchParams(Profile
* profile
,
395 const extensions::Extension
* extension
,
397 chrome::HostDesktopType desktop_type
)
399 extension_id(extension
? extension
->id() : std::string()),
400 container(extensions::LAUNCH_CONTAINER_NONE
),
401 disposition(ui::DispositionFromEventFlags(event_flags
)),
402 desktop_type(desktop_type
),
405 command_line(CommandLine::NO_PROGRAM
) {
406 if (disposition
== NEW_FOREGROUND_TAB
|| disposition
== NEW_BACKGROUND_TAB
) {
407 container
= extensions::LAUNCH_CONTAINER_TAB
;
408 } else if (disposition
== NEW_WINDOW
) {
409 container
= extensions::LAUNCH_CONTAINER_WINDOW
;
411 // Look at preference to find the right launch container. If no preference
412 // is set, launch as a regular tab.
414 extensions::GetLaunchContainer(ExtensionPrefs::Get(profile
), extension
);
415 disposition
= NEW_FOREGROUND_TAB
;
419 AppLaunchParams::~AppLaunchParams() {
422 WebContents
* OpenApplication(const AppLaunchParams
& params
) {
423 return OpenEnabledApplication(params
);
426 void OpenApplicationWithReenablePrompt(const AppLaunchParams
& params
) {
427 const Extension
* extension
= GetExtension(params
);
430 Profile
* profile
= params
.profile
;
432 ExtensionService
* service
=
433 extensions::ExtensionSystem::Get(profile
)->extension_service();
434 if (!service
->IsExtensionEnabled(extension
->id()) ||
435 extensions::ExtensionRegistry::Get(profile
)->GetExtensionById(
436 extension
->id(), extensions::ExtensionRegistry::TERMINATED
)) {
437 (new EnableViaAppListFlow(
438 service
, profile
, params
.desktop_type
, extension
->id(),
439 base::Bind(base::IgnoreResult(OpenEnabledApplication
), params
)))->Run();
443 OpenEnabledApplication(params
);
446 WebContents
* OpenAppShortcutWindow(Profile
* profile
,
448 AppLaunchParams
launch_params(
450 NULL
, // this is a URL app. No extension.
451 extensions::LAUNCH_CONTAINER_WINDOW
,
453 launch_params
.override_url
= url
;
455 WebContents
* tab
= OpenApplicationWindow(launch_params
);
460 // Set UPDATE_SHORTCUT as the pending web app action. This action is picked
461 // up in LoadingStateChanged to schedule a GetApplicationInfo. And when
462 // the web app info is available, extensions::TabHelper notifies Browser via
463 // OnDidGetApplicationInfo, which calls
464 // web_app::UpdateShortcutForTabContents when it sees UPDATE_SHORTCUT as
465 // pending web app action.
466 extensions::TabHelper::FromWebContents(tab
)->set_pending_web_app_action(
467 extensions::TabHelper::UPDATE_SHORTCUT
);
472 bool CanLaunchViaEvent(const extensions::Extension
* extension
) {
473 const extensions::FeatureProvider
* feature_provider
=
474 extensions::FeatureProvider::GetAPIFeatures();
475 extensions::Feature
* feature
= feature_provider
->GetFeature("app.runtime");
476 return feature
->IsAvailableToExtension(extension
).is_available();