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/metrics/histogram.h"
11 #include "chrome/browser/app_mode/app_mode_utils.h"
12 #include "chrome/browser/apps/per_app_settings_service.h"
13 #include "chrome/browser/apps/per_app_settings_service_factory.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/launch_util.h"
16 #include "chrome/browser/extensions/tab_helper.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/app_list/app_list_service.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_commands.h"
21 #include "chrome/browser/ui/browser_finder.h"
22 #include "chrome/browser/ui/browser_tabstrip.h"
23 #include "chrome/browser/ui/browser_window.h"
24 #include "chrome/browser/ui/extensions/app_launch_params.h"
25 #include "chrome/browser/ui/extensions/extension_enable_flow.h"
26 #include "chrome/browser/ui/extensions/extension_enable_flow_delegate.h"
27 #include "chrome/browser/ui/tabs/tab_strip_model.h"
28 #include "chrome/browser/web_applications/web_app.h"
29 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
30 #include "chrome/common/url_constants.h"
31 #include "content/public/browser/render_view_host.h"
32 #include "content/public/browser/web_contents.h"
33 #include "content/public/common/renderer_preferences.h"
34 #include "extensions/browser/extension_prefs.h"
35 #include "extensions/browser/extension_registry.h"
36 #include "extensions/browser/extension_system.h"
37 #include "extensions/common/constants.h"
38 #include "extensions/common/extension.h"
39 #include "extensions/common/features/feature.h"
40 #include "extensions/common/features/feature_provider.h"
41 #include "extensions/common/manifest_handlers/options_page_info.h"
42 #include "ui/base/window_open_disposition.h"
43 #include "ui/gfx/geometry/rect.h"
45 #if defined(OS_MACOSX)
46 #include "chrome/browser/ui/browser_commands_mac.h"
49 using content::WebContents
;
50 using extensions::Extension
;
51 using extensions::ExtensionPrefs
;
52 using extensions::ExtensionRegistry
;
56 // Shows the app list for |desktop_type| and returns the app list's window.
57 gfx::NativeWindow
ShowAppListAndGetNativeWindow(
58 chrome::HostDesktopType desktop_type
) {
59 AppListService
* app_list_service
= AppListService::Get(desktop_type
);
60 app_list_service
->Show();
61 return app_list_service
->GetAppListWindow();
64 // Attempts to launch an app, prompting the user to enable it if necessary. If
65 // a prompt is required it will be shown inside the window returned by
66 // |parent_window_getter|.
67 // This class manages its own lifetime.
68 class EnableViaDialogFlow
: public ExtensionEnableFlowDelegate
{
71 ExtensionService
* service
,
73 const std::string
& extension_id
,
74 const base::Callback
<gfx::NativeWindow(void)>& parent_window_getter
,
75 const base::Closure
& callback
)
78 extension_id_(extension_id
),
79 parent_window_getter_(parent_window_getter
),
83 ~EnableViaDialogFlow() override
{}
86 DCHECK(!service_
->IsExtensionEnabled(extension_id_
));
87 flow_
.reset(new ExtensionEnableFlow(profile_
, extension_id_
, this));
88 flow_
->StartForCurrentlyNonexistentWindow(parent_window_getter_
);
92 // ExtensionEnableFlowDelegate overrides.
93 void ExtensionEnableFlowFinished() override
{
94 const Extension
* extension
=
95 service_
->GetExtensionById(extension_id_
, false);
102 void ExtensionEnableFlowAborted(bool user_initiated
) override
{ delete this; }
104 ExtensionService
* service_
;
106 std::string extension_id_
;
107 base::Callback
<gfx::NativeWindow(void)> parent_window_getter_
;
108 base::Closure callback_
;
109 scoped_ptr
<ExtensionEnableFlow
> flow_
;
111 DISALLOW_COPY_AND_ASSIGN(EnableViaDialogFlow
);
114 const Extension
* GetExtension(const AppLaunchParams
& params
) {
115 if (params
.extension_id
.empty())
117 ExtensionRegistry
* registry
= ExtensionRegistry::Get(params
.profile
);
118 return registry
->GetExtensionById(params
.extension_id
,
119 ExtensionRegistry::ENABLED
|
120 ExtensionRegistry::DISABLED
|
121 ExtensionRegistry::TERMINATED
);
124 // Get the launch URL for a given extension, with optional override/fallback.
125 // |override_url|, if non-empty, will be preferred over the extension's
127 GURL
UrlForExtension(const extensions::Extension
* extension
,
128 const GURL
& override_url
) {
133 if (!override_url
.is_empty()) {
134 DCHECK(extension
->web_extent().MatchesURL(override_url
) ||
135 override_url
.GetOrigin() == extension
->url());
138 url
= extensions::AppLaunchInfo::GetFullLaunchURL(extension
);
141 // For extensions lacking launch urls, determine a reasonable fallback.
142 if (!url
.is_valid()) {
143 url
= extensions::OptionsPageInfo::GetOptionsPage(extension
);
145 url
= GURL(chrome::kChromeUIExtensionsURL
);
151 ui::WindowShowState
DetermineWindowShowState(
153 extensions::LaunchContainer container
,
154 const Extension
* extension
) {
155 if (!extension
|| container
!= extensions::LAUNCH_CONTAINER_WINDOW
)
156 return ui::SHOW_STATE_DEFAULT
;
158 if (chrome::IsRunningInForcedAppMode())
159 return ui::SHOW_STATE_FULLSCREEN
;
162 // In ash, LAUNCH_TYPE_FULLSCREEN launches in a maximized app window and
163 // LAUNCH_TYPE_WINDOW launches in a normal app window.
164 extensions::LaunchType launch_type
=
165 extensions::GetLaunchType(ExtensionPrefs::Get(profile
), extension
);
166 if (launch_type
== extensions::LAUNCH_TYPE_FULLSCREEN
)
167 return ui::SHOW_STATE_MAXIMIZED
;
168 else if (launch_type
== extensions::LAUNCH_TYPE_WINDOW
)
169 return ui::SHOW_STATE_NORMAL
;
172 return ui::SHOW_STATE_DEFAULT
;
175 WebContents
* OpenApplicationWindow(const AppLaunchParams
& params
,
177 Profile
* const profile
= params
.profile
;
178 const Extension
* const extension
= GetExtension(params
);
180 std::string app_name
= extension
?
181 web_app::GenerateApplicationNameFromExtensionId(extension
->id()) :
182 web_app::GenerateApplicationNameFromURL(url
);
184 gfx::Rect initial_bounds
;
185 if (!params
.override_bounds
.IsEmpty()) {
186 initial_bounds
= params
.override_bounds
;
187 } else if (extension
) {
188 initial_bounds
.set_width(
189 extensions::AppLaunchInfo::GetLaunchWidth(extension
));
190 initial_bounds
.set_height(
191 extensions::AppLaunchInfo::GetLaunchHeight(extension
));
194 Browser::CreateParams
browser_params(
195 Browser::CreateParams::CreateForApp(app_name
,
196 true /* trusted_source */,
199 params
.desktop_type
));
201 browser_params
.initial_show_state
= DetermineWindowShowState(profile
,
205 Browser
* browser
= new Browser(browser_params
);
207 WebContents
* web_contents
= chrome::AddSelectedTabWithURL(
208 browser
, url
, ui::PAGE_TRANSITION_AUTO_TOPLEVEL
);
209 web_contents
->GetMutableRendererPrefs()->can_accept_load_drops
= false;
210 web_contents
->GetRenderViewHost()->SyncRendererPrefs();
212 browser
->window()->Show();
214 // TODO(jcampan): http://crbug.com/8123 we should not need to set the initial
216 web_contents
->SetInitialFocus();
220 WebContents
* OpenApplicationTab(const AppLaunchParams
& launch_params
,
222 const Extension
* extension
= GetExtension(launch_params
);
224 Profile
* const profile
= launch_params
.profile
;
225 WindowOpenDisposition disposition
= launch_params
.disposition
;
227 Browser
* browser
= chrome::FindTabbedBrowser(profile
,
229 launch_params
.desktop_type
);
230 WebContents
* contents
= NULL
;
232 // No browser for this profile, need to open a new one.
233 browser
= new Browser(Browser::CreateParams(Browser::TYPE_TABBED
,
235 launch_params
.desktop_type
));
236 browser
->window()->Show();
237 // There's no current tab in this browser window, so add a new one.
238 disposition
= NEW_FOREGROUND_TAB
;
240 // For existing browser, ensure its window is shown and activated.
241 browser
->window()->Show();
242 browser
->window()->Activate();
245 extensions::LaunchType launch_type
=
246 extensions::GetLaunchType(ExtensionPrefs::Get(profile
), extension
);
247 UMA_HISTOGRAM_ENUMERATION("Extensions.AppTabLaunchType", launch_type
, 100);
249 int add_type
= TabStripModel::ADD_ACTIVE
;
250 if (launch_type
== extensions::LAUNCH_TYPE_PINNED
)
251 add_type
|= TabStripModel::ADD_PINNED
;
253 chrome::NavigateParams
params(browser
, url
,
254 ui::PAGE_TRANSITION_AUTO_TOPLEVEL
);
255 params
.tabstrip_add_types
= add_type
;
256 params
.disposition
= disposition
;
258 if (disposition
== CURRENT_TAB
) {
259 WebContents
* existing_tab
=
260 browser
->tab_strip_model()->GetActiveWebContents();
261 TabStripModel
* model
= browser
->tab_strip_model();
262 int tab_index
= model
->GetIndexOfWebContents(existing_tab
);
264 existing_tab
->OpenURL(content::OpenURLParams(
266 content::Referrer(existing_tab
->GetURL(),
267 blink::WebReferrerPolicyDefault
),
268 disposition
, ui::PAGE_TRANSITION_LINK
, false));
269 // Reset existing_tab as OpenURL() may have clobbered it.
270 existing_tab
= browser
->tab_strip_model()->GetActiveWebContents();
271 if (params
.tabstrip_add_types
& TabStripModel::ADD_PINNED
) {
272 model
->SetTabPinned(tab_index
, true);
273 // Pinning may have moved the tab.
274 tab_index
= model
->GetIndexOfWebContents(existing_tab
);
276 if (params
.tabstrip_add_types
& TabStripModel::ADD_ACTIVE
)
277 model
->ActivateTabAt(tab_index
, true);
279 contents
= existing_tab
;
281 chrome::Navigate(¶ms
);
282 contents
= params
.target_contents
;
285 // On Chrome OS the host desktop type for a browser window is always set to
286 // HOST_DESKTOP_TYPE_ASH. On Windows 8 it is only the case for Chrome ASH
288 if (browser
->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH
) {
289 // In ash, LAUNCH_FULLSCREEN launches in the OpenApplicationWindow function
290 // i.e. it should not reach here.
291 DCHECK(launch_type
!= extensions::LAUNCH_TYPE_FULLSCREEN
);
293 // TODO(skerner): If we are already in full screen mode, and the user
294 // set the app to open as a regular or pinned tab, what should happen?
295 // Today we open the tab, but stay in full screen mode. Should we leave
296 // full screen mode in this case?
297 if (launch_type
== extensions::LAUNCH_TYPE_FULLSCREEN
&&
298 !browser
->window()->IsFullscreen()) {
299 #if defined(OS_MACOSX)
300 chrome::ToggleFullscreenWithToolbarOrFallback(browser
);
302 chrome::ToggleFullscreenMode(browser
);
309 WebContents
* OpenEnabledApplication(const AppLaunchParams
& params
) {
310 const Extension
* extension
= GetExtension(params
);
313 Profile
* profile
= params
.profile
;
315 WebContents
* tab
= NULL
;
316 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile
);
317 prefs
->SetActiveBit(extension
->id(), true);
319 if (CanLaunchViaEvent(extension
)) {
320 // Remember what desktop the launch happened on so that when the app opens a
321 // window we can open them on the right desktop.
322 PerAppSettingsServiceFactory::GetForBrowserContext(profile
)->
323 SetDesktopLastLaunchedFrom(extension
->id(), params
.desktop_type
);
325 apps::LaunchPlatformAppWithCommandLine(profile
,
328 params
.current_directory
,
333 UMA_HISTOGRAM_ENUMERATION("Extensions.HostedAppLaunchContainer",
335 extensions::NUM_LAUNCH_CONTAINERS
);
337 // Record v1 app launch. Platform app launch is recorded when dispatching
338 // the onLaunched event.
339 prefs
->SetLastLaunchTime(extension
->id(), base::Time::Now());
341 GURL url
= UrlForExtension(extension
, params
.override_url
);
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
, url
);
351 case extensions::LAUNCH_CONTAINER_TAB
: {
352 tab
= OpenApplicationTab(params
, url
);
364 WebContents
* OpenApplication(const AppLaunchParams
& params
) {
365 return OpenEnabledApplication(params
);
368 void OpenApplicationWithReenablePrompt(const AppLaunchParams
& params
) {
369 const Extension
* extension
= GetExtension(params
);
372 Profile
* profile
= params
.profile
;
374 ExtensionService
* service
=
375 extensions::ExtensionSystem::Get(profile
)->extension_service();
376 if (!service
->IsExtensionEnabled(extension
->id()) ||
377 extensions::ExtensionRegistry::Get(profile
)->GetExtensionById(
378 extension
->id(), extensions::ExtensionRegistry::TERMINATED
)) {
379 base::Callback
<gfx::NativeWindow(void)> dialog_parent_window_getter
;
380 // TODO(pkotwicz): Figure out which window should be used as the parent for
381 // the "enable application" dialog in Athena.
382 dialog_parent_window_getter
=
383 base::Bind(&ShowAppListAndGetNativeWindow
, params
.desktop_type
);
384 (new EnableViaDialogFlow(
385 service
, profile
, extension
->id(), dialog_parent_window_getter
,
386 base::Bind(base::IgnoreResult(OpenEnabledApplication
), params
)))->Run();
390 OpenEnabledApplication(params
);
393 WebContents
* OpenAppShortcutWindow(Profile
* profile
,
395 AppLaunchParams
launch_params(profile
,
396 NULL
, // this is a URL app. No extension.
397 extensions::LAUNCH_CONTAINER_WINDOW
, NEW_WINDOW
,
398 extensions::SOURCE_COMMAND_LINE
);
399 launch_params
.override_url
= url
;
401 WebContents
* tab
= OpenApplicationWindow(launch_params
, url
);
406 extensions::TabHelper::FromWebContents(tab
)->UpdateShortcutOnLoadComplete();
411 bool CanLaunchViaEvent(const extensions::Extension
* extension
) {
412 const extensions::Feature
* feature
=
413 extensions::FeatureProvider::GetAPIFeature("app.runtime");
414 return feature
->IsAvailableToExtension(extension
).is_available();