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. 4 5 #include "chrome/browser/metro_viewer/chrome_metro_viewer_process_host_aurawin.h" 6 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" 39 #include "url/gurl.h" 40 41 namespace { 42 43 void CloseOpenAshBrowsers() { 44 BrowserList* browser_list = 45 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); 46 if (browser_list) { 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); 55 } 56 } 57 } 58 59 void OpenURL(const GURL& url) { 60 chrome::NavigateParams params( 61 ProfileManager::GetActiveUserProfile(), 62 GURL(url), 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); 67 } 68 69 } // namespace 70 71 ChromeMetroViewerProcessHost::ChromeMetroViewerProcessHost() 72 : MetroViewerProcessHost( 73 content::BrowserThread::GetMessageLoopProxyForThread( 74 content::BrowserThread::IO)) { 75 chrome::IncrementKeepAliveCount(); 76 } 77 78 ChromeMetroViewerProcessHost::~ChromeMetroViewerProcessHost() { 79 } 80 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"; 85 86 // Unset environment variable to let breakpad know that metro process wasn't 87 // connected. 88 ::SetEnvironmentVariableA(env_vars::kMetroConnected, NULL); 89 90 aura::RemoteWindowTreeHostWin::Instance()->Disconnected(); 91 chrome::DecrementKeepAliveCount(); 92 93 // If browser is trying to quit, we shouldn't reenter the process. 94 // TODO(shrikant): In general there seem to be issues with how AttemptExit 95 // reentry works. In future release please clean up related code. 96 if (!browser_shutdown::IsTryingToQuit()) { 97 CloseOpenAshBrowsers(); 98 chrome::CloseAsh(); 99 } 100 // Tell the rest of Chrome about it. 101 content::NotificationService::current()->Notify( 102 chrome::NOTIFICATION_ASH_SESSION_ENDED, 103 content::NotificationService::AllSources(), 104 content::NotificationService::NoDetails()); 105 106 // This will delete the MetroViewerProcessHost object. Don't access member 107 // variables/functions after this call. 108 g_browser_process->platform_part()->OnMetroViewerProcessTerminated(); 109 } 110 111 void ChromeMetroViewerProcessHost::OnChannelConnected(int32 /*peer_pid*/) { 112 DVLOG(1) << "ChromeMetroViewerProcessHost::OnChannelConnected: "; 113 // Set environment variable to let breakpad know that metro process was 114 // connected. 115 ::SetEnvironmentVariableA(env_vars::kMetroConnected, "1"); 116 117 if (!content::GpuDataManager::GetInstance()->GpuAccessAllowed(NULL)) { 118 DVLOG(1) << "No GPU access, attempting to restart in Desktop\n"; 119 chrome::AttemptRestartToDesktopMode(); 120 } 121 } 122 123 void ChromeMetroViewerProcessHost::OnSetTargetSurface( 124 gfx::NativeViewId target_surface, 125 float device_scale) { 126 HWND hwnd = reinterpret_cast<HWND>(target_surface); 127 128 gfx::InitDeviceScaleFactor(device_scale); 129 chrome::OpenAsh(hwnd); 130 DCHECK(aura::RemoteWindowTreeHostWin::Instance()); 131 DCHECK_EQ(hwnd, aura::RemoteWindowTreeHostWin::Instance()->remote_window()); 132 ash::Shell::GetInstance()->CreateShelf(); 133 ash::Shell::GetInstance()->ShowShelf(); 134 135 // Tell our root window host that the viewer has connected. 136 aura::RemoteWindowTreeHostWin::Instance()->Connected(this); 137 138 // On Windows 8 ASH we default to SHOW_STATE_MAXIMIZED for the browser 139 // window. This is to ensure that we honor metro app conventions by default. 140 ash::WindowPositioner::SetMaximizeFirstWindow(true); 141 // Tell the rest of Chrome that Ash is running. 142 content::NotificationService::current()->Notify( 143 chrome::NOTIFICATION_ASH_SESSION_STARTED, 144 content::NotificationService::AllSources(), 145 content::NotificationService::NoDetails()); 146 } 147 148 void ChromeMetroViewerProcessHost::OnOpenURL(const base::string16& url) { 149 OpenURL(GURL(url)); 150 } 151 152 void ChromeMetroViewerProcessHost::OnHandleSearchRequest( 153 const base::string16& search_string) { 154 GURL url(GetDefaultSearchURLForSearchTerms( 155 TemplateURLServiceFactory::GetForProfile( 156 ProfileManager::GetActiveUserProfile()), search_string)); 157 if (url.is_valid()) 158 OpenURL(url); 159 } 160 161 void ChromeMetroViewerProcessHost::OnWindowSizeChanged(uint32 width, 162 uint32 height) { 163 std::vector<ash::DisplayInfo> info_list; 164 info_list.push_back(ash::DisplayInfo::CreateFromSpec( 165 base::StringPrintf("%dx%d*%f", width, height, gfx::GetDPIScale()))); 166 ash::Shell::GetInstance()->display_manager()->OnNativeDisplaysChanged( 167 info_list); 168 aura::RemoteWindowTreeHostWin::Instance()->HandleWindowSizeChanged(width, 169 height); 170 } 171