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/util.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 "content/public/browser/browser_thread.h"
31 #include "content/public/browser/gpu_data_manager.h"
32 #include "content/public/browser/notification_service.h"
33 #include "content/public/browser/page_navigator.h"
34 #include "content/public/browser/web_contents.h"
35 #include "ui/aura/remote_window_tree_host_win.h"
36 #include "ui/gfx/win/dpi.h"
41 void CloseOpenAshBrowsers() {
42 BrowserList
* browser_list
=
43 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH
);
45 for (BrowserList::const_iterator i
= browser_list
->begin();
46 i
!= browser_list
->end(); ++i
) {
47 Browser
* browser
= *i
;
48 browser
->window()->Close();
49 // If the attempt to Close the browser fails due to unload handlers on
50 // the page or in progress downloads, etc, destroy all tabs on the page.
51 while (browser
->tab_strip_model()->count())
52 delete browser
->tab_strip_model()->GetWebContentsAt(0);
57 void OpenURL(const GURL
& url
) {
58 chrome::NavigateParams
params(
59 ProfileManager::GetActiveUserProfile(),
61 content::PAGE_TRANSITION_TYPED
);
62 params
.disposition
= NEW_FOREGROUND_TAB
;
63 params
.host_desktop_type
= chrome::HOST_DESKTOP_TYPE_ASH
;
64 chrome::Navigate(¶ms
);
69 ChromeMetroViewerProcessHost::ChromeMetroViewerProcessHost()
70 : MetroViewerProcessHost(
71 content::BrowserThread::GetMessageLoopProxyForThread(
72 content::BrowserThread::IO
)) {
73 chrome::IncrementKeepAliveCount();
76 void ChromeMetroViewerProcessHost::OnChannelError() {
77 // TODO(cpu): At some point we only close the browser. Right now this
78 // is very convenient for developing.
79 DVLOG(1) << "viewer channel error : Quitting browser";
81 // Unset environment variable to let breakpad know that metro process wasn't
83 ::SetEnvironmentVariableA(env_vars::kMetroConnected
, NULL
);
85 aura::RemoteWindowTreeHostWin::Instance()->Disconnected();
86 chrome::DecrementKeepAliveCount();
88 // If browser is trying to quit, we shouldn't reenter the process.
89 // TODO(shrikant): In general there seem to be issues with how AttemptExit
90 // reentry works. In future release please clean up related code.
91 if (!browser_shutdown::IsTryingToQuit()) {
92 CloseOpenAshBrowsers();
95 // Tell the rest of Chrome about it.
96 content::NotificationService::current()->Notify(
97 chrome::NOTIFICATION_ASH_SESSION_ENDED
,
98 content::NotificationService::AllSources(),
99 content::NotificationService::NoDetails());
101 // This will delete the MetroViewerProcessHost object. Don't access member
102 // variables/functions after this call.
103 g_browser_process
->platform_part()->OnMetroViewerProcessTerminated();
106 void ChromeMetroViewerProcessHost::OnChannelConnected(int32
/*peer_pid*/) {
107 DVLOG(1) << "ChromeMetroViewerProcessHost::OnChannelConnected: ";
108 // Set environment variable to let breakpad know that metro process was
110 ::SetEnvironmentVariableA(env_vars::kMetroConnected
, "1");
112 if (!content::GpuDataManager::GetInstance()->GpuAccessAllowed(NULL
)) {
113 DVLOG(1) << "No GPU access, attempting to restart in Desktop\n";
114 chrome::AttemptRestartToDesktopMode();
118 void ChromeMetroViewerProcessHost::OnSetTargetSurface(
119 gfx::NativeViewId target_surface
,
120 float device_scale
) {
121 HWND hwnd
= reinterpret_cast<HWND
>(target_surface
);
123 gfx::InitDeviceScaleFactor(device_scale
);
124 chrome::OpenAsh(hwnd
);
125 DCHECK(aura::RemoteWindowTreeHostWin::Instance());
126 DCHECK_EQ(hwnd
, aura::RemoteWindowTreeHostWin::Instance()->remote_window());
127 ash::Shell::GetInstance()->CreateShelf();
128 ash::Shell::GetInstance()->ShowShelf();
130 // Tell our root window host that the viewer has connected.
131 aura::RemoteWindowTreeHostWin::Instance()->Connected(this);
133 // On Windows 8 ASH we default to SHOW_STATE_MAXIMIZED for the browser
134 // window. This is to ensure that we honor metro app conventions by default.
135 ash::WindowPositioner::SetMaximizeFirstWindow(true);
136 // Tell the rest of Chrome that Ash is running.
137 content::NotificationService::current()->Notify(
138 chrome::NOTIFICATION_ASH_SESSION_STARTED
,
139 content::NotificationService::AllSources(),
140 content::NotificationService::NoDetails());
143 void ChromeMetroViewerProcessHost::OnOpenURL(const base::string16
& url
) {
147 void ChromeMetroViewerProcessHost::OnHandleSearchRequest(
148 const base::string16
& search_string
) {
149 GURL
url(GetDefaultSearchURLForSearchTerms(
150 ProfileManager::GetActiveUserProfile(), search_string
));
155 void ChromeMetroViewerProcessHost::OnWindowSizeChanged(uint32 width
,
157 std::vector
<ash::DisplayInfo
> info_list
;
158 info_list
.push_back(ash::DisplayInfo::CreateFromSpec(
159 base::StringPrintf("%dx%d*%f", width
, height
, gfx::GetDPIScale())));
160 ash::Shell::GetInstance()->display_manager()->OnNativeDisplaysChanged(
162 aura::RemoteWindowTreeHostWin::Instance()->HandleWindowSizeChanged(width
,