OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/extensions/application_launch.h" | 5 #include "chrome/browser/ui/extensions/application_launch_web_app.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "apps/launcher.h" | 9 #include "apps/launcher.h" |
10 #include "base/command_line.h" | |
11 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
12 #include "base/strings/utf_string_conversions.h" | |
13 #include "chrome/browser/app_mode/app_mode_utils.h" | 11 #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" | 12 #include "chrome/browser/extensions/launch_util.h" |
18 #include "chrome/browser/extensions/tab_helper.h" | |
19 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
20 #include "chrome/browser/ui/app_list/app_list_service.h" | |
21 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
22 #include "chrome/browser/ui/browser_commands.h" | 15 #include "chrome/browser/ui/browser_commands.h" |
23 #include "chrome/browser/ui/browser_finder.h" | 16 #include "chrome/browser/ui/browser_finder.h" |
24 #include "chrome/browser/ui/browser_tabstrip.h" | 17 #include "chrome/browser/ui/browser_tabstrip.h" |
25 #include "chrome/browser/ui/browser_window.h" | 18 #include "chrome/browser/ui/browser_window.h" |
26 #include "chrome/browser/ui/extensions/extension_enable_flow.h" | 19 #include "chrome/browser/ui/extensions/app_launch_params.h" |
27 #include "chrome/browser/ui/extensions/extension_enable_flow_delegate.h" | |
28 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
29 #include "chrome/browser/web_applications/web_app.h" | 21 #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" | 22 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" |
32 #include "chrome/common/url_constants.h" | |
33 #include "content/public/browser/render_view_host.h" | 23 #include "content/public/browser/render_view_host.h" |
34 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
35 #include "content/public/common/renderer_preferences.h" | 25 #include "content/public/common/renderer_preferences.h" |
36 #include "extensions/browser/extension_prefs.h" | 26 #include "extensions/browser/extension_prefs.h" |
37 #include "extensions/browser/extension_registry.h" | 27 #include "extensions/browser/extension_registry.h" |
38 #include "extensions/browser/extension_system.h" | |
39 #include "extensions/common/constants.h" | |
40 #include "extensions/common/extension.h" | 28 #include "extensions/common/extension.h" |
41 #include "extensions/common/features/feature.h" | |
42 #include "extensions/common/features/feature_provider.h" | |
43 #include "extensions/common/manifest_handlers/options_page_info.h" | |
44 #include "ui/base/window_open_disposition.h" | 29 #include "ui/base/window_open_disposition.h" |
45 #include "ui/gfx/rect.h" | 30 #include "ui/gfx/rect.h" |
46 | 31 |
47 #if defined(OS_MACOSX) | 32 #if defined(OS_MACOSX) |
48 #include "chrome/browser/ui/browser_commands_mac.h" | 33 #include "chrome/browser/ui/browser_commands_mac.h" |
49 #endif | 34 #endif |
50 | 35 |
51 using content::WebContents; | 36 using content::WebContents; |
52 using extensions::Extension; | 37 using extensions::Extension; |
53 using extensions::ExtensionPrefs; | 38 using extensions::ExtensionPrefs; |
54 using extensions::ExtensionRegistry; | 39 using extensions::ExtensionRegistry; |
55 | 40 |
56 namespace { | 41 namespace { |
57 | 42 |
58 // Attempts to launch a packaged app, prompting the user to enable it if | |
59 // necessary. If a prompt is required it will be shown inside the AppList. | |
60 // This class manages its own lifetime. | |
61 class EnableViaAppListFlow : public ExtensionEnableFlowDelegate { | |
62 public: | |
63 EnableViaAppListFlow(ExtensionService* service, | |
64 Profile* profile, | |
65 chrome::HostDesktopType desktop_type, | |
66 const std::string& extension_id, | |
67 const base::Closure& callback) | |
68 : service_(service), | |
69 profile_(profile), | |
70 desktop_type_(desktop_type), | |
71 extension_id_(extension_id), | |
72 callback_(callback) { | |
73 } | |
74 | |
75 virtual ~EnableViaAppListFlow() { | |
76 } | |
77 | |
78 void Run() { | |
79 DCHECK(!service_->IsExtensionEnabled(extension_id_)); | |
80 flow_.reset(new ExtensionEnableFlow(profile_, extension_id_, this)); | |
81 flow_->StartForCurrentlyNonexistentWindow( | |
82 base::Bind(&EnableViaAppListFlow::ShowAppList, base::Unretained(this))); | |
83 } | |
84 | |
85 private: | |
86 gfx::NativeWindow ShowAppList() { | |
87 AppListService* app_list_service = AppListService::Get(desktop_type_); | |
88 app_list_service->Show(); | |
89 return app_list_service->GetAppListWindow(); | |
90 } | |
91 | |
92 // ExtensionEnableFlowDelegate overrides. | |
93 virtual void ExtensionEnableFlowFinished() override { | |
94 const Extension* extension = | |
95 service_->GetExtensionById(extension_id_, false); | |
96 if (!extension) | |
97 return; | |
98 callback_.Run(); | |
99 delete this; | |
100 } | |
101 | |
102 virtual void ExtensionEnableFlowAborted(bool user_initiated) override { | |
103 delete this; | |
104 } | |
105 | |
106 ExtensionService* service_; | |
107 Profile* profile_; | |
108 chrome::HostDesktopType desktop_type_; | |
109 std::string extension_id_; | |
110 base::Closure callback_; | |
111 scoped_ptr<ExtensionEnableFlow> flow_; | |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(EnableViaAppListFlow); | |
114 }; | |
115 | |
116 const Extension* GetExtension(const AppLaunchParams& params) { | 43 const Extension* GetExtension(const AppLaunchParams& params) { |
117 if (params.extension_id.empty()) | 44 if (params.extension_id.empty()) |
118 return NULL; | 45 return NULL; |
119 ExtensionRegistry* registry = ExtensionRegistry::Get(params.profile); | 46 ExtensionRegistry* registry = ExtensionRegistry::Get(params.profile); |
120 return registry->GetExtensionById(params.extension_id, | 47 return registry->GetExtensionById(params.extension_id, |
121 ExtensionRegistry::ENABLED | | 48 ExtensionRegistry::ENABLED | |
122 ExtensionRegistry::DISABLED | | 49 ExtensionRegistry::DISABLED | |
123 ExtensionRegistry::TERMINATED); | 50 ExtensionRegistry::TERMINATED); |
124 } | 51 } |
125 | 52 |
(...skipping 14 matching lines...) Expand all Loading... |
140 extensions::GetLaunchType(ExtensionPrefs::Get(profile), extension); | 67 extensions::GetLaunchType(ExtensionPrefs::Get(profile), extension); |
141 if (launch_type == extensions::LAUNCH_TYPE_FULLSCREEN) | 68 if (launch_type == extensions::LAUNCH_TYPE_FULLSCREEN) |
142 return ui::SHOW_STATE_MAXIMIZED; | 69 return ui::SHOW_STATE_MAXIMIZED; |
143 else if (launch_type == extensions::LAUNCH_TYPE_WINDOW) | 70 else if (launch_type == extensions::LAUNCH_TYPE_WINDOW) |
144 return ui::SHOW_STATE_NORMAL; | 71 return ui::SHOW_STATE_NORMAL; |
145 #endif | 72 #endif |
146 | 73 |
147 return ui::SHOW_STATE_DEFAULT; | 74 return ui::SHOW_STATE_DEFAULT; |
148 } | 75 } |
149 | 76 |
150 WebContents* OpenApplicationWindow(const AppLaunchParams& params) { | 77 } // namespace |
| 78 |
| 79 WebContents* OpenWebAppWindow(const AppLaunchParams& params, const GURL& url) { |
151 Profile* const profile = params.profile; | 80 Profile* const profile = params.profile; |
152 const Extension* const extension = GetExtension(params); | 81 const Extension* const extension = GetExtension(params); |
153 const GURL url_input = params.override_url; | |
154 | 82 |
155 DCHECK(!url_input.is_empty() || extension); | |
156 GURL url = UrlForExtension(extension, url_input); | |
157 std::string app_name = extension ? | 83 std::string app_name = extension ? |
158 web_app::GenerateApplicationNameFromExtensionId(extension->id()) : | 84 web_app::GenerateApplicationNameFromExtensionId(extension->id()) : |
159 web_app::GenerateApplicationNameFromURL(url); | 85 web_app::GenerateApplicationNameFromURL(url); |
160 | 86 |
161 gfx::Rect initial_bounds; | 87 gfx::Rect initial_bounds; |
162 if (!params.override_bounds.IsEmpty()) { | 88 if (!params.override_bounds.IsEmpty()) { |
163 initial_bounds = params.override_bounds; | 89 initial_bounds = params.override_bounds; |
164 } else if (extension) { | 90 } else if (extension) { |
165 initial_bounds.set_width( | 91 initial_bounds.set_width( |
166 extensions::AppLaunchInfo::GetLaunchWidth(extension)); | 92 extensions::AppLaunchInfo::GetLaunchWidth(extension)); |
(...skipping 20 matching lines...) Expand all Loading... |
187 web_contents->GetRenderViewHost()->SyncRendererPrefs(); | 113 web_contents->GetRenderViewHost()->SyncRendererPrefs(); |
188 | 114 |
189 browser->window()->Show(); | 115 browser->window()->Show(); |
190 | 116 |
191 // TODO(jcampan): http://crbug.com/8123 we should not need to set the initial | 117 // TODO(jcampan): http://crbug.com/8123 we should not need to set the initial |
192 // focus explicitly. | 118 // focus explicitly. |
193 web_contents->SetInitialFocus(); | 119 web_contents->SetInitialFocus(); |
194 return web_contents; | 120 return web_contents; |
195 } | 121 } |
196 | 122 |
197 WebContents* OpenApplicationTab(const AppLaunchParams& launch_params) { | 123 WebContents* OpenWebAppTab(const AppLaunchParams& launch_params, |
| 124 const GURL& url) { |
198 const Extension* extension = GetExtension(launch_params); | 125 const Extension* extension = GetExtension(launch_params); |
199 CHECK(extension); | 126 CHECK(extension); |
200 Profile* const profile = launch_params.profile; | 127 Profile* const profile = launch_params.profile; |
201 WindowOpenDisposition disposition = launch_params.disposition; | 128 WindowOpenDisposition disposition = launch_params.disposition; |
202 | 129 |
203 Browser* browser = chrome::FindTabbedBrowser(profile, | 130 Browser* browser = chrome::FindTabbedBrowser(profile, |
204 false, | 131 false, |
205 launch_params.desktop_type); | 132 launch_params.desktop_type); |
206 WebContents* contents = NULL; | 133 WebContents* contents = NULL; |
207 if (!browser) { | 134 if (!browser) { |
(...skipping 11 matching lines...) Expand all Loading... |
219 } | 146 } |
220 | 147 |
221 extensions::LaunchType launch_type = | 148 extensions::LaunchType launch_type = |
222 extensions::GetLaunchType(ExtensionPrefs::Get(profile), extension); | 149 extensions::GetLaunchType(ExtensionPrefs::Get(profile), extension); |
223 UMA_HISTOGRAM_ENUMERATION("Extensions.AppTabLaunchType", launch_type, 100); | 150 UMA_HISTOGRAM_ENUMERATION("Extensions.AppTabLaunchType", launch_type, 100); |
224 | 151 |
225 int add_type = TabStripModel::ADD_ACTIVE; | 152 int add_type = TabStripModel::ADD_ACTIVE; |
226 if (launch_type == extensions::LAUNCH_TYPE_PINNED) | 153 if (launch_type == extensions::LAUNCH_TYPE_PINNED) |
227 add_type |= TabStripModel::ADD_PINNED; | 154 add_type |= TabStripModel::ADD_PINNED; |
228 | 155 |
229 GURL extension_url = UrlForExtension(extension, launch_params.override_url); | 156 chrome::NavigateParams params(browser, url, |
230 chrome::NavigateParams params(browser, extension_url, | |
231 ui::PAGE_TRANSITION_AUTO_TOPLEVEL); | 157 ui::PAGE_TRANSITION_AUTO_TOPLEVEL); |
232 params.tabstrip_add_types = add_type; | 158 params.tabstrip_add_types = add_type; |
233 params.disposition = disposition; | 159 params.disposition = disposition; |
234 | 160 |
235 if (disposition == CURRENT_TAB) { | 161 if (disposition == CURRENT_TAB) { |
236 WebContents* existing_tab = | 162 WebContents* existing_tab = |
237 browser->tab_strip_model()->GetActiveWebContents(); | 163 browser->tab_strip_model()->GetActiveWebContents(); |
238 TabStripModel* model = browser->tab_strip_model(); | 164 TabStripModel* model = browser->tab_strip_model(); |
239 int tab_index = model->GetIndexOfWebContents(existing_tab); | 165 int tab_index = model->GetIndexOfWebContents(existing_tab); |
240 | 166 |
241 existing_tab->OpenURL(content::OpenURLParams( | 167 existing_tab->OpenURL(content::OpenURLParams( |
242 extension_url, | 168 url, |
243 content::Referrer(existing_tab->GetURL(), | 169 content::Referrer(existing_tab->GetURL(), |
244 blink::WebReferrerPolicyDefault), | 170 blink::WebReferrerPolicyDefault), |
245 disposition, ui::PAGE_TRANSITION_LINK, false)); | 171 disposition, ui::PAGE_TRANSITION_LINK, false)); |
246 // Reset existing_tab as OpenURL() may have clobbered it. | 172 // Reset existing_tab as OpenURL() may have clobbered it. |
247 existing_tab = browser->tab_strip_model()->GetActiveWebContents(); | 173 existing_tab = browser->tab_strip_model()->GetActiveWebContents(); |
248 if (params.tabstrip_add_types & TabStripModel::ADD_PINNED) { | 174 if (params.tabstrip_add_types & TabStripModel::ADD_PINNED) { |
249 model->SetTabPinned(tab_index, true); | 175 model->SetTabPinned(tab_index, true); |
250 // Pinning may have moved the tab. | 176 // Pinning may have moved the tab. |
251 tab_index = model->GetIndexOfWebContents(existing_tab); | 177 tab_index = model->GetIndexOfWebContents(existing_tab); |
252 } | 178 } |
(...skipping 22 matching lines...) Expand all Loading... |
275 !browser->window()->IsFullscreen()) { | 201 !browser->window()->IsFullscreen()) { |
276 #if defined(OS_MACOSX) | 202 #if defined(OS_MACOSX) |
277 chrome::ToggleFullscreenWithChromeOrFallback(browser); | 203 chrome::ToggleFullscreenWithChromeOrFallback(browser); |
278 #else | 204 #else |
279 chrome::ToggleFullscreenMode(browser); | 205 chrome::ToggleFullscreenMode(browser); |
280 #endif | 206 #endif |
281 } | 207 } |
282 } | 208 } |
283 return contents; | 209 return contents; |
284 } | 210 } |
285 | |
286 WebContents* OpenEnabledApplication(const AppLaunchParams& params) { | |
287 const Extension* extension = GetExtension(params); | |
288 if (!extension) | |
289 return NULL; | |
290 Profile* profile = params.profile; | |
291 | |
292 WebContents* tab = NULL; | |
293 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile); | |
294 prefs->SetActiveBit(extension->id(), true); | |
295 | |
296 UMA_HISTOGRAM_ENUMERATION( | |
297 "Extensions.AppLaunchContainer", params.container, 100); | |
298 | |
299 if (CanLaunchViaEvent(extension)) { | |
300 // Remember what desktop the launch happened on so that when the app opens a | |
301 // window we can open them on the right desktop. | |
302 PerAppSettingsServiceFactory::GetForBrowserContext(profile)-> | |
303 SetDesktopLastLaunchedFrom(extension->id(), params.desktop_type); | |
304 | |
305 apps::LaunchPlatformAppWithCommandLine( | |
306 profile, extension, params.command_line, params.current_directory); | |
307 return NULL; | |
308 } | |
309 | |
310 // Record v1 app launch. Platform app launch is recorded when dispatching | |
311 // the onLaunched event. | |
312 prefs->SetLastLaunchTime(extension->id(), base::Time::Now()); | |
313 | |
314 switch (params.container) { | |
315 case extensions::LAUNCH_CONTAINER_NONE: { | |
316 NOTREACHED(); | |
317 break; | |
318 } | |
319 case extensions::LAUNCH_CONTAINER_PANEL: | |
320 case extensions::LAUNCH_CONTAINER_WINDOW: | |
321 tab = OpenApplicationWindow(params); | |
322 break; | |
323 case extensions::LAUNCH_CONTAINER_TAB: { | |
324 tab = OpenApplicationTab(params); | |
325 break; | |
326 } | |
327 default: | |
328 NOTREACHED(); | |
329 break; | |
330 } | |
331 return tab; | |
332 } | |
333 | |
334 } // namespace | |
335 | |
336 AppLaunchParams::AppLaunchParams(Profile* profile, | |
337 const extensions::Extension* extension, | |
338 extensions::LaunchContainer container, | |
339 WindowOpenDisposition disposition) | |
340 : profile(profile), | |
341 extension_id(extension ? extension->id() : std::string()), | |
342 container(container), | |
343 disposition(disposition), | |
344 desktop_type(chrome::GetActiveDesktop()), | |
345 override_url(), | |
346 override_bounds(), | |
347 command_line(CommandLine::NO_PROGRAM) {} | |
348 | |
349 AppLaunchParams::AppLaunchParams(Profile* profile, | |
350 const extensions::Extension* extension, | |
351 WindowOpenDisposition disposition) | |
352 : profile(profile), | |
353 extension_id(extension ? extension->id() : std::string()), | |
354 container(extensions::LAUNCH_CONTAINER_NONE), | |
355 disposition(disposition), | |
356 desktop_type(chrome::GetActiveDesktop()), | |
357 override_url(), | |
358 override_bounds(), | |
359 command_line(CommandLine::NO_PROGRAM) { | |
360 // Look up the app preference to find out the right launch container. Default | |
361 // is to launch as a regular tab. | |
362 container = | |
363 extensions::GetLaunchContainer(ExtensionPrefs::Get(profile), extension); | |
364 } | |
365 | |
366 AppLaunchParams::AppLaunchParams(Profile* profile, | |
367 const extensions::Extension* extension, | |
368 int event_flags, | |
369 chrome::HostDesktopType desktop_type) | |
370 : profile(profile), | |
371 extension_id(extension ? extension->id() : std::string()), | |
372 container(extensions::LAUNCH_CONTAINER_NONE), | |
373 disposition(ui::DispositionFromEventFlags(event_flags)), | |
374 desktop_type(desktop_type), | |
375 override_url(), | |
376 override_bounds(), | |
377 command_line(CommandLine::NO_PROGRAM) { | |
378 if (disposition == NEW_FOREGROUND_TAB || disposition == NEW_BACKGROUND_TAB) { | |
379 container = extensions::LAUNCH_CONTAINER_TAB; | |
380 } else if (disposition == NEW_WINDOW) { | |
381 container = extensions::LAUNCH_CONTAINER_WINDOW; | |
382 } else { | |
383 // Look at preference to find the right launch container. If no preference | |
384 // is set, launch as a regular tab. | |
385 container = | |
386 extensions::GetLaunchContainer(ExtensionPrefs::Get(profile), extension); | |
387 disposition = NEW_FOREGROUND_TAB; | |
388 } | |
389 } | |
390 | |
391 AppLaunchParams::~AppLaunchParams() { | |
392 } | |
393 | |
394 WebContents* OpenApplication(const AppLaunchParams& params) { | |
395 return OpenEnabledApplication(params); | |
396 } | |
397 | |
398 void OpenApplicationWithReenablePrompt(const AppLaunchParams& params) { | |
399 const Extension* extension = GetExtension(params); | |
400 if (!extension) | |
401 return; | |
402 Profile* profile = params.profile; | |
403 | |
404 ExtensionService* service = | |
405 extensions::ExtensionSystem::Get(profile)->extension_service(); | |
406 if (!service->IsExtensionEnabled(extension->id()) || | |
407 extensions::ExtensionRegistry::Get(profile)->GetExtensionById( | |
408 extension->id(), extensions::ExtensionRegistry::TERMINATED)) { | |
409 (new EnableViaAppListFlow( | |
410 service, profile, params.desktop_type, extension->id(), | |
411 base::Bind(base::IgnoreResult(OpenEnabledApplication), params)))->Run(); | |
412 return; | |
413 } | |
414 | |
415 OpenEnabledApplication(params); | |
416 } | |
417 | |
418 WebContents* OpenAppShortcutWindow(Profile* profile, | |
419 const GURL& url) { | |
420 AppLaunchParams launch_params( | |
421 profile, | |
422 NULL, // this is a URL app. No extension. | |
423 extensions::LAUNCH_CONTAINER_WINDOW, | |
424 NEW_WINDOW); | |
425 launch_params.override_url = url; | |
426 | |
427 WebContents* tab = OpenApplicationWindow(launch_params); | |
428 | |
429 if (!tab) | |
430 return NULL; | |
431 | |
432 extensions::TabHelper::FromWebContents(tab)->UpdateShortcutOnLoadComplete(); | |
433 | |
434 return tab; | |
435 } | |
436 | |
437 bool CanLaunchViaEvent(const extensions::Extension* extension) { | |
438 const extensions::FeatureProvider* feature_provider = | |
439 extensions::FeatureProvider::GetAPIFeatures(); | |
440 extensions::Feature* feature = feature_provider->GetFeature("app.runtime"); | |
441 return feature->IsAvailableToExtension(extension).is_available(); | |
442 } | |
443 | |
444 GURL UrlForExtension(const Extension* extension, const GURL& override_url) { | |
445 if (!extension) | |
446 return override_url; | |
447 | |
448 GURL url; | |
449 if (!override_url.is_empty()) { | |
450 DCHECK(extension->web_extent().MatchesURL(override_url) || | |
451 override_url.GetOrigin() == extension->url()); | |
452 url = override_url; | |
453 } else { | |
454 url = extensions::AppLaunchInfo::GetFullLaunchURL(extension); | |
455 } | |
456 | |
457 // For extensions lacking launch urls, determine a reasonable fallback. | |
458 if (!url.is_valid()) { | |
459 url = extensions::OptionsPageInfo::GetOptionsPage(extension); | |
460 if (!url.is_valid()) | |
461 url = GURL(chrome::kChromeUIExtensionsURL); | |
462 } | |
463 | |
464 return url; | |
465 } | |
OLD | NEW |