Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / metro_viewer / chrome_metro_viewer_process_host_aurawin.cc
blobee614c18e3aa165ba41160b305ae38c0b65f1536
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"
37 #include "url/gurl.h"
39 namespace {
41 void CloseOpenAshBrowsers() {
42 BrowserList* browser_list =
43 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH);
44 if (browser_list) {
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(),
60 GURL(url),
61 content::PAGE_TRANSITION_TYPED);
62 params.disposition = NEW_FOREGROUND_TAB;
63 params.host_desktop_type = chrome::HOST_DESKTOP_TYPE_ASH;
64 chrome::Navigate(&params);
67 } // namespace
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
82 // connected.
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();
93 chrome::CloseAsh();
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
109 // connected.
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 HWND hwnd = reinterpret_cast<HWND>(target_surface);
122 // Make hwnd available as early as possible for proper InputMethod
123 // initialization.
124 ash::AshRemoteWindowTreeHostWin::Init();
125 aura::RemoteWindowTreeHostWin::Instance()->SetRemoteWindowHandle(hwnd);
127 // Now start the Ash shell environment.
128 chrome::OpenAsh();
129 ash::Shell::GetInstance()->CreateShelf();
130 ash::Shell::GetInstance()->ShowShelf();
132 // Tell our root window host that the viewer has connected.
133 aura::RemoteWindowTreeHostWin::Instance()->Connected(this);
135 // On Windows 8 ASH we default to SHOW_STATE_MAXIMIZED for the browser
136 // window. This is to ensure that we honor metro app conventions by default.
137 ash::WindowPositioner::SetMaximizeFirstWindow(true);
138 // Tell the rest of Chrome that Ash is running.
139 content::NotificationService::current()->Notify(
140 chrome::NOTIFICATION_ASH_SESSION_STARTED,
141 content::NotificationService::AllSources(),
142 content::NotificationService::NoDetails());
145 void ChromeMetroViewerProcessHost::OnOpenURL(const base::string16& url) {
146 OpenURL(GURL(url));
149 void ChromeMetroViewerProcessHost::OnHandleSearchRequest(
150 const base::string16& search_string) {
151 GURL url(GetDefaultSearchURLForSearchTerms(
152 ProfileManager::GetActiveUserProfile(), search_string));
153 if (url.is_valid())
154 OpenURL(url);
157 void ChromeMetroViewerProcessHost::OnWindowSizeChanged(uint32 width,
158 uint32 height) {
159 std::vector<ash::DisplayInfo> info_list;
160 info_list.push_back(ash::DisplayInfo::CreateFromSpec(
161 base::StringPrintf("%dx%d*%f", width, height, gfx::GetModernUIScale())));
162 ash::Shell::GetInstance()->display_manager()->OnNativeDisplaysChanged(
163 info_list);
164 aura::RemoteWindowTreeHostWin::Instance()->HandleWindowSizeChanged(width,
165 height);