Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / extensions / application_launch_web_app.cc
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.
4
5 #include "chrome/browser/ui/extensions/application_launch_web_app.h"
6
7 #include <string>
8
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"
31
32 #if defined(OS_MACOSX)
33 #include "chrome/browser/ui/browser_commands_mac.h"
34 #endif
35
36 using content::WebContents;
37 using extensions::Extension;
38 using extensions::ExtensionPrefs;
39 using extensions::ExtensionRegistry;
40
41 namespace {
42
43 const Extension* GetExtension(const AppLaunchParams& params) {
44   if (params.extension_id.empty())
45     return NULL;
46   ExtensionRegistry* registry = ExtensionRegistry::Get(params.profile);
47   return registry->GetExtensionById(params.extension_id,
48                                     ExtensionRegistry::ENABLED |
49                                         ExtensionRegistry::DISABLED |
50                                         ExtensionRegistry::TERMINATED);
51 }
52
53 ui::WindowShowState DetermineWindowShowState(
54     Profile* profile,
55     extensions::LaunchContainer container,
56     const Extension* extension) {
57   if (!extension || container != extensions::LAUNCH_CONTAINER_WINDOW)
58     return ui::SHOW_STATE_DEFAULT;
59
60   if (chrome::IsRunningInForcedAppMode())
61     return ui::SHOW_STATE_FULLSCREEN;
62
63 #if defined(USE_ASH)
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;
72 #endif
73
74   return ui::SHOW_STATE_DEFAULT;
75 }
76
77 }  // namespace
78
79 WebContents* OpenWebAppWindow(const AppLaunchParams& params, const GURL& url) {
80   Profile* const profile = params.profile;
81   const Extension* const extension = GetExtension(params);
82
83   std::string app_name = extension ?
84       web_app::GenerateApplicationNameFromExtensionId(extension->id()) :
85       web_app::GenerateApplicationNameFromURL(url);
86
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));
95   }
96
97   Browser::CreateParams browser_params(
98       Browser::CreateParams::CreateForApp(app_name,
99                                           true /* trusted_source */,
100                                           initial_bounds,
101                                           profile,
102                                           params.desktop_type));
103
104   browser_params.initial_show_state = DetermineWindowShowState(profile,
105                                                                params.container,
106                                                                extension);
107
108   Browser* browser = new Browser(browser_params);
109
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();
114
115   browser->window()->Show();
116
117   // TODO(jcampan): http://crbug.com/8123 we should not need to set the initial
118   //                focus explicitly.
119   web_contents->SetInitialFocus();
120   return web_contents;
121 }
122
123 WebContents* OpenWebAppTab(const AppLaunchParams& launch_params,
124                            const GURL& url) {
125   const Extension* extension = GetExtension(launch_params);
126   CHECK(extension);
127   Profile* const profile = launch_params.profile;
128   WindowOpenDisposition disposition = launch_params.disposition;
129
130   Browser* browser = chrome::FindTabbedBrowser(profile,
131                                                false,
132                                                launch_params.desktop_type);
133   WebContents* contents = NULL;
134   if (!browser) {
135     // No browser for this profile, need to open a new one.
136     browser = new Browser(Browser::CreateParams(Browser::TYPE_TABBED,
137                                                 profile,
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;
142   } else {
143     // For existing browser, ensure its window is shown and activated.
144     browser->window()->Show();
145     browser->window()->Activate();
146   }
147
148   extensions::LaunchType launch_type =
149       extensions::GetLaunchType(ExtensionPrefs::Get(profile), extension);
150   UMA_HISTOGRAM_ENUMERATION("Extensions.AppTabLaunchType", launch_type, 100);
151
152   int add_type = TabStripModel::ADD_ACTIVE;
153   if (launch_type == extensions::LAUNCH_TYPE_PINNED)
154     add_type |= TabStripModel::ADD_PINNED;
155
156   chrome::NavigateParams params(browser, url,
157                                 ui::PAGE_TRANSITION_AUTO_TOPLEVEL);
158   params.tabstrip_add_types = add_type;
159   params.disposition = disposition;
160
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);
166
167     existing_tab->OpenURL(content::OpenURLParams(
168           url,
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);
178     }
179     if (params.tabstrip_add_types & TabStripModel::ADD_ACTIVE)
180       model->ActivateTabAt(tab_index, true);
181
182     contents = existing_tab;
183   } else {
184     chrome::Navigate(&params);
185     contents = params.target_contents;
186   }
187
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
190   // in metro mode.
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);
195   } else {
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);
204 #else
205       chrome::ToggleFullscreenMode(browser);
206 #endif
207     }
208   }
209   return contents;
210 }