Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / metro_viewer / chrome_metro_viewer_process_host_aurawin.cc
blob99713515a0ccdc6cb1d5f07a0537514e2288cfa5
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/shell.h"
10 #include "ash/wm/window_positioner.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/strings/stringprintf.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/browser_process_platform_part_aurawin.h"
16 #include "chrome/browser/browser_shutdown.h"
17 #include "chrome/browser/chrome_notification_types.h"
18 #include "chrome/browser/lifetime/application_lifetime.h"
19 #include "chrome/browser/profiles/profile_manager.h"
20 #include "chrome/browser/search_engines/util.h"
21 #include "chrome/browser/ui/ash/ash_init.h"
22 #include "chrome/browser/ui/browser.h"
23 #include "chrome/browser/ui/browser_list.h"
24 #include "chrome/browser/ui/browser_navigator.h"
25 #include "chrome/browser/ui/browser_window.h"
26 #include "chrome/browser/ui/host_desktop.h"
27 #include "chrome/browser/ui/tabs/tab_strip_model.h"
28 #include "chrome/common/env_vars.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/gpu_data_manager.h"
31 #include "content/public/browser/notification_service.h"
32 #include "content/public/browser/page_navigator.h"
33 #include "content/public/browser/web_contents.h"
34 #include "ui/aura/remote_root_window_host_win.h"
35 #include "ui/surface/accelerated_surface_win.h"
36 #include "url/gurl.h"
38 namespace {
40 void CloseOpenAshBrowsers() {
41 BrowserList* browser_list =
42 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH);
43 if (browser_list) {
44 for (BrowserList::const_iterator i = browser_list->begin();
45 i != browser_list->end(); ++i) {
46 Browser* browser = *i;
47 browser->window()->Close();
48 // If the attempt to Close the browser fails due to unload handlers on
49 // the page or in progress downloads, etc, destroy all tabs on the page.
50 while (browser->tab_strip_model()->count())
51 delete browser->tab_strip_model()->GetWebContentsAt(0);
56 void OpenURL(const GURL& url) {
57 chrome::NavigateParams params(
58 ProfileManager::GetActiveUserProfile(),
59 GURL(url),
60 content::PAGE_TRANSITION_TYPED);
61 params.disposition = NEW_FOREGROUND_TAB;
62 params.host_desktop_type = chrome::HOST_DESKTOP_TYPE_ASH;
63 chrome::Navigate(&params);
66 } // namespace
68 ChromeMetroViewerProcessHost::ChromeMetroViewerProcessHost()
69 : MetroViewerProcessHost(
70 content::BrowserThread::GetMessageLoopProxyForThread(
71 content::BrowserThread::IO)) {
72 g_browser_process->AddRefModule();
75 void ChromeMetroViewerProcessHost::OnChannelError() {
76 // TODO(cpu): At some point we only close the browser. Right now this
77 // is very convenient for developing.
78 DVLOG(1) << "viewer channel error : Quitting browser";
80 // Unset environment variable to let breakpad know that metro process wasn't
81 // connected.
82 ::SetEnvironmentVariableA(env_vars::kMetroConnected, NULL);
84 aura::RemoteWindowTreeHostWin::Instance()->Disconnected();
85 g_browser_process->ReleaseModule();
87 // If browser is trying to quit, we shouldn't reenter the process.
88 // TODO(shrikant): In general there seem to be issues with how AttemptExit
89 // reentry works. In future release please clean up related code.
90 if (!browser_shutdown::IsTryingToQuit()) {
91 CloseOpenAshBrowsers();
92 chrome::CloseAsh();
94 // Tell the rest of Chrome about it.
95 content::NotificationService::current()->Notify(
96 chrome::NOTIFICATION_ASH_SESSION_ENDED,
97 content::NotificationService::AllSources(),
98 content::NotificationService::NoDetails());
100 // This will delete the MetroViewerProcessHost object. Don't access member
101 // variables/functions after this call.
102 g_browser_process->platform_part()->OnMetroViewerProcessTerminated();
105 void ChromeMetroViewerProcessHost::OnChannelConnected(int32 /*peer_pid*/) {
106 DVLOG(1) << "ChromeMetroViewerProcessHost::OnChannelConnected: ";
107 // Set environment variable to let breakpad know that metro process was
108 // connected.
109 ::SetEnvironmentVariableA(env_vars::kMetroConnected, "1");
111 if (!content::GpuDataManager::GetInstance()->GpuAccessAllowed(NULL)) {
112 DVLOG(1) << "No GPU access, attempting to restart in Desktop\n";
113 chrome::AttemptRestartToDesktopMode();
117 void ChromeMetroViewerProcessHost::OnSetTargetSurface(
118 gfx::NativeViewId target_surface) {
119 // Now start the Ash shell environment.
120 chrome::OpenAsh();
121 ash::Shell::GetInstance()->CreateShelf();
122 ash::Shell::GetInstance()->ShowShelf();
124 HWND hwnd = reinterpret_cast<HWND>(target_surface);
125 // Tell our root window host that the viewer has connected.
126 aura::RemoteWindowTreeHostWin::Instance()->Connected(this, hwnd);
128 // On Windows 8 ASH we default to SHOW_STATE_MAXIMIZED for the browser
129 // window. This is to ensure that we honor metro app conventions by default.
130 ash::WindowPositioner::SetMaximizeFirstWindow(true);
131 // Tell the rest of Chrome that Ash is running.
132 content::NotificationService::current()->Notify(
133 chrome::NOTIFICATION_ASH_SESSION_STARTED,
134 content::NotificationService::AllSources(),
135 content::NotificationService::NoDetails());
138 void ChromeMetroViewerProcessHost::OnOpenURL(const base::string16& url) {
139 OpenURL(GURL(url));
142 void ChromeMetroViewerProcessHost::OnHandleSearchRequest(
143 const base::string16& search_string) {
144 GURL url(GetDefaultSearchURLForSearchTerms(
145 ProfileManager::GetActiveUserProfile(), search_string));
146 if (url.is_valid())
147 OpenURL(url);
150 void ChromeMetroViewerProcessHost::OnWindowSizeChanged(uint32 width,
151 uint32 height) {
152 std::vector<ash::internal::DisplayInfo> info_list;
153 info_list.push_back(ash::internal::DisplayInfo::CreateFromSpec(
154 base::StringPrintf("%dx%d", width, height)));
155 ash::Shell::GetInstance()->display_manager()->OnNativeDisplaysChanged(
156 info_list);
157 aura::RemoteWindowTreeHostWin::Instance()->HandleWindowSizeChanged(width,
158 height);