Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / views / chrome_views_delegate.cc
blob8bb35263d77fa4011bd2cc7d29fd4d5184cff1a0
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/views/accessibility/accessibility_event_router_views.h"
16 #include "chrome/common/pref_names.h"
17 #include "grit/chrome_unscaled_resources.h"
18 #include "ui/base/resource/resource_bundle.h"
19 #include "ui/base/ui_base_switches.h"
20 #include "ui/gfx/rect.h"
21 #include "ui/gfx/screen.h"
22 #include "ui/views/widget/native_widget.h"
23 #include "ui/views/widget/widget.h"
25 #if defined(OS_WIN)
26 #include <dwmapi.h>
27 #include "base/win/windows_version.h"
28 #include "chrome/browser/app_icon_win.h"
29 #include "ui/base/win/shell.h"
30 #endif
32 #if defined(USE_AURA)
33 #include "ui/aura/root_window.h"
34 #endif
36 #if defined(USE_AURA) && !defined(OS_CHROMEOS)
37 #include "chrome/browser/ui/host_desktop.h"
38 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
39 #include "ui/views/widget/native_widget_aura.h"
40 #endif
42 #if defined(USE_ASH)
43 #include "ash/shell.h"
44 #include "ash/wm/window_state.h"
45 #include "chrome/browser/ui/ash/ash_init.h"
46 #include "chrome/browser/ui/ash/ash_util.h"
47 #endif
49 namespace {
51 // If the given window has a profile associated with it, use that profile's
52 // preference service. Otherwise, store and retrieve the data from Local State.
53 // This function may return NULL if the necessary pref service has not yet
54 // been initialized.
55 // TODO(mirandac): This function will also separate windows by profile in a
56 // multi-profile environment.
57 PrefService* GetPrefsForWindow(const views::Widget* window) {
58 Profile* profile = reinterpret_cast<Profile*>(
59 window->GetNativeWindowProperty(Profile::kProfileKey));
60 if (!profile) {
61 // Use local state for windows that have no explicit profile.
62 return g_browser_process->local_state();
64 return profile->GetPrefs();
67 } // namespace
69 ///////////////////////////////////////////////////////////////////////////////
70 // ChromeViewsDelegate, views::ViewsDelegate implementation:
72 void ChromeViewsDelegate::SaveWindowPlacement(const views::Widget* window,
73 const std::string& window_name,
74 const gfx::Rect& bounds,
75 ui::WindowShowState show_state) {
76 PrefService* prefs = GetPrefsForWindow(window);
77 if (!prefs)
78 return;
80 DCHECK(prefs->FindPreference(window_name.c_str()));
81 DictionaryPrefUpdate update(prefs, window_name.c_str());
82 base::DictionaryValue* window_preferences = update.Get();
83 window_preferences->SetInteger("left", bounds.x());
84 window_preferences->SetInteger("top", bounds.y());
85 window_preferences->SetInteger("right", bounds.right());
86 window_preferences->SetInteger("bottom", bounds.bottom());
87 window_preferences->SetBoolean("maximized",
88 show_state == ui::SHOW_STATE_MAXIMIZED);
89 gfx::Rect work_area(gfx::Screen::GetScreenFor(window->GetNativeView())->
90 GetDisplayNearestWindow(window->GetNativeView()).work_area());
91 window_preferences->SetInteger("work_area_left", work_area.x());
92 window_preferences->SetInteger("work_area_top", work_area.y());
93 window_preferences->SetInteger("work_area_right", work_area.right());
94 window_preferences->SetInteger("work_area_bottom", work_area.bottom());
97 bool ChromeViewsDelegate::GetSavedWindowPlacement(
98 const views::Widget* widget,
99 const std::string& window_name,
100 gfx::Rect* bounds,
101 ui::WindowShowState* show_state) const {
102 PrefService* prefs = g_browser_process->local_state();
103 if (!prefs)
104 return false;
106 DCHECK(prefs->FindPreference(window_name.c_str()));
107 const base::DictionaryValue* dictionary =
108 prefs->GetDictionary(window_name.c_str());
109 int left, top, right, bottom;
110 if (!dictionary || !dictionary->GetInteger("left", &left) ||
111 !dictionary->GetInteger("top", &top) ||
112 !dictionary->GetInteger("right", &right) ||
113 !dictionary->GetInteger("bottom", &bottom))
114 return false;
116 bounds->SetRect(left, top, right - left, bottom - top);
118 bool maximized = false;
119 if (dictionary)
120 dictionary->GetBoolean("maximized", &maximized);
121 *show_state = maximized ? ui::SHOW_STATE_MAXIMIZED : ui::SHOW_STATE_NORMAL;
123 #if defined(USE_ASH)
124 // On Ash environment, a window won't span across displays. Adjust
125 // the bounds to fit the work area.
126 gfx::NativeView window = widget->GetNativeView();
127 if (chrome::GetHostDesktopTypeForNativeView(window) ==
128 chrome::HOST_DESKTOP_TYPE_ASH) {
129 gfx::Display display = gfx::Screen::GetScreenFor(window)->
130 GetDisplayMatching(*bounds);
131 bounds->AdjustToFit(display.work_area());
132 ash::wm::GetWindowState(window)->set_minimum_visibility(true);
134 #endif
135 return true;
138 void ChromeViewsDelegate::NotifyAccessibilityEvent(
139 views::View* view, ui::AccessibilityTypes::Event event_type) {
140 AccessibilityEventRouterViews::GetInstance()->HandleAccessibilityEvent(
141 view, event_type);
144 void ChromeViewsDelegate::NotifyMenuItemFocused(
145 const base::string16& menu_name,
146 const base::string16& menu_item_name,
147 int item_index,
148 int item_count,
149 bool has_submenu) {
150 AccessibilityEventRouterViews::GetInstance()->HandleMenuItemFocused(
151 menu_name, menu_item_name, item_index, item_count, has_submenu);
154 #if defined(OS_WIN)
155 HICON ChromeViewsDelegate::GetDefaultWindowIcon() const {
156 return GetAppIcon();
159 bool ChromeViewsDelegate::IsWindowInMetro(gfx::NativeWindow window) const {
160 return chrome::IsNativeViewInAsh(window);
163 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS)
164 gfx::ImageSkia* ChromeViewsDelegate::GetDefaultWindowIcon() const {
165 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
166 return rb.GetImageSkiaNamed(IDR_PRODUCT_LOGO_64);
168 #endif
170 views::NonClientFrameView* ChromeViewsDelegate::CreateDefaultNonClientFrameView(
171 views::Widget* widget) {
172 #if defined(USE_ASH)
173 if (chrome::IsNativeViewInAsh(widget->GetNativeView()))
174 return ash::Shell::GetInstance()->CreateDefaultNonClientFrameView(widget);
175 #endif
176 return NULL;
179 void ChromeViewsDelegate::AddRef() {
180 g_browser_process->AddRefModule();
183 void ChromeViewsDelegate::ReleaseRef() {
184 g_browser_process->ReleaseModule();
187 content::WebContents* ChromeViewsDelegate::CreateWebContents(
188 content::BrowserContext* browser_context,
189 content::SiteInstance* site_instance) {
190 return NULL;
193 void ChromeViewsDelegate::OnBeforeWidgetInit(
194 views::Widget::InitParams* params,
195 views::internal::NativeWidgetDelegate* delegate) {
196 // We need to determine opacity if it's not already specified.
197 if (params->opacity == views::Widget::InitParams::INFER_OPACITY)
198 params->opacity = GetOpacityForInitParams(*params);
200 // If we already have a native_widget, we don't have to try to come
201 // up with one.
202 if (params->native_widget)
203 return;
205 #if defined(USE_AURA) && !defined(OS_CHROMEOS)
206 bool use_non_toplevel_window =
207 params->parent && params->type != views::Widget::InitParams::TYPE_MENU;
209 #if defined(OS_WIN)
210 // On desktop Linux Chrome must run in an environment that supports a variety
211 // of window managers, some of which do not play nicely with parts of our UI
212 // that have specific expectations about window sizing and placement. For this
213 // reason windows opened as top level (params.top_level) are always
214 // constrained by the browser frame, so we can position them correctly. This
215 // has some negative side effects, like dialogs being clipped by the browser
216 // frame, but the side effects are not as bad as the poor window manager
217 // interactions. On Windows however these WM interactions are not an issue, so
218 // we open windows requested as top_level as actual top level windows on the
219 // desktop.
220 use_non_toplevel_window = use_non_toplevel_window &&
221 (!params->top_level ||
222 chrome::GetHostDesktopTypeForNativeView(params->parent) !=
223 chrome::HOST_DESKTOP_TYPE_NATIVE);
225 if (!ui::win::IsAeroGlassEnabled()) {
226 // If we don't have composition (either because Glass is not enabled or
227 // because it was disabled at the command line), anything that requires
228 // transparency will be broken with a toplevel window, so force the use of
229 // a non toplevel window.
230 if (params->opacity == views::Widget::InitParams::TRANSLUCENT_WINDOW &&
231 params->type != views::Widget::InitParams::TYPE_MENU)
232 use_non_toplevel_window = true;
233 } else {
234 // If we're on Vista+ with composition enabled, then we can use toplevel
235 // windows for most things (they get blended via WS_EX_COMPOSITED, which
236 // allows for animation effects, but also exceeding the bounds of the parent
237 // window).
238 if (chrome::GetActiveDesktop() != chrome::HOST_DESKTOP_TYPE_ASH &&
239 params->parent &&
240 params->type != views::Widget::InitParams::TYPE_CONTROL &&
241 params->type != views::Widget::InitParams::TYPE_WINDOW) {
242 // When we set this to false, we get a DesktopNativeWidgetAura from the
243 // default case (not handled in this function).
244 use_non_toplevel_window = false;
247 #endif // OS_WIN
248 #endif // USE_AURA
250 #if defined(OS_CHROMEOS)
251 // When we are doing straight chromeos builds, we still need to handle the
252 // toplevel window case.
253 // There may be a few remaining widgets in Chrome OS that are not top level,
254 // but have neither a context nor a parent. Provide a fallback context so
255 // users don't crash. Developers will hit the DCHECK and should provide a
256 // context.
257 if (params->context)
258 params->context = params->context->GetRootWindow();
259 DCHECK(params->parent || params->context || params->top_level)
260 << "Please provide a parent or context for this widget.";
261 if (!params->parent && !params->context)
262 params->context = ash::Shell::GetPrimaryRootWindow();
263 #elif defined(USE_AURA)
264 // While the majority of the time, context wasn't plumbed through due to the
265 // existence of a global WindowTreeClient, if this window is a toplevel, it's
266 // possible that there is no contextual state that we can use.
267 if (params->parent == NULL && params->context == NULL && params->top_level) {
268 // We need to make a decision about where to place this window based on the
269 // desktop type.
270 switch (chrome::GetActiveDesktop()) {
271 case chrome::HOST_DESKTOP_TYPE_NATIVE:
272 // If we're native, we should give this window its own toplevel desktop
273 // widget.
274 params->native_widget = new views::DesktopNativeWidgetAura(delegate);
275 break;
276 #if defined(USE_ASH)
277 case chrome::HOST_DESKTOP_TYPE_ASH:
278 // If we're in ash, give this window the context of the main monitor.
279 params->context = ash::Shell::GetPrimaryRootWindow();
280 break;
281 #endif
282 default:
283 NOTREACHED();
285 } else if (use_non_toplevel_window) {
286 params->native_widget = new views::NativeWidgetAura(delegate);
287 } else if (params->type != views::Widget::InitParams::TYPE_TOOLTIP) {
288 // TODO(erg): Once we've threaded context to everywhere that needs it, we
289 // should remove this check here.
290 gfx::NativeView to_check =
291 params->context ? params->context : params->parent;
292 if (chrome::GetHostDesktopTypeForNativeView(to_check) ==
293 chrome::HOST_DESKTOP_TYPE_NATIVE) {
294 params->native_widget = new views::DesktopNativeWidgetAura(delegate);
297 #endif
300 base::TimeDelta
301 ChromeViewsDelegate::GetDefaultTextfieldObscuredRevealDuration() {
302 return base::TimeDelta();
305 #if !defined(USE_AURA) && !defined(USE_CHROMEOS)
306 views::Widget::InitParams::WindowOpacity
307 ChromeViewsDelegate::GetOpacityForInitParams(
308 const views::Widget::InitParams& params) {
309 return views::Widget::InitParams::OPAQUE_WINDOW;
311 #endif