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