Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / views / chrome_views_delegate.cc
blob0fc0bbbb98df196ca4925383202f154e24ff0bdd
1 // Copyright (c) 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/ui/views/chrome_views_delegate.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/prefs/scoped_user_pref_update.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/ui/browser_window_state.h"
16 #include "components/version_info/version_info.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/context_factory.h"
19 #include "grit/chrome_unscaled_resources.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/base/ui_base_switches.h"
22 #include "ui/gfx/geometry/rect.h"
23 #include "ui/gfx/screen.h"
24 #include "ui/views/widget/native_widget.h"
25 #include "ui/views/widget/widget.h"
27 #if defined(OS_WIN)
28 #include <dwmapi.h>
29 #include <shellapi.h>
30 #include "base/profiler/scoped_tracker.h"
31 #include "base/task_runner_util.h"
32 #include "base/win/windows_version.h"
33 #include "chrome/browser/app_icon_win.h"
34 #include "content/public/browser/browser_thread.h"
35 #include "ui/base/win/shell.h"
36 #endif
38 #if defined(USE_AURA)
39 #include "chrome/browser/ui/aura/accessibility/automation_manager_aura.h"
40 #include "ui/aura/window.h"
41 #include "ui/aura/window_event_dispatcher.h"
42 #endif
44 #if defined(USE_AURA) && !defined(OS_CHROMEOS)
45 #include "chrome/browser/ui/host_desktop.h"
46 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
47 #include "ui/views/widget/native_widget_aura.h"
48 #endif
50 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
51 #include "ui/views/linux_ui/linux_ui.h"
52 #endif
54 #if defined(USE_ASH)
55 #include "ash/shell.h"
56 #include "ash/wm/window_state.h"
57 #include "chrome/browser/ui/ash/ash_init.h"
58 #include "chrome/browser/ui/ash/ash_util.h"
59 #endif
62 // Helpers --------------------------------------------------------------------
64 namespace {
66 Profile* GetProfileForWindow(const views::Widget* window) {
67 if (!window)
68 return NULL;
69 return reinterpret_cast<Profile*>(
70 window->GetNativeWindowProperty(Profile::kProfileKey));
73 // If the given window has a profile associated with it, use that profile's
74 // preference service. Otherwise, store and retrieve the data from Local State.
75 // This function may return NULL if the necessary pref service has not yet
76 // been initialized.
77 // TODO(mirandac): This function will also separate windows by profile in a
78 // multi-profile environment.
79 PrefService* GetPrefsForWindow(const views::Widget* window) {
80 Profile* profile = GetProfileForWindow(window);
81 if (!profile) {
82 // Use local state for windows that have no explicit profile.
83 return g_browser_process->local_state();
85 return profile->GetPrefs();
88 #if defined(OS_WIN)
89 bool MonitorHasTopmostAutohideTaskbarForEdge(UINT edge, HMONITOR monitor) {
90 APPBARDATA taskbar_data = { sizeof(APPBARDATA), NULL, 0, edge };
92 // TODO(robliao): Remove ScopedTracker below once crbug.com/462368 is fixed.
93 tracked_objects::ScopedTracker tracking_profile(
94 FROM_HERE_WITH_EXPLICIT_FUNCTION(
95 "462368 MonitorHasTopmostAutohideTaskbarForEdge"));
97 // MSDN documents an ABM_GETAUTOHIDEBAREX, which supposedly takes a monitor
98 // rect and returns autohide bars on that monitor. This sounds like a good
99 // idea for multi-monitor systems. Unfortunately, it appears to not work at
100 // least some of the time (erroneously returning NULL) and there's almost no
101 // online documentation or other sample code using it that suggests ways to
102 // address this problem. So we just use ABM_GETAUTOHIDEBAR and hope the user
103 // only cares about autohide bars on the monitor with the primary taskbar.
105 // NOTE: This call spins a nested message loop.
106 HWND taskbar = reinterpret_cast<HWND>(SHAppBarMessage(ABM_GETAUTOHIDEBAR,
107 &taskbar_data));
108 return ::IsWindow(taskbar) &&
109 (MonitorFromWindow(taskbar, MONITOR_DEFAULTTONULL) == monitor) &&
110 (GetWindowLong(taskbar, GWL_EXSTYLE) & WS_EX_TOPMOST);
113 int GetAppbarAutohideEdgesOnWorkerThread(HMONITOR monitor) {
114 DCHECK(monitor);
116 int edges = 0;
117 if (MonitorHasTopmostAutohideTaskbarForEdge(ABE_LEFT, monitor))
118 edges |= views::ViewsDelegate::EDGE_LEFT;
119 if (MonitorHasTopmostAutohideTaskbarForEdge(ABE_TOP, monitor))
120 edges |= views::ViewsDelegate::EDGE_TOP;
121 if (MonitorHasTopmostAutohideTaskbarForEdge(ABE_RIGHT, monitor))
122 edges |= views::ViewsDelegate::EDGE_RIGHT;
123 if (MonitorHasTopmostAutohideTaskbarForEdge(ABE_BOTTOM, monitor))
124 edges |= views::ViewsDelegate::EDGE_BOTTOM;
125 return edges;
127 #endif
129 } // namespace
132 // ChromeViewsDelegate --------------------------------------------------------
134 #if defined(OS_WIN)
135 ChromeViewsDelegate::ChromeViewsDelegate()
136 : in_autohide_edges_callback_(false),
137 weak_factory_(this) {
138 #else
139 ChromeViewsDelegate::ChromeViewsDelegate() {
140 #endif
143 ChromeViewsDelegate::~ChromeViewsDelegate() {
146 void ChromeViewsDelegate::SaveWindowPlacement(const views::Widget* window,
147 const std::string& window_name,
148 const gfx::Rect& bounds,
149 ui::WindowShowState show_state) {
150 PrefService* prefs = GetPrefsForWindow(window);
151 if (!prefs)
152 return;
154 scoped_ptr<DictionaryPrefUpdate> pref_update =
155 chrome::GetWindowPlacementDictionaryReadWrite(window_name, prefs);
156 base::DictionaryValue* window_preferences = pref_update->Get();
157 window_preferences->SetInteger("left", bounds.x());
158 window_preferences->SetInteger("top", bounds.y());
159 window_preferences->SetInteger("right", bounds.right());
160 window_preferences->SetInteger("bottom", bounds.bottom());
161 window_preferences->SetBoolean("maximized",
162 show_state == ui::SHOW_STATE_MAXIMIZED);
163 window_preferences->SetBoolean("docked", show_state == ui::SHOW_STATE_DOCKED);
164 gfx::Rect work_area(gfx::Screen::GetScreenFor(window->GetNativeView())->
165 GetDisplayNearestWindow(window->GetNativeView()).work_area());
166 window_preferences->SetInteger("work_area_left", work_area.x());
167 window_preferences->SetInteger("work_area_top", work_area.y());
168 window_preferences->SetInteger("work_area_right", work_area.right());
169 window_preferences->SetInteger("work_area_bottom", work_area.bottom());
172 bool ChromeViewsDelegate::GetSavedWindowPlacement(
173 const views::Widget* widget,
174 const std::string& window_name,
175 gfx::Rect* bounds,
176 ui::WindowShowState* show_state) const {
177 PrefService* prefs = g_browser_process->local_state();
178 if (!prefs)
179 return false;
181 DCHECK(prefs->FindPreference(window_name));
182 const base::DictionaryValue* dictionary = prefs->GetDictionary(window_name);
183 int left = 0;
184 int top = 0;
185 int right = 0;
186 int bottom = 0;
187 if (!dictionary || !dictionary->GetInteger("left", &left) ||
188 !dictionary->GetInteger("top", &top) ||
189 !dictionary->GetInteger("right", &right) ||
190 !dictionary->GetInteger("bottom", &bottom))
191 return false;
193 bounds->SetRect(left, top, right - left, bottom - top);
195 bool maximized = false;
196 if (dictionary)
197 dictionary->GetBoolean("maximized", &maximized);
198 *show_state = maximized ? ui::SHOW_STATE_MAXIMIZED : ui::SHOW_STATE_NORMAL;
200 #if defined(USE_ASH)
201 // On Ash environment, a window won't span across displays. Adjust
202 // the bounds to fit the work area.
203 gfx::NativeView window = widget->GetNativeView();
204 if (chrome::GetHostDesktopTypeForNativeView(window) ==
205 chrome::HOST_DESKTOP_TYPE_ASH) {
206 gfx::Display display = gfx::Screen::GetScreenFor(window)->
207 GetDisplayMatching(*bounds);
208 bounds->AdjustToFit(display.work_area());
209 ash::wm::GetWindowState(window)->set_minimum_visibility(true);
211 #endif
212 return true;
215 void ChromeViewsDelegate::NotifyAccessibilityEvent(
216 views::View* view, ui::AXEvent event_type) {
217 #if defined(USE_AURA)
218 AutomationManagerAura::GetInstance()->HandleEvent(
219 GetProfileForWindow(view->GetWidget()), view, event_type);
220 #endif
223 #if defined(OS_WIN)
224 HICON ChromeViewsDelegate::GetDefaultWindowIcon() const {
225 return GetAppIcon();
228 HICON ChromeViewsDelegate::GetSmallWindowIcon() const {
229 return GetSmallAppIcon();
232 bool ChromeViewsDelegate::IsWindowInMetro(gfx::NativeWindow window) const {
233 return chrome::IsNativeViewInAsh(window);
236 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS)
237 gfx::ImageSkia* ChromeViewsDelegate::GetDefaultWindowIcon() const {
238 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
239 return rb.GetImageSkiaNamed(IDR_PRODUCT_LOGO_64);
241 #endif
243 #if defined(USE_ASH)
244 views::NonClientFrameView* ChromeViewsDelegate::CreateDefaultNonClientFrameView(
245 views::Widget* widget) {
246 return chrome::IsNativeViewInAsh(widget->GetNativeView()) ?
247 ash::Shell::GetInstance()->CreateDefaultNonClientFrameView(widget) : NULL;
249 #endif
251 void ChromeViewsDelegate::AddRef() {
252 g_browser_process->AddRefModule();
255 void ChromeViewsDelegate::ReleaseRef() {
256 g_browser_process->ReleaseModule();
259 void ChromeViewsDelegate::OnBeforeWidgetInit(
260 views::Widget::InitParams* params,
261 views::internal::NativeWidgetDelegate* delegate) {
262 // We need to determine opacity if it's not already specified.
263 if (params->opacity == views::Widget::InitParams::INFER_OPACITY)
264 params->opacity = GetOpacityForInitParams(*params);
266 // If we already have a native_widget, we don't have to try to come
267 // up with one.
268 if (params->native_widget)
269 return;
271 #if defined(USE_AURA) && !defined(OS_CHROMEOS)
272 bool use_non_toplevel_window =
273 params->parent &&
274 params->type != views::Widget::InitParams::TYPE_MENU &&
275 params->type != views::Widget::InitParams::TYPE_TOOLTIP;
277 #if defined(OS_WIN)
278 // On desktop Linux Chrome must run in an environment that supports a variety
279 // of window managers, some of which do not play nicely with parts of our UI
280 // that have specific expectations about window sizing and placement. For this
281 // reason windows opened as top level (!params.child) are always constrained
282 // by the browser frame, so we can position them correctly. This has some
283 // negative side effects, like dialogs being clipped by the browser frame, but
284 // the side effects are not as bad as the poor window manager interactions. On
285 // Windows however these WM interactions are not an issue, so we open windows
286 // requested as top_level as actual top level windows on the desktop.
287 use_non_toplevel_window = use_non_toplevel_window &&
288 (params->child ||
289 chrome::GetHostDesktopTypeForNativeView(params->parent) !=
290 chrome::HOST_DESKTOP_TYPE_NATIVE);
292 if (!ui::win::IsAeroGlassEnabled()) {
293 // If we don't have composition (either because Glass is not enabled or
294 // because it was disabled at the command line), anything that requires
295 // transparency will be broken with a toplevel window, so force the use of
296 // a non toplevel window.
297 if (params->opacity == views::Widget::InitParams::TRANSLUCENT_WINDOW &&
298 params->type != views::Widget::InitParams::TYPE_MENU)
299 use_non_toplevel_window = true;
300 } else {
301 // If we're on Vista+ with composition enabled, then we can use toplevel
302 // windows for most things (they get blended via WS_EX_COMPOSITED, which
303 // allows for animation effects, but also exceeding the bounds of the parent
304 // window).
305 if (chrome::GetActiveDesktop() != chrome::HOST_DESKTOP_TYPE_ASH &&
306 params->parent &&
307 params->type != views::Widget::InitParams::TYPE_CONTROL &&
308 params->type != views::Widget::InitParams::TYPE_WINDOW) {
309 // When we set this to false, we get a DesktopNativeWidgetAura from the
310 // default case (not handled in this function).
311 use_non_toplevel_window = false;
314 #endif // OS_WIN
315 #endif // USE_AURA
317 #if defined(OS_CHROMEOS)
318 // When we are doing straight chromeos builds, we still need to handle the
319 // toplevel window case.
320 // There may be a few remaining widgets in Chrome OS that are not top level,
321 // but have neither a context nor a parent. Provide a fallback context so
322 // users don't crash. Developers will hit the DCHECK and should provide a
323 // context.
324 if (params->context)
325 params->context = params->context->GetRootWindow();
326 DCHECK(params->parent || params->context || !params->child)
327 << "Please provide a parent or context for this widget.";
328 if (!params->parent && !params->context)
329 params->context = ash::Shell::GetPrimaryRootWindow();
330 #elif defined(USE_AURA)
331 // While the majority of the time, context wasn't plumbed through due to the
332 // existence of a global WindowTreeClient, if this window is toplevel, it's
333 // possible that there is no contextual state that we can use.
334 if (params->parent == NULL && params->context == NULL && !params->child) {
335 // We need to make a decision about where to place this window based on the
336 // desktop type.
337 switch (chrome::GetActiveDesktop()) {
338 case chrome::HOST_DESKTOP_TYPE_NATIVE:
339 // If we're native, we should give this window its own toplevel desktop
340 // widget.
341 params->native_widget = new views::DesktopNativeWidgetAura(delegate);
342 break;
343 #if defined(USE_ASH)
344 case chrome::HOST_DESKTOP_TYPE_ASH:
345 // If we're in ash, give this window the context of the main monitor.
346 params->context = ash::Shell::GetPrimaryRootWindow();
347 break;
348 #endif
349 default:
350 NOTREACHED();
352 } else if (use_non_toplevel_window) {
353 views::NativeWidgetAura* native_widget =
354 new views::NativeWidgetAura(delegate);
355 if (params->parent) {
356 Profile* parent_profile = reinterpret_cast<Profile*>(
357 params->parent->GetNativeWindowProperty(Profile::kProfileKey));
358 native_widget->SetNativeWindowProperty(Profile::kProfileKey,
359 parent_profile);
361 params->native_widget = native_widget;
362 } else {
363 // TODO(erg): Once we've threaded context to everywhere that needs it, we
364 // should remove this check here.
365 gfx::NativeView to_check =
366 params->context ? params->context : params->parent;
367 if (chrome::GetHostDesktopTypeForNativeView(to_check) ==
368 chrome::HOST_DESKTOP_TYPE_NATIVE) {
369 params->native_widget = new views::DesktopNativeWidgetAura(delegate);
372 #endif
375 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
376 bool ChromeViewsDelegate::WindowManagerProvidesTitleBar(bool maximized) {
377 // On Ubuntu Unity, the system always provides a title bar for maximized
378 // windows.
379 views::LinuxUI* ui = views::LinuxUI::instance();
380 return maximized && ui && ui->UnityIsRunning();
382 #endif
384 ui::ContextFactory* ChromeViewsDelegate::GetContextFactory() {
385 return content::GetContextFactory();
388 std::string ChromeViewsDelegate::GetApplicationName() {
389 return version_info::GetProductName();
392 #if defined(OS_WIN)
393 int ChromeViewsDelegate::GetAppbarAutohideEdges(HMONITOR monitor,
394 const base::Closure& callback) {
395 // Initialize the map with EDGE_BOTTOM. This is important, as if we return an
396 // initial value of 0 (no auto-hide edges) then we'll go fullscreen and
397 // windows will automatically remove WS_EX_TOPMOST from the appbar resulting
398 // in us thinking there is no auto-hide edges. By returning at least one edge
399 // we don't initially go fullscreen until we figure out the real auto-hide
400 // edges.
401 if (!appbar_autohide_edge_map_.count(monitor))
402 appbar_autohide_edge_map_[monitor] = EDGE_BOTTOM;
403 if (monitor && !in_autohide_edges_callback_) {
404 base::PostTaskAndReplyWithResult(
405 content::BrowserThread::GetBlockingPool(),
406 FROM_HERE,
407 base::Bind(&GetAppbarAutohideEdgesOnWorkerThread,
408 monitor),
409 base::Bind(&ChromeViewsDelegate::OnGotAppbarAutohideEdges,
410 weak_factory_.GetWeakPtr(),
411 callback,
412 monitor,
413 appbar_autohide_edge_map_[monitor]));
415 return appbar_autohide_edge_map_[monitor];
418 void ChromeViewsDelegate::OnGotAppbarAutohideEdges(
419 const base::Closure& callback,
420 HMONITOR monitor,
421 int returned_edges,
422 int edges) {
423 appbar_autohide_edge_map_[monitor] = edges;
424 if (returned_edges == edges)
425 return;
427 base::AutoReset<bool> in_callback_setter(&in_autohide_edges_callback_, true);
428 callback.Run();
430 #endif
432 scoped_refptr<base::TaskRunner>
433 ChromeViewsDelegate::GetBlockingPoolTaskRunner() {
434 return content::BrowserThread::GetBlockingPool();
437 #if !defined(USE_AURA) && !defined(USE_CHROMEOS)
438 views::Widget::InitParams::WindowOpacity
439 ChromeViewsDelegate::GetOpacityForInitParams(
440 const views::Widget::InitParams& params) {
441 return views::Widget::InitParams::OPAQUE_WINDOW;
443 #endif