[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / views / frame / browser_frame.cc
blob82fbf6d8e3009e64581ef9c955199220430bfdd3
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/frame/browser_frame.h"
7 #include "ash/shell.h"
8 #include "base/command_line.h"
9 #include "base/debug/leak_annotations.h"
10 #include "base/i18n/rtl.h"
11 #include "chrome/browser/app_mode/app_mode_utils.h"
12 #include "chrome/browser/themes/theme_service.h"
13 #include "chrome/browser/themes/theme_service_factory.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_list.h"
16 #include "chrome/browser/ui/browser_window_state.h"
17 #include "chrome/browser/ui/views/frame/browser_non_client_frame_view.h"
18 #include "chrome/browser/ui/views/frame/browser_root_view.h"
19 #include "chrome/browser/ui/views/frame/browser_view.h"
20 #include "chrome/browser/ui/views/frame/immersive_mode_controller.h"
21 #include "chrome/browser/ui/views/frame/native_browser_frame.h"
22 #include "chrome/browser/ui/views/frame/native_browser_frame_factory.h"
23 #include "chrome/browser/ui/views/frame/system_menu_model_builder.h"
24 #include "chrome/browser/ui/views/frame/top_container_view.h"
25 #include "chrome/browser/web_applications/web_app.h"
26 #include "chrome/common/chrome_switches.h"
27 #include "chrome/common/pref_names.h"
28 #include "ui/aura/window.h"
29 #include "ui/aura/window_event_dispatcher.h"
30 #include "ui/base/hit_test.h"
31 #include "ui/base/theme_provider.h"
32 #include "ui/events/event_handler.h"
33 #include "ui/gfx/font_list.h"
34 #include "ui/gfx/screen.h"
35 #include "ui/views/controls/menu/menu_runner.h"
36 #include "ui/views/widget/native_widget.h"
38 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
39 #include "chrome/browser/shell_integration_linux.h"
40 #endif
42 #if defined(USE_ASH)
43 #include "chrome/browser/ui/ash/ash_init.h"
44 #endif
46 #if defined(OS_CHROMEOS)
47 #include "ash/session/session_state_delegate.h"
48 #endif
50 #if defined(USE_X11)
51 #include "chrome/browser/ui/views/frame/browser_command_handler_x11.h"
52 #endif
54 ////////////////////////////////////////////////////////////////////////////////
55 // BrowserFrame, public:
57 BrowserFrame::BrowserFrame(BrowserView* browser_view)
58 : native_browser_frame_(NULL),
59 root_view_(NULL),
60 browser_frame_view_(NULL),
61 browser_view_(browser_view),
62 theme_provider_(ThemeServiceFactory::GetForProfile(
63 browser_view_->browser()->profile())) {
64 browser_view_->set_frame(this);
65 set_is_secondary_widget(false);
66 // Don't focus anything on creation, selecting a tab will set the focus.
67 set_focus_on_creation(false);
69 #if defined(USE_X11)
70 browser_command_handler_.reset(
71 new BrowserCommandHandlerX11(browser_view_->browser()));
72 #endif
75 BrowserFrame::~BrowserFrame() {
76 if (browser_command_handler_ && GetNativeView())
77 GetNativeView()->RemovePreTargetHandler(browser_command_handler_.get());
80 // static
81 const gfx::FontList& BrowserFrame::GetTitleFontList() {
82 static const gfx::FontList* title_font_list = new gfx::FontList();
83 ANNOTATE_LEAKING_OBJECT_PTR(title_font_list);
84 return *title_font_list;
87 void BrowserFrame::InitBrowserFrame() {
88 use_custom_frame_pref_.Init(
89 prefs::kUseCustomChromeFrame,
90 browser_view_->browser()->profile()->GetPrefs(),
91 base::Bind(&BrowserFrame::OnUseCustomChromeFrameChanged,
92 base::Unretained(this)));
94 native_browser_frame_ =
95 NativeBrowserFrameFactory::CreateNativeBrowserFrame(this, browser_view_);
96 views::Widget::InitParams params;
97 params.delegate = browser_view_;
98 params.native_widget = native_browser_frame_->AsNativeWidget();
99 if (browser_view_->browser()->is_type_tabbed()) {
100 // Typed panel/popup can only return a size once the widget has been
101 // created.
102 chrome::GetSavedWindowBoundsAndShowState(browser_view_->browser(),
103 &params.bounds,
104 &params.show_state);
106 #if defined(USE_ASH)
107 if (browser_view_->browser()->host_desktop_type() ==
108 chrome::HOST_DESKTOP_TYPE_ASH || chrome::ShouldOpenAshOnStartup()) {
109 params.context = ash::Shell::GetPrimaryRootWindow();
110 #if defined(OS_WIN)
111 // If this window is under ASH on Windows, we need it to be translucent.
112 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
113 #endif
115 #endif
117 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
118 // Set up a custom WM_CLASS for some sorts of window types. This allows
119 // task switchers in X11 environments to distinguish between main browser
120 // windows and e.g app windows.
121 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
122 const Browser& browser = *browser_view_->browser();
123 params.wm_class_class = ShellIntegrationLinux::GetProgramClassName();
124 params.wm_class_name = params.wm_class_class;
125 if (browser.is_app() && !browser.is_devtools()) {
126 // This window is a hosted app or v1 packaged app.
127 // NOTE: v2 packaged app windows are created by ChromeNativeAppWindowViews.
128 params.wm_class_name = web_app::GetWMClassFromAppName(browser.app_name());
129 } else if (command_line.HasSwitch(switches::kUserDataDir)) {
130 // Set the class name to e.g. "Chrome (/tmp/my-user-data)". The
131 // class name will show up in the alt-tab list in gnome-shell if
132 // you're running a binary that doesn't have a matching .desktop
133 // file.
134 const std::string user_data_dir =
135 command_line.GetSwitchValueNative(switches::kUserDataDir);
136 params.wm_class_name += " (" + user_data_dir + ")";
138 const char kX11WindowRoleBrowser[] = "browser";
139 const char kX11WindowRolePopup[] = "pop-up";
140 params.wm_role_name = browser_view_->browser()->is_type_tabbed() ?
141 std::string(kX11WindowRoleBrowser) : std::string(kX11WindowRolePopup);
143 params.remove_standard_frame = UseCustomFrame();
144 set_frame_type(UseCustomFrame() ? Widget::FRAME_TYPE_FORCE_CUSTOM
145 : Widget::FRAME_TYPE_FORCE_NATIVE);
146 #endif // defined(OS_LINUX)
148 Init(params);
150 if (!native_browser_frame_->UsesNativeSystemMenu()) {
151 DCHECK(non_client_view());
152 non_client_view()->set_context_menu_controller(this);
155 if (browser_command_handler_)
156 GetNativeWindow()->AddPreTargetHandler(browser_command_handler_.get());
159 void BrowserFrame::SetThemeProvider(scoped_ptr<ui::ThemeProvider> provider) {
160 owned_theme_provider_ = provider.Pass();
161 theme_provider_ = owned_theme_provider_.get();
164 int BrowserFrame::GetMinimizeButtonOffset() const {
165 return native_browser_frame_->GetMinimizeButtonOffset();
168 gfx::Rect BrowserFrame::GetBoundsForTabStrip(views::View* tabstrip) const {
169 // This can be invoked before |browser_frame_view_| has been set.
170 return browser_frame_view_ ?
171 browser_frame_view_->GetBoundsForTabStrip(tabstrip) : gfx::Rect();
174 int BrowserFrame::GetTopInset() const {
175 return browser_frame_view_->GetTopInset();
178 int BrowserFrame::GetThemeBackgroundXInset() const {
179 return browser_frame_view_->GetThemeBackgroundXInset();
182 void BrowserFrame::UpdateThrobber(bool running) {
183 browser_frame_view_->UpdateThrobber(running);
186 views::View* BrowserFrame::GetFrameView() const {
187 return browser_frame_view_;
190 bool BrowserFrame::UseCustomFrame() const {
191 return use_custom_frame_pref_.GetValue();
194 bool BrowserFrame::ShouldSaveWindowPlacement() const {
195 return native_browser_frame_->ShouldSaveWindowPlacement();
198 void BrowserFrame::GetWindowPlacement(gfx::Rect* bounds,
199 ui::WindowShowState* show_state) const {
200 return native_browser_frame_->GetWindowPlacement(bounds, show_state);
203 ///////////////////////////////////////////////////////////////////////////////
204 // BrowserFrame, views::Widget overrides:
206 views::internal::RootView* BrowserFrame::CreateRootView() {
207 root_view_ = new BrowserRootView(browser_view_, this);
208 return root_view_;
211 views::NonClientFrameView* BrowserFrame::CreateNonClientFrameView() {
212 browser_frame_view_ =
213 chrome::CreateBrowserNonClientFrameView(this, browser_view_);
214 return browser_frame_view_;
217 bool BrowserFrame::GetAccelerator(int command_id,
218 ui::Accelerator* accelerator) const {
219 return browser_view_->GetAccelerator(command_id, accelerator);
222 ui::ThemeProvider* BrowserFrame::GetThemeProvider() const {
223 return theme_provider_;
226 void BrowserFrame::SchedulePaintInRect(const gfx::Rect& rect) {
227 views::Widget::SchedulePaintInRect(rect);
229 // Paint the frame caption area and window controls during immersive reveal.
230 if (browser_view_ &&
231 browser_view_->immersive_mode_controller()->IsRevealed()) {
232 // This function should not be reentrant because the TopContainerView
233 // paints to a layer for the duration of the immersive reveal.
234 views::View* top_container = browser_view_->top_container();
235 CHECK(top_container->layer());
236 top_container->SchedulePaintInRect(rect);
240 void BrowserFrame::OnNativeWidgetActivationChanged(bool active) {
241 if (active) {
242 // When running under remote desktop, if the remote desktop client is not
243 // active on the users desktop, then none of the windows contained in the
244 // remote desktop will be activated. However, NativeWidget::Activate() will
245 // still bring this browser window to the foreground. We explicitly set
246 // ourselves as the last active browser window to ensure that we get treated
247 // as such by the rest of Chrome.
248 BrowserList::SetLastActive(browser_view_->browser());
250 Widget::OnNativeWidgetActivationChanged(active);
253 void BrowserFrame::ShowContextMenuForView(views::View* source,
254 const gfx::Point& p,
255 ui::MenuSourceType source_type) {
256 if (chrome::IsRunningInForcedAppMode())
257 return;
259 // Only show context menu if point is in unobscured parts of browser, i.e.
260 // if NonClientHitTest returns :
261 // - HTCAPTION: in title bar or unobscured part of tabstrip
262 // - HTNOWHERE: as the name implies.
263 gfx::Point point_in_view_coords(p);
264 views::View::ConvertPointFromScreen(non_client_view(), &point_in_view_coords);
265 int hit_test = non_client_view()->NonClientHitTest(point_in_view_coords);
266 if (hit_test == HTCAPTION || hit_test == HTNOWHERE) {
267 menu_runner_.reset(new views::MenuRunner(GetSystemMenuModel()));
268 if (menu_runner_->RunMenuAt(source->GetWidget(),
269 NULL,
270 gfx::Rect(p, gfx::Size(0, 0)),
271 views::MENU_ANCHOR_TOPLEFT,
272 source_type,
273 views::MenuRunner::HAS_MNEMONICS |
274 views::MenuRunner::CONTEXT_MENU) ==
275 views::MenuRunner::MENU_DELETED) {
276 return;
281 ui::MenuModel* BrowserFrame::GetSystemMenuModel() {
282 #if defined(OS_CHROMEOS)
283 ash::SessionStateDelegate* delegate =
284 ash::Shell::GetInstance()->session_state_delegate();
285 if (delegate && delegate->NumberOfLoggedInUsers() > 1) {
286 // In Multi user mode, the number of users as well as the order of users
287 // can change. Coming here we have more then one user and since the menu
288 // model contains the user information, it must get updated to show any
289 // changes happened since the last invocation.
290 menu_model_builder_.reset();
292 #endif
293 if (!menu_model_builder_.get()) {
294 menu_model_builder_.reset(
295 new SystemMenuModelBuilder(browser_view_, browser_view_->browser()));
296 menu_model_builder_->Init();
298 return menu_model_builder_->menu_model();
301 AvatarMenuButton* BrowserFrame::GetAvatarMenuButton() {
302 return browser_frame_view_->avatar_button();
305 NewAvatarButton* BrowserFrame::GetNewAvatarMenuButton() {
306 return browser_frame_view_->new_avatar_button();
309 bool BrowserFrame::ShouldLeaveOffsetNearTopBorder() {
310 return !IsMaximized();
313 void BrowserFrame::OnUseCustomChromeFrameChanged() {
314 // Tell the window manager to add or remove system borders.
315 set_frame_type(UseCustomFrame() ? Widget::FRAME_TYPE_FORCE_CUSTOM
316 : Widget::FRAME_TYPE_FORCE_NATIVE);
317 FrameTypeChanged();