1 // Copyright 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/metro_viewer/chrome_metro_viewer_process_host_aurawin.h"
7 #include "ash/display/display_info.h"
8 #include "ash/display/display_manager.h"
9 #include "ash/host/ash_remote_window_tree_host_win.h"
10 #include "ash/shell.h"
11 #include "ash/wm/window_positioner.h"
12 #include "base/logging.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/strings/stringprintf.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/browser_process_platform_part_aurawin.h"
17 #include "chrome/browser/browser_shutdown.h"
18 #include "chrome/browser/chrome_notification_types.h"
19 #include "chrome/browser/lifetime/application_lifetime.h"
20 #include "chrome/browser/profiles/profile_manager.h"
21 #include "chrome/browser/search_engines/template_url_service_factory.h"
22 #include "chrome/browser/ui/ash/ash_init.h"
23 #include "chrome/browser/ui/browser.h"
24 #include "chrome/browser/ui/browser_list.h"
25 #include "chrome/browser/ui/browser_navigator.h"
26 #include "chrome/browser/ui/browser_window.h"
27 #include "chrome/browser/ui/host_desktop.h"
28 #include "chrome/browser/ui/tabs/tab_strip_model.h"
29 #include "chrome/common/env_vars.h"
30 #include "components/search_engines/util.h"
31 #include "content/public/browser/browser_thread.h"
32 #include "content/public/browser/gpu_data_manager.h"
33 #include "content/public/browser/notification_service.h"
34 #include "content/public/browser/page_navigator.h"
35 #include "content/public/browser/web_contents.h"
36 #include "ui/aura/remote_window_tree_host_win.h"
37 #include "ui/gfx/win/dpi.h"
38 #include "ui/metro_viewer/metro_viewer_messages.h"
43 void CloseOpenAshBrowsers() {
44 BrowserList
* browser_list
=
45 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH
);
47 for (BrowserList::const_iterator i
= browser_list
->begin();
48 i
!= browser_list
->end(); ++i
) {
49 Browser
* browser
= *i
;
50 browser
->window()->Close();
51 // If the attempt to Close the browser fails due to unload handlers on
52 // the page or in progress downloads, etc, destroy all tabs on the page.
53 while (browser
->tab_strip_model()->count())
54 delete browser
->tab_strip_model()->GetWebContentsAt(0);
59 void OpenURL(const GURL
& url
) {
60 chrome::NavigateParams
params(
61 ProfileManager::GetActiveUserProfile(),
63 ui::PAGE_TRANSITION_TYPED
);
64 params
.disposition
= NEW_FOREGROUND_TAB
;
65 params
.host_desktop_type
= chrome::HOST_DESKTOP_TYPE_ASH
;
66 chrome::Navigate(¶ms
);
71 ChromeMetroViewerProcessHost::ChromeMetroViewerProcessHost()
72 : MetroViewerProcessHost(
73 content::BrowserThread::GetMessageLoopProxyForThread(
74 content::BrowserThread::IO
)) {
75 chrome::IncrementKeepAliveCount();
78 ChromeMetroViewerProcessHost::~ChromeMetroViewerProcessHost() {
81 void ChromeMetroViewerProcessHost::OnChannelError() {
82 // TODO(cpu): At some point we only close the browser. Right now this
83 // is very convenient for developing.
84 DVLOG(1) << "viewer channel error : Quitting browser";
86 // Unset environment variable to let breakpad know that metro process wasn't
88 ::SetEnvironmentVariableA(env_vars::kMetroConnected
, NULL
);
90 // It seems possible that channel is connected, but ASH desktop is not yet
91 // created (instance is still NULL) and we receive channel error.
92 if (aura::RemoteWindowTreeHostWin::Instance()) {
93 aura::RemoteWindowTreeHostWin::Instance()->Disconnected();
95 chrome::DecrementKeepAliveCount();
97 // If browser is trying to quit, we shouldn't reenter the process.
98 // TODO(shrikant): In general there seem to be issues with how AttemptExit
99 // reentry works. In future release please clean up related code.
100 if (!browser_shutdown::IsTryingToQuit()) {
101 CloseOpenAshBrowsers();
104 // Tell the rest of Chrome about it.
105 content::NotificationService::current()->Notify(
106 chrome::NOTIFICATION_ASH_SESSION_ENDED
,
107 content::NotificationService::AllSources(),
108 content::NotificationService::NoDetails());
112 chrome::DecrementKeepAliveCount();
114 // This will delete the MetroViewerProcessHost object. Don't access member
115 // variables/functions after this call.
116 g_browser_process
->platform_part()->OnMetroViewerProcessTerminated();
119 void ChromeMetroViewerProcessHost::OnChannelConnected(int32
/*peer_pid*/) {
120 DVLOG(1) << "ChromeMetroViewerProcessHost::OnChannelConnected: ";
121 // Set environment variable to let breakpad know that metro process was
123 ::SetEnvironmentVariableA(env_vars::kMetroConnected
, "1");
125 if (!content::GpuDataManager::GetInstance()->GpuAccessAllowed(NULL
)) {
126 DVLOG(1) << "No GPU access, attempting to restart in Desktop\n";
127 chrome::AttemptRestartToDesktopMode();
131 void ChromeMetroViewerProcessHost::OnSetTargetSurface(
132 gfx::NativeViewId target_surface
,
133 float device_scale
) {
134 HWND hwnd
= reinterpret_cast<HWND
>(target_surface
);
136 gfx::InitDeviceScaleFactor(device_scale
);
137 chrome::OpenAsh(hwnd
);
138 DCHECK(aura::RemoteWindowTreeHostWin::Instance());
139 DCHECK_EQ(hwnd
, aura::RemoteWindowTreeHostWin::Instance()->remote_window());
140 ash::Shell::GetInstance()->CreateShelf();
141 ash::Shell::GetInstance()->ShowShelf();
143 // Tell our root window host that the viewer has connected.
144 aura::RemoteWindowTreeHostWin::Instance()->Connected(this);
146 // On Windows 8 ASH we default to SHOW_STATE_MAXIMIZED for the browser
147 // window. This is to ensure that we honor metro app conventions by default.
148 ash::WindowPositioner::SetMaximizeFirstWindow(true);
149 // Tell the rest of Chrome that Ash is running.
150 content::NotificationService::current()->Notify(
151 chrome::NOTIFICATION_ASH_SESSION_STARTED
,
152 content::NotificationService::AllSources(),
153 content::NotificationService::NoDetails());
156 void ChromeMetroViewerProcessHost::OnOpenURL(const base::string16
& url
) {
160 void ChromeMetroViewerProcessHost::OnHandleSearchRequest(
161 const base::string16
& search_string
) {
162 GURL
url(GetDefaultSearchURLForSearchTerms(
163 TemplateURLServiceFactory::GetForProfile(
164 ProfileManager::GetActiveUserProfile()), search_string
));
169 void ChromeMetroViewerProcessHost::OnWindowSizeChanged(uint32 width
,
171 std::vector
<ash::DisplayInfo
> info_list
;
172 info_list
.push_back(ash::DisplayInfo::CreateFromSpec(
173 base::StringPrintf("%dx%d*%f", width
, height
, gfx::GetDPIScale())));
174 ash::Shell::GetInstance()->display_manager()->OnNativeDisplaysChanged(
176 aura::RemoteWindowTreeHostWin::Instance()->HandleWindowSizeChanged(width
,