Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / metro_viewer / chrome_metro_viewer_process_host_aurawin.cc
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/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_window_tree_host_win.h"
35 #include "url/gurl.h"
36
37 namespace {
38
39 void CloseOpenAshBrowsers() {
40   BrowserList* browser_list =
41       BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH);
42   if (browser_list) {
43     for (BrowserList::const_iterator i = browser_list->begin();
44          i != browser_list->end(); ++i) {
45       Browser* browser = *i;
46       browser->window()->Close();
47       // If the attempt to Close the browser fails due to unload handlers on
48       // the page or in progress downloads, etc, destroy all tabs on the page.
49       while (browser->tab_strip_model()->count())
50         delete browser->tab_strip_model()->GetWebContentsAt(0);
51     }
52   }
53 }
54
55 void OpenURL(const GURL& url) {
56   chrome::NavigateParams params(
57       ProfileManager::GetActiveUserProfile(),
58       GURL(url),
59       content::PAGE_TRANSITION_TYPED);
60   params.disposition = NEW_FOREGROUND_TAB;
61   params.host_desktop_type = chrome::HOST_DESKTOP_TYPE_ASH;
62   chrome::Navigate(&params);
63 }
64
65 }  // namespace
66
67 ChromeMetroViewerProcessHost::ChromeMetroViewerProcessHost()
68     : MetroViewerProcessHost(
69           content::BrowserThread::GetMessageLoopProxyForThread(
70               content::BrowserThread::IO)) {
71   g_browser_process->AddRefModule();
72 }
73
74 void ChromeMetroViewerProcessHost::OnChannelError() {
75   // TODO(cpu): At some point we only close the browser. Right now this
76   // is very convenient for developing.
77   DVLOG(1) << "viewer channel error : Quitting browser";
78
79   // Unset environment variable to let breakpad know that metro process wasn't
80   // connected.
81   ::SetEnvironmentVariableA(env_vars::kMetroConnected, NULL);
82
83   aura::RemoteWindowTreeHostWin::Instance()->Disconnected();
84   g_browser_process->ReleaseModule();
85
86   // If browser is trying to quit, we shouldn't reenter the process.
87   // TODO(shrikant): In general there seem to be issues with how AttemptExit
88   // reentry works. In future release please clean up related code.
89   if (!browser_shutdown::IsTryingToQuit()) {
90     CloseOpenAshBrowsers();
91     chrome::CloseAsh();
92   }
93   // Tell the rest of Chrome about it.
94   content::NotificationService::current()->Notify(
95       chrome::NOTIFICATION_ASH_SESSION_ENDED,
96       content::NotificationService::AllSources(),
97       content::NotificationService::NoDetails());
98
99   // This will delete the MetroViewerProcessHost object. Don't access member
100   // variables/functions after this call.
101   g_browser_process->platform_part()->OnMetroViewerProcessTerminated();
102 }
103
104 void ChromeMetroViewerProcessHost::OnChannelConnected(int32 /*peer_pid*/) {
105   DVLOG(1) << "ChromeMetroViewerProcessHost::OnChannelConnected: ";
106   // Set environment variable to let breakpad know that metro process was
107   // connected.
108   ::SetEnvironmentVariableA(env_vars::kMetroConnected, "1");
109
110   if (!content::GpuDataManager::GetInstance()->GpuAccessAllowed(NULL)) {
111     DVLOG(1) << "No GPU access, attempting to restart in Desktop\n";
112     chrome::AttemptRestartToDesktopMode();
113   }
114 }
115
116 void ChromeMetroViewerProcessHost::OnSetTargetSurface(
117     gfx::NativeViewId target_surface) {
118   HWND hwnd = reinterpret_cast<HWND>(target_surface);
119   // Make hwnd available as early as possible for proper InputMethod
120   // initialization.
121   aura::RemoteWindowTreeHostWin::Instance()->SetRemoteWindowHandle(hwnd);
122
123   // Now start the Ash shell environment.
124   chrome::OpenAsh();
125   ash::Shell::GetInstance()->CreateShelf();
126   ash::Shell::GetInstance()->ShowShelf();
127
128   // Tell our root window host that the viewer has connected.
129   aura::RemoteWindowTreeHostWin::Instance()->Connected(this);
130
131   // On Windows 8 ASH we default to SHOW_STATE_MAXIMIZED for the browser
132   // window. This is to ensure that we honor metro app conventions by default.
133   ash::WindowPositioner::SetMaximizeFirstWindow(true);
134   // Tell the rest of Chrome that Ash is running.
135   content::NotificationService::current()->Notify(
136       chrome::NOTIFICATION_ASH_SESSION_STARTED,
137       content::NotificationService::AllSources(),
138       content::NotificationService::NoDetails());
139 }
140
141 void ChromeMetroViewerProcessHost::OnOpenURL(const base::string16& url) {
142   OpenURL(GURL(url));
143 }
144
145 void ChromeMetroViewerProcessHost::OnHandleSearchRequest(
146     const base::string16& search_string) {
147   GURL url(GetDefaultSearchURLForSearchTerms(
148       ProfileManager::GetActiveUserProfile(), search_string));
149   if (url.is_valid())
150     OpenURL(url);
151 }
152
153 void ChromeMetroViewerProcessHost::OnWindowSizeChanged(uint32 width,
154                                                        uint32 height) {
155   std::vector<ash::internal::DisplayInfo> info_list;
156   info_list.push_back(ash::internal::DisplayInfo::CreateFromSpec(
157       base::StringPrintf("%dx%d", width, height)));
158   ash::Shell::GetInstance()->display_manager()->OnNativeDisplaysChanged(
159       info_list);
160   aura::RemoteWindowTreeHostWin::Instance()->HandleWindowSizeChanged(width,
161                                                                      height);
162 }