browser_view.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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/ui/views/frame/browser_view.h"
6
7#include <algorithm>
8
9#include "base/auto_reset.h"
10#include "base/command_line.h"
11#include "base/i18n/rtl.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/metrics/histogram.h"
14#include "base/prefs/pref_service.h"
15#include "base/strings/string_number_conversions.h"
16#include "chrome/app/chrome_command_ids.h"
17#include "chrome/app/chrome_dll_resource.h"
18#include "chrome/browser/app_mode/app_mode_utils.h"
19#include "chrome/browser/bookmarks/bookmark_stats.h"
20#include "chrome/browser/browser_process.h"
21#include "chrome/browser/chrome_notification_types.h"
22#include "chrome/browser/extensions/tab_helper.h"
23#include "chrome/browser/infobars/infobar_service.h"
24#include "chrome/browser/native_window_notification_source.h"
25#include "chrome/browser/password_manager/chrome_password_manager_client.h"
26#include "chrome/browser/password_manager/password_manager.h"
27#include "chrome/browser/profiles/avatar_menu.h"
28#include "chrome/browser/profiles/profile.h"
29#include "chrome/browser/profiles/profile_info_cache.h"
30#include "chrome/browser/profiles/profile_manager.h"
31#include "chrome/browser/profiles/profiles_state.h"
32#include "chrome/browser/search/search.h"
33#include "chrome/browser/sessions/tab_restore_service.h"
34#include "chrome/browser/sessions/tab_restore_service_factory.h"
35#include "chrome/browser/speech/tts_controller.h"
36#include "chrome/browser/themes/theme_properties.h"
37#include "chrome/browser/themes/theme_service_factory.h"
38#include "chrome/browser/translate/translate_tab_helper.h"
39#include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog.h"
40#include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h"
41#include "chrome/browser/ui/bookmarks/bookmark_bar_constants.h"
42#include "chrome/browser/ui/bookmarks/bookmark_bubble_delegate.h"
43#include "chrome/browser/ui/bookmarks/bookmark_bubble_sign_in_delegate.h"
44#include "chrome/browser/ui/browser.h"
45#include "chrome/browser/ui/browser_command_controller.h"
46#include "chrome/browser/ui/browser_commands.h"
47#include "chrome/browser/ui/browser_dialogs.h"
48#include "chrome/browser/ui/browser_finder.h"
49#include "chrome/browser/ui/browser_list.h"
50#include "chrome/browser/ui/browser_window_state.h"
51#include "chrome/browser/ui/ntp_background_util.h"
52#include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
53#include "chrome/browser/ui/omnibox/omnibox_popup_view.h"
54#include "chrome/browser/ui/omnibox/omnibox_view.h"
55#include "chrome/browser/ui/search/search_delegate.h"
56#include "chrome/browser/ui/search/search_model.h"
57#include "chrome/browser/ui/search/search_ui.h"
58#include "chrome/browser/ui/tabs/tab_menu_model.h"
59#include "chrome/browser/ui/tabs/tab_strip_model.h"
60#include "chrome/browser/ui/view_ids.h"
61#include "chrome/browser/ui/views/accelerator_table.h"
62#include "chrome/browser/ui/views/accessibility/invert_bubble_view.h"
63#include "chrome/browser/ui/views/avatar_menu_bubble_view.h"
64#include "chrome/browser/ui/views/avatar_menu_button.h"
65#include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
66#include "chrome/browser/ui/views/bookmarks/bookmark_bubble_view.h"
67#include "chrome/browser/ui/views/browser_dialogs.h"
68#include "chrome/browser/ui/views/download/download_in_progress_dialog_view.h"
69#include "chrome/browser/ui/views/download/download_shelf_view.h"
70#include "chrome/browser/ui/views/extensions/bookmark_app_bubble_view.h"
71#include "chrome/browser/ui/views/frame/browser_view_layout.h"
72#include "chrome/browser/ui/views/frame/browser_view_layout_delegate.h"
73#include "chrome/browser/ui/views/frame/contents_layout_manager.h"
74#include "chrome/browser/ui/views/frame/immersive_mode_controller.h"
75#include "chrome/browser/ui/views/frame/native_browser_frame_factory.h"
76#include "chrome/browser/ui/views/frame/top_container_view.h"
77#include "chrome/browser/ui/views/fullscreen_exit_bubble_views.h"
78#include "chrome/browser/ui/views/infobars/infobar_container_view.h"
79#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
80#include "chrome/browser/ui/views/location_bar/location_icon_view.h"
81#include "chrome/browser/ui/views/omnibox/omnibox_view_views.h"
82#include "chrome/browser/ui/views/password_generation_bubble_view.h"
83#include "chrome/browser/ui/views/profile_chooser_view.h"
84#include "chrome/browser/ui/views/status_bubble_views.h"
85#include "chrome/browser/ui/views/tabs/browser_tab_strip_controller.h"
86#include "chrome/browser/ui/views/tabs/tab.h"
87#include "chrome/browser/ui/views/tabs/tab_strip.h"
88#include "chrome/browser/ui/views/toolbar/reload_button.h"
89#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
90#include "chrome/browser/ui/views/translate/translate_bubble_view.h"
91#include "chrome/browser/ui/views/update_recommended_message_box.h"
92#include "chrome/browser/ui/views/website_settings/permissions_bubble_view.h"
93#include "chrome/browser/ui/views/website_settings/website_settings_popup_view.h"
94#include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
95#include "chrome/browser/ui/window_sizer/window_sizer.h"
96#include "chrome/common/chrome_switches.h"
97#include "chrome/common/pref_names.h"
98#include "chrome/common/profile_management_switches.h"
99#include "chrome/common/url_constants.h"
100#include "content/public/browser/download_manager.h"
101#include "content/public/browser/native_web_keyboard_event.h"
102#include "content/public/browser/notification_service.h"
103#include "content/public/browser/render_view_host.h"
104#include "content/public/browser/render_widget_host.h"
105#include "content/public/browser/user_metrics.h"
106#include "content/public/browser/web_contents.h"
107#include "content/public/browser/web_contents_view.h"
108#include "content/public/common/content_switches.h"
109#include "grit/chromium_strings.h"
110#include "grit/generated_resources.h"
111#include "grit/locale_settings.h"
112#include "grit/theme_resources.h"
113#include "grit/ui_resources.h"
114#include "grit/ui_strings.h"
115#include "grit/webkit_resources.h"
116#include "ui/base/accelerators/accelerator.h"
117#include "ui/base/accessibility/accessible_view_state.h"
118#include "ui/base/hit_test.h"
119#include "ui/base/l10n/l10n_util.h"
120#include "ui/base/resource/resource_bundle.h"
121#include "ui/base/theme_provider.h"
122#include "ui/events/event_utils.h"
123#include "ui/gfx/canvas.h"
124#include "ui/gfx/color_utils.h"
125#include "ui/gfx/rect_conversions.h"
126#include "ui/gfx/sys_color_change_listener.h"
127#include "ui/views/controls/button/menu_button.h"
128#include "ui/views/controls/textfield/textfield.h"
129#include "ui/views/controls/webview/webview.h"
130#include "ui/views/focus/external_focus_tracker.h"
131#include "ui/views/focus/view_storage.h"
132#include "ui/views/layout/grid_layout.h"
133#include "ui/views/widget/native_widget.h"
134#include "ui/views/widget/root_view.h"
135#include "ui/views/widget/widget.h"
136#include "ui/views/window/dialog_delegate.h"
137
138#if defined(USE_ASH)
139#include "ash/ash_switches.h"
140#include "ash/shelf/shelf.h"
141#include "ash/shelf/shelf_model.h"
142#include "ash/shell.h"
143#include "chrome/browser/ui/ash/ash_util.h"
144#endif
145
146#if defined(USE_AURA)
147#include "ui/aura/root_window.h"
148#include "ui/aura/window.h"
149#include "ui/gfx/screen.h"
150#endif
151
152#if defined(OS_WIN)
153#include "base/win/windows_version.h"
154#include "chrome/browser/jumplist_win.h"
155#include "ui/views/win/scoped_fullscreen_visibility.h"
156#include "win8/util/win8_util.h"
157#endif
158
159#if defined(ENABLE_ONE_CLICK_SIGNIN)
160#include "chrome/browser/ui/sync/one_click_signin_bubble_delegate.h"
161#include "chrome/browser/ui/sync/one_click_signin_bubble_links_delegate.h"
162#include "chrome/browser/ui/views/sync/one_click_signin_bubble_view.h"
163#endif
164
165#if defined(OS_CHROMEOS)
166#include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
167#endif
168
169using base::TimeDelta;
170using base::UserMetricsAction;
171using content::NativeWebKeyboardEvent;
172using content::SSLStatus;
173using content::WebContents;
174using views::ColumnSet;
175using views::GridLayout;
176using web_modal::WebContentsModalDialogHost;
177
178namespace {
179// The name of a key to store on the window handle so that other code can
180// locate this object using just the handle.
181const char* const kBrowserViewKey = "__BROWSER_VIEW__";
182
183// The number of milliseconds between loading animation frames.
184const int kLoadingAnimationFrameTimeMs = 30;
185
186// TODO(kuan): These functions are temporarily for the bookmark bar while its
187// detached state is at the top of the page;  it'll be moved to float on the
188// content page in the very near future, at which time, these local functions
189// will be removed.
190void PaintDetachedBookmarkBar(gfx::Canvas* canvas,
191                              DetachableToolbarView* view,
192                              ThemeService* theme_service) {
193  // Paint background for detached state; if animating, this is fade in/out.
194  canvas->DrawColor(
195      chrome::GetDetachedBookmarkBarBackgroundColor(theme_service));
196  // Draw the separators above and below bookmark bar;
197  // if animating, these are fading in/out.
198  SkColor separator_color =
199      chrome::GetDetachedBookmarkBarSeparatorColor(theme_service);
200  DetachableToolbarView::PaintHorizontalBorder(canvas, view, true,
201                                               separator_color);
202  // The bottom border needs to be 1-px thick in both regular and retina
203  // displays, so we can't use DetachableToolbarView::PaintHorizontalBorder
204  // which paints a 2-px thick border in retina display.
205  SkPaint paint;
206  paint.setAntiAlias(false);
207  // Sets border to 1-px thick regardless of scale factor.
208  paint.setStrokeWidth(0);
209  // Bottom border is at 50% opacity of top border.
210  paint.setColor(SkColorSetA(separator_color,
211                             SkColorGetA(separator_color) / 2));
212  // Calculate thickness of bottom border as per current scale factor to
213  // determine where to draw the 1-px thick border.
214  float thickness = views::NonClientFrameView::kClientEdgeThickness /
215                    canvas->image_scale();
216  SkScalar y = SkIntToScalar(view->height()) - SkFloatToScalar(thickness);
217  canvas->sk_canvas()->drawLine(SkIntToScalar(0), y,
218                                SkIntToScalar(view->width()), y, paint);
219}
220
221void PaintAttachedBookmarkBar(gfx::Canvas* canvas,
222                              DetachableToolbarView* view,
223                              BrowserView* browser_view,
224                              chrome::HostDesktopType host_desktop_type,
225                              int toolbar_overlap) {
226  // Paint background for attached state, this is fade in/out.
227  gfx::Point background_image_offset =
228      browser_view->OffsetPointForToolbarBackgroundImage(
229          gfx::Point(view->GetMirroredX(), view->y()));
230  DetachableToolbarView::PaintBackgroundAttachedMode(canvas,
231      view->GetThemeProvider(), view->GetLocalBounds(),
232      background_image_offset, host_desktop_type);
233  if (view->height() >= toolbar_overlap) {
234    // Draw the separator below bookmark bar; this is fading in/out.
235    DetachableToolbarView::PaintHorizontalBorder(canvas, view, false,
236        ThemeProperties::GetDefaultColor(
237            ThemeProperties::COLOR_TOOLBAR_SEPARATOR));
238  }
239}
240
241}  // namespace
242
243// static
244const char BrowserView::kViewClassName[] = "BrowserView";
245
246namespace {
247
248bool ShouldSaveOrRestoreWindowPos() {
249#if defined(OS_WIN)
250  // In Windows 8's single window Metro mode the window is always maximized
251  // (without the WS_MAXIMIZE style).
252  if (win8::IsSingleWindowMetroMode())
253    return false;
254#endif
255  return true;
256}
257
258}  // namespace
259
260///////////////////////////////////////////////////////////////////////////////
261
262// Delegate implementation for BrowserViewLayout. Usually just forwards calls
263// into BrowserView.
264class BrowserViewLayoutDelegateImpl : public BrowserViewLayoutDelegate {
265 public:
266  explicit BrowserViewLayoutDelegateImpl(BrowserView* browser_view)
267      : browser_view_(browser_view) {}
268  virtual ~BrowserViewLayoutDelegateImpl() {}
269
270  // BrowserViewLayoutDelegate overrides:
271  virtual views::View* GetContentsWebView() const OVERRIDE {
272    return browser_view_->contents_web_view_;
273  }
274
275  virtual views::View* GetWindowSwitcherButton() const OVERRIDE {
276    return browser_view_->window_switcher_button();
277  }
278
279  virtual bool DownloadShelfNeedsLayout() const OVERRIDE {
280    DownloadShelfView* download_shelf = browser_view_->download_shelf_.get();
281    // Re-layout the shelf either if it is visible or if its close animation
282    // is currently running.
283    return download_shelf &&
284           (download_shelf->IsShowing() || download_shelf->IsClosing());
285  }
286
287  virtual bool IsTabStripVisible() const OVERRIDE {
288    return browser_view_->IsTabStripVisible();
289  }
290
291  virtual gfx::Rect GetBoundsForTabStripInBrowserView() const OVERRIDE {
292    gfx::RectF bounds_f(browser_view_->frame()->GetBoundsForTabStrip(
293        browser_view_->tabstrip()));
294    views::View::ConvertRectToTarget(browser_view_->parent(), browser_view_,
295        &bounds_f);
296    return gfx::ToEnclosingRect(bounds_f);
297  }
298
299  virtual int GetTopInsetInBrowserView() const OVERRIDE {
300    return browser_view_->frame()->GetTopInset() -
301        browser_view_->y();
302  }
303
304  virtual int GetThemeBackgroundXInset() const OVERRIDE {
305    // TODO(pkotwicz): Return the inset with respect to the left edge of the
306    // BrowserView.
307    return browser_view_->frame()->GetThemeBackgroundXInset();
308  }
309
310  virtual bool IsToolbarVisible() const OVERRIDE {
311    return browser_view_->IsToolbarVisible();
312  }
313
314  virtual bool IsBookmarkBarVisible() const OVERRIDE {
315    return browser_view_->IsBookmarkBarVisible();
316  }
317
318  virtual FullscreenExitBubbleViews* GetFullscreenExitBubble() const OVERRIDE {
319    return browser_view_->fullscreen_exit_bubble();
320  }
321
322 private:
323  BrowserView* browser_view_;
324
325  DISALLOW_COPY_AND_ASSIGN(BrowserViewLayoutDelegateImpl);
326};
327
328///////////////////////////////////////////////////////////////////////////////
329// BookmarkExtensionBackground, private:
330// This object serves as the views::Background object which is used to layout
331// and paint the bookmark bar.
332class BookmarkExtensionBackground : public views::Background {
333 public:
334  BookmarkExtensionBackground(BrowserView* browser_view,
335                              DetachableToolbarView* host_view,
336                              Browser* browser);
337
338  // View methods overridden from views:Background.
339  virtual void Paint(gfx::Canvas* canvas, views::View* view) const OVERRIDE;
340
341 private:
342  BrowserView* browser_view_;
343
344  // The view hosting this background.
345  DetachableToolbarView* host_view_;
346
347  Browser* browser_;
348
349  DISALLOW_COPY_AND_ASSIGN(BookmarkExtensionBackground);
350};
351
352BookmarkExtensionBackground::BookmarkExtensionBackground(
353    BrowserView* browser_view,
354    DetachableToolbarView* host_view,
355    Browser* browser)
356    : browser_view_(browser_view),
357      host_view_(host_view),
358      browser_(browser) {
359}
360
361void BookmarkExtensionBackground::Paint(gfx::Canvas* canvas,
362                                        views::View* view) const {
363  int toolbar_overlap = host_view_->GetToolbarOverlap();
364  if (!host_view_->IsDetached()) {
365    PaintAttachedBookmarkBar(canvas, host_view_, browser_view_,
366                             browser_->host_desktop_type(), toolbar_overlap);
367    return;
368  }
369
370  // As 'hidden' according to the animation is the full in-tab state, we invert
371  // the value - when current_state is at '0', we expect the bar to be docked.
372  double current_state = 1 - host_view_->GetAnimationValue();
373
374  ThemeService* ts =
375      ThemeServiceFactory::GetForProfile(browser_->profile());
376  if (current_state == 0.0 || current_state == 1.0) {
377    PaintDetachedBookmarkBar(canvas, host_view_, ts);
378    return;
379  }
380  // While animating, set opacity to cross-fade between attached and detached
381  // backgrounds including their respective separators.
382  int detached_alpha = static_cast<uint8>(current_state * 255);
383  int attached_alpha = 255 - detached_alpha;
384  if (browser_->bookmark_bar_state() == BookmarkBar::DETACHED) {
385    // To animate from attached to detached state:
386    // - fade out attached background
387    // - fade in detached background.
388    canvas->SaveLayerAlpha(attached_alpha);
389    PaintAttachedBookmarkBar(canvas, host_view_, browser_view_,
390                             browser_->host_desktop_type(),
391                             toolbar_overlap);
392    canvas->Restore();
393    canvas->SaveLayerAlpha(detached_alpha);
394    PaintDetachedBookmarkBar(canvas, host_view_, ts);
395  } else {
396    // To animate from detached to attached state:
397    // - fade out detached background
398    // - fade in attached background.
399    canvas->SaveLayerAlpha(detached_alpha);
400    PaintDetachedBookmarkBar(canvas, host_view_, ts);
401    canvas->Restore();
402    canvas->SaveLayerAlpha(attached_alpha);
403    PaintAttachedBookmarkBar(canvas, host_view_, browser_view_,
404                             browser_->host_desktop_type(),
405                             toolbar_overlap);
406  }
407  canvas->Restore();
408}
409
410///////////////////////////////////////////////////////////////////////////////
411// BrowserView, public:
412
413BrowserView::BrowserView()
414    : views::ClientView(NULL, NULL),
415      last_focused_view_storage_id_(
416          views::ViewStorage::GetInstance()->CreateStorageID()),
417      frame_(NULL),
418      top_container_(NULL),
419      tabstrip_(NULL),
420      toolbar_(NULL),
421      window_switcher_button_(NULL),
422      find_bar_host_view_(NULL),
423      infobar_container_(NULL),
424      contents_web_view_(NULL),
425      contents_container_(NULL),
426      devtools_window_(NULL),
427      initialized_(false),
428      in_process_fullscreen_(false),
429#if defined(OS_WIN)
430      hung_window_detector_(&hung_plugin_action_),
431      ticker_(0),
432#endif
433      force_location_bar_focus_(false),
434      immersive_mode_controller_(chrome::CreateImmersiveModeController()),
435#if defined(OS_CHROMEOS)
436      scroll_end_effect_controller_(ScrollEndEffectController::Create()),
437#endif
438      color_change_listener_(this),
439      activate_modal_dialog_factory_(this) {
440}
441
442BrowserView::~BrowserView() {
443  // All the tabs should have been destroyed already. If we were closed by the
444  // OS with some tabs than the NativeBrowserFrame should have destroyed them.
445  DCHECK_EQ(0, browser_->tab_strip_model()->count());
446
447  // Immersive mode may need to reparent views before they are removed/deleted.
448  immersive_mode_controller_.reset();
449
450  browser_->tab_strip_model()->RemoveObserver(this);
451
452#if defined(OS_WIN)
453  // Stop hung plugin monitoring.
454  ticker_.Stop();
455  ticker_.UnregisterTickHandler(&hung_window_detector_);
456
457  // Terminate the jumplist (must be called before browser_->profile() is
458  // destroyed.
459  if (jumplist_) {
460    jumplist_->Terminate();
461  }
462#endif
463
464  // We destroy the download shelf before |browser_| to remove its child
465  // download views from the set of download observers (since the observed
466  // downloads can be destroyed along with |browser_| and the observer
467  // notifications will call back into deleted objects).
468  BrowserViewLayout* browser_view_layout = GetBrowserViewLayout();
469  if (browser_view_layout)
470    browser_view_layout->set_download_shelf(NULL);
471  download_shelf_.reset();
472
473  // The TabStrip attaches a listener to the model. Make sure we shut down the
474  // TabStrip first so that it can cleanly remove the listener.
475  if (tabstrip_) {
476    tabstrip_->parent()->RemoveChildView(tabstrip_);
477    if (browser_view_layout)
478      browser_view_layout->set_tab_strip(NULL);
479    delete tabstrip_;
480    tabstrip_ = NULL;
481  }
482  // Child views maintain PrefMember attributes that point to
483  // OffTheRecordProfile's PrefService which gets deleted by ~Browser.
484  RemoveAllChildViews(true);
485  toolbar_ = NULL;
486
487  // Explicitly set browser_ to NULL.
488  browser_.reset();
489}
490
491void BrowserView::Init(Browser* browser) {
492  browser_.reset(browser);
493  browser_->tab_strip_model()->AddObserver(this);
494}
495
496// static
497BrowserView* BrowserView::GetBrowserViewForNativeWindow(
498    gfx::NativeWindow window) {
499  views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
500  return widget ?
501      reinterpret_cast<BrowserView*>(widget->GetNativeWindowProperty(
502          kBrowserViewKey)) : NULL;
503}
504
505// static
506BrowserView* BrowserView::GetBrowserViewForBrowser(const Browser* browser) {
507  return static_cast<BrowserView*>(browser->window());
508}
509
510void BrowserView::InitStatusBubble() {
511  status_bubble_.reset(new StatusBubbleViews(contents_web_view_));
512  contents_web_view_->SetStatusBubble(status_bubble_.get());
513}
514
515void BrowserView::InitPermissionBubbleView() {
516  permission_bubble_view_.reset(new PermissionBubbleViewViews(
517      GetLocationBarView()->GetLocationIconView()));
518}
519
520gfx::Rect BrowserView::GetToolbarBounds() const {
521  gfx::Rect toolbar_bounds(toolbar_->bounds());
522  if (toolbar_bounds.IsEmpty())
523    return toolbar_bounds;
524  // The apparent toolbar edges are outside the "real" toolbar edges.
525  toolbar_bounds.Inset(-views::NonClientFrameView::kClientEdgeThickness, 0);
526  return toolbar_bounds;
527}
528
529gfx::Rect BrowserView::GetFindBarBoundingBox() const {
530  return GetBrowserViewLayout()->GetFindBarBoundingBox();
531}
532
533int BrowserView::GetTabStripHeight() const {
534  // We want to return tabstrip_->height(), but we might be called in the midst
535  // of layout, when that hasn't yet been updated to reflect the current state.
536  // So return what the tabstrip height _ought_ to be right now.
537  return IsTabStripVisible() ? tabstrip_->GetPreferredSize().height() : 0;
538}
539
540gfx::Point BrowserView::OffsetPointForToolbarBackgroundImage(
541    const gfx::Point& point) const {
542  // The background image starts tiling horizontally at the window left edge and
543  // vertically at the top edge of the horizontal tab strip (or where it would
544  // be).  We expect our parent's origin to be the window origin.
545  gfx::Point window_point(point + GetMirroredPosition().OffsetFromOrigin());
546  window_point.Offset(frame_->GetThemeBackgroundXInset(),
547                      -frame_->GetTopInset());
548  return window_point;
549}
550
551bool BrowserView::IsTabStripVisible() const {
552  if (immersive_mode_controller_->ShouldHideTopViews() &&
553      immersive_mode_controller_->ShouldHideTabIndicators())
554    return false;
555  return browser_->SupportsWindowFeature(Browser::FEATURE_TABSTRIP);
556}
557
558bool BrowserView::IsOffTheRecord() const {
559  return browser_->profile()->IsOffTheRecord();
560}
561
562bool BrowserView::IsGuestSession() const {
563  return browser_->profile()->IsGuestSession();
564}
565
566bool BrowserView::IsRegularOrGuestSession() const {
567  return profiles::IsRegularOrGuestSession(browser_.get());
568}
569
570int BrowserView::GetOTRIconResourceID() const {
571  int otr_resource_id = IDR_OTR_ICON;
572  if (ui::GetDisplayLayout() == ui::LAYOUT_TOUCH) {
573    if (IsFullscreen())
574      otr_resource_id = IDR_OTR_ICON_FULLSCREEN;
575#if defined(OS_WIN)
576    if (win8::IsSingleWindowMetroMode())
577      otr_resource_id = IDR_OTR_ICON_FULLSCREEN;
578#endif
579  }
580
581  return otr_resource_id;
582}
583
584int BrowserView::GetGuestIconResourceID() const {
585  return IDR_LOGIN_GUEST;
586}
587
588bool BrowserView::ShouldShowAvatar() const {
589  if (!IsBrowserTypeNormal())
590    return false;
591#if defined(OS_CHROMEOS)
592  if (IsOffTheRecord() && !IsGuestSession())
593    return true;
594
595  // Note: In case of the M-31 mode the window manager won't exist.
596  if (chrome::MultiUserWindowManager::GetMultiProfileMode() ==
597          chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED) {
598    // This function is called via BrowserNonClientFrameView::UpdateAvatarInfo
599    // during the creation of the BrowserWindow, so browser->window() will not
600    // yet be set. In this case we can safely return false.
601    if (!browser_->window())
602      return false;
603
604    // If the window is shown on a different desktop than the user, it should
605    // have the avatar icon.
606    aura::Window* window = browser_->window()->GetNativeWindow();
607
608    // Note: When the window manager the window is either on it's owners desktop
609    // (and shows no icon) or it is now (in which it will show an icon). So we
610    // can return here.
611    chrome::MultiUserWindowManager* window_manager =
612        chrome::MultiUserWindowManager::GetInstance();
613    return !window_manager->IsWindowOnDesktopOfUser(
614        window,
615        window_manager->GetWindowOwner(window));
616  }
617#else
618  if (IsOffTheRecord())  // Desktop guest is incognito and needs avatar.
619    return true;
620#endif
621  // Tests may not have a profile manager.
622  if (!g_browser_process->profile_manager())
623    return false;
624  ProfileInfoCache& cache =
625      g_browser_process->profile_manager()->GetProfileInfoCache();
626  if (cache.GetIndexOfProfileWithPath(browser_->profile()->GetPath()) ==
627      std::string::npos) {
628    return false;
629  }
630
631  return AvatarMenu::ShouldShowAvatarMenu();
632}
633
634bool BrowserView::GetAccelerator(int cmd_id, ui::Accelerator* accelerator) {
635  // We retrieve the accelerator information for standard accelerators
636  // for cut, copy and paste.
637  if (chrome::GetStandardAcceleratorForCommandId(cmd_id, accelerator))
638    return true;
639  // Else, we retrieve the accelerator information from the accelerator table.
640  for (std::map<ui::Accelerator, int>::const_iterator it =
641           accelerator_table_.begin(); it != accelerator_table_.end(); ++it) {
642    if (it->second == cmd_id) {
643      *accelerator = it->first;
644      return true;
645    }
646  }
647  // Else, we retrieve the accelerator information from Ash (if applicable).
648  return chrome::GetAshAcceleratorForCommandId(
649      cmd_id, browser_->host_desktop_type(), accelerator);
650}
651
652bool BrowserView::IsAcceleratorRegistered(const ui::Accelerator& accelerator) {
653  return accelerator_table_.find(accelerator) != accelerator_table_.end();
654}
655
656WebContents* BrowserView::GetActiveWebContents() const {
657  return browser_->tab_strip_model()->GetActiveWebContents();
658}
659
660gfx::ImageSkia BrowserView::GetOTRAvatarIcon() const {
661  return *GetThemeProvider()->GetImageSkiaNamed(GetOTRIconResourceID());
662}
663
664///////////////////////////////////////////////////////////////////////////////
665// BrowserView, BrowserWindow implementation:
666
667void BrowserView::Show() {
668  // If the window is already visible, just activate it.
669  if (frame_->IsVisible()) {
670    frame_->Activate();
671    return;
672  }
673
674  // Showing the window doesn't make the browser window active right away.
675  // This can cause SetFocusToLocationBar() to skip setting focus to the
676  // location bar. To avoid this we explicilty let SetFocusToLocationBar()
677  // know that it's ok to steal focus.
678  force_location_bar_focus_ = true;
679
680  // Setting the focus doesn't work when the window is invisible, so any focus
681  // initialization that happened before this will be lost.
682  //
683  // We really "should" restore the focus whenever the window becomes unhidden,
684  // but I think initializing is the only time where this can happen where
685  // there is some focus change we need to pick up, and this is easier than
686  // plumbing through an un-hide message all the way from the frame.
687  //
688  // If we do find there are cases where we need to restore the focus on show,
689  // that should be added and this should be removed.
690  RestoreFocus();
691
692  frame_->Show();
693
694  force_location_bar_focus_ = false;
695
696  browser()->OnWindowDidShow();
697
698  chrome::MaybeShowInvertBubbleView(browser_.get(), contents_container_);
699}
700
701void BrowserView::ShowInactive() {
702  if (frame_->IsVisible())
703    return;
704  frame_->ShowInactive();
705}
706
707void BrowserView::Hide() {
708  // Not implemented.
709}
710
711void BrowserView::SetBounds(const gfx::Rect& bounds) {
712  ExitFullscreen();
713  GetWidget()->SetBounds(bounds);
714}
715
716void BrowserView::Close() {
717  frame_->Close();
718}
719
720void BrowserView::Activate() {
721  frame_->Activate();
722}
723
724void BrowserView::Deactivate() {
725  frame_->Deactivate();
726}
727
728bool BrowserView::IsActive() const {
729  return frame_->IsActive();
730}
731
732void BrowserView::FlashFrame(bool flash) {
733  frame_->FlashFrame(flash);
734}
735
736bool BrowserView::IsAlwaysOnTop() const {
737  return false;
738}
739
740void BrowserView::SetAlwaysOnTop(bool always_on_top) {
741  // Not implemented for browser windows.
742  NOTIMPLEMENTED();
743}
744
745gfx::NativeWindow BrowserView::GetNativeWindow() {
746  // While the browser destruction is going on, the widget can already be gone,
747  // but utility functions like FindBrowserWithWindow will come here and crash.
748  // We short circuit therefore.
749  if (!GetWidget())
750    return NULL;
751  return GetWidget()->GetTopLevelWidget()->GetNativeWindow();
752}
753
754BrowserWindowTesting* BrowserView::GetBrowserWindowTesting() {
755  return this;
756}
757
758StatusBubble* BrowserView::GetStatusBubble() {
759  return status_bubble_.get();
760}
761
762namespace {
763  // Only used by ToolbarSizeChanged() below, but placed here because template
764  // arguments (to base::AutoReset<>) must have external linkage.
765  enum CallState { NORMAL, REENTRANT, REENTRANT_FORCE_FAST_RESIZE };
766}
767
768void BrowserView::UpdateTitleBar() {
769  frame_->UpdateWindowTitle();
770  if (ShouldShowWindowIcon() && !loading_animation_timer_.IsRunning())
771    frame_->UpdateWindowIcon();
772}
773
774void BrowserView::BookmarkBarStateChanged(
775    BookmarkBar::AnimateChangeType change_type) {
776  if (bookmark_bar_view_.get()) {
777    BookmarkBar::State new_state = browser_->bookmark_bar_state();
778
779    // We don't properly support animating the bookmark bar to and from the
780    // detached state in immersive fullscreen.
781    bool detached_changed = (new_state == BookmarkBar::DETACHED) ||
782        bookmark_bar_view_->IsDetached();
783    if (detached_changed && immersive_mode_controller_->IsEnabled())
784      change_type = BookmarkBar::DONT_ANIMATE_STATE_CHANGE;
785
786    bookmark_bar_view_->SetBookmarkBarState(new_state, change_type);
787  }
788  if (MaybeShowBookmarkBar(GetActiveWebContents()))
789    Layout();
790}
791
792void BrowserView::UpdateDevTools() {
793  UpdateDevToolsForContents(GetActiveWebContents(), true);
794  Layout();
795}
796
797void BrowserView::UpdateLoadingAnimations(bool should_animate) {
798  if (should_animate) {
799    if (!loading_animation_timer_.IsRunning()) {
800      // Loads are happening, and the timer isn't running, so start it.
801      last_animation_time_ = base::TimeTicks::Now();
802      loading_animation_timer_.Start(FROM_HERE,
803          TimeDelta::FromMilliseconds(kLoadingAnimationFrameTimeMs), this,
804          &BrowserView::LoadingAnimationCallback);
805    }
806  } else {
807    if (loading_animation_timer_.IsRunning()) {
808      last_animation_time_ = base::TimeTicks();
809      loading_animation_timer_.Stop();
810      // Loads are now complete, update the state if a task was scheduled.
811      LoadingAnimationCallback();
812    }
813  }
814}
815
816void BrowserView::SetStarredState(bool is_starred) {
817  GetLocationBarView()->SetStarToggled(is_starred);
818}
819
820void BrowserView::SetTranslateIconToggled(bool is_lit) {
821  GetLocationBarView()->SetTranslateIconToggled(is_lit);
822}
823
824void BrowserView::OnActiveTabChanged(content::WebContents* old_contents,
825                                     content::WebContents* new_contents,
826                                     int index,
827                                     int reason) {
828  DCHECK(new_contents);
829
830  // If |contents_container_| already has the correct WebContents, we can save
831  // some work.  This also prevents extra events from being reported by the
832  // Visibility API under Windows, as ChangeWebContents will briefly hide
833  // the WebContents window.
834  bool change_tab_contents =
835      contents_web_view_->web_contents() != new_contents;
836
837  // Update various elements that are interested in knowing the current
838  // WebContents.
839
840  // When we toggle the NTP floating bookmarks bar and/or the info bar,
841  // we don't want any WebContents to be attached, so that we
842  // avoid an unnecessary resize and re-layout of a WebContents.
843  if (change_tab_contents) {
844    contents_web_view_->SetWebContents(NULL);
845    devtools_web_view_->SetWebContents(NULL);
846  }
847  infobar_container_->ChangeInfoBarService(
848      InfoBarService::FromWebContents(new_contents));
849
850  if (old_contents && PermissionBubbleManager::FromWebContents(old_contents))
851    PermissionBubbleManager::FromWebContents(old_contents)->SetView(NULL);
852
853  if (new_contents && PermissionBubbleManager::FromWebContents(new_contents)) {
854    PermissionBubbleManager::FromWebContents(new_contents)->SetView(
855        permission_bubble_view_.get());
856  }
857
858  if (bookmark_bar_view_.get()) {
859    bookmark_bar_view_->SetBookmarkBarState(
860        browser_->bookmark_bar_state(),
861        BookmarkBar::DONT_ANIMATE_STATE_CHANGE);
862  }
863  UpdateUIForContents(new_contents);
864
865  // Layout for DevTools _before_ setting the both main and devtools WebContents
866  // to avoid toggling the size of any of them.
867  UpdateDevToolsForContents(new_contents, !change_tab_contents);
868
869  if (change_tab_contents) {
870    contents_web_view_->SetWebContents(new_contents);
871    // The second layout update should be no-op. It will just set the
872    // DevTools WebContents.
873    UpdateDevToolsForContents(new_contents, true);
874  }
875
876  if (!browser_->tab_strip_model()->closing_all() && GetWidget()->IsActive() &&
877      GetWidget()->IsVisible()) {
878    // We only restore focus if our window is visible, to avoid invoking blur
879    // handlers when we are eventually shown.
880    new_contents->GetView()->RestoreFocus();
881  }
882
883  // Update all the UI bits.
884  UpdateTitleBar();
885}
886
887void BrowserView::ZoomChangedForActiveTab(bool can_show_bubble) {
888  GetLocationBarView()->ZoomChangedForActiveTab(
889      can_show_bubble && !toolbar_->IsWrenchMenuShowing());
890}
891
892gfx::Rect BrowserView::GetRestoredBounds() const {
893  return frame_->GetRestoredBounds();
894}
895
896ui::WindowShowState BrowserView::GetRestoredState() const {
897  if (IsMaximized())
898    return ui::SHOW_STATE_MAXIMIZED;
899  if (IsMinimized())
900    return ui::SHOW_STATE_MINIMIZED;
901  return ui::SHOW_STATE_NORMAL;
902}
903
904gfx::Rect BrowserView::GetBounds() const {
905  return frame_->GetWindowBoundsInScreen();
906}
907
908bool BrowserView::IsMaximized() const {
909  return frame_->IsMaximized();
910}
911
912bool BrowserView::IsMinimized() const {
913  return frame_->IsMinimized();
914}
915
916void BrowserView::Maximize() {
917  frame_->Maximize();
918}
919
920void BrowserView::Minimize() {
921  frame_->Minimize();
922}
923
924void BrowserView::Restore() {
925  frame_->Restore();
926}
927
928void BrowserView::EnterFullscreen(
929    const GURL& url, FullscreenExitBubbleType bubble_type) {
930  if (IsFullscreen())
931    return;  // Nothing to do.
932
933  ProcessFullscreen(true, FOR_DESKTOP, url, bubble_type);
934}
935
936void BrowserView::ExitFullscreen() {
937  if (!IsFullscreen())
938    return;  // Nothing to do.
939
940  ProcessFullscreen(false, FOR_DESKTOP, GURL(), FEB_TYPE_NONE);
941}
942
943void BrowserView::UpdateFullscreenExitBubbleContent(
944    const GURL& url,
945    FullscreenExitBubbleType bubble_type) {
946  // Immersive mode has no exit bubble because it has a visible strip at the
947  // top that gives the user a hover target.
948  // TODO(jamescook): Figure out what to do with mouse-lock.
949  if (bubble_type == FEB_TYPE_NONE || ShouldUseImmersiveFullscreenForUrl(url)) {
950    fullscreen_bubble_.reset();
951  } else if (fullscreen_bubble_.get()) {
952    fullscreen_bubble_->UpdateContent(url, bubble_type);
953  } else {
954    fullscreen_bubble_.reset(new FullscreenExitBubbleViews(
955        this, url, bubble_type));
956  }
957}
958
959bool BrowserView::ShouldHideUIForFullscreen() const {
960#if defined(USE_ASH)
961  // Immersive mode needs UI for the slide-down top panel.
962  return IsFullscreen() && !immersive_mode_controller_->IsEnabled();
963#endif
964  return IsFullscreen();
965}
966
967bool BrowserView::IsFullscreen() const {
968  return frame_->IsFullscreen();
969}
970
971bool BrowserView::IsFullscreenBubbleVisible() const {
972  return fullscreen_bubble_ != NULL;
973}
974
975#if defined(OS_WIN)
976void BrowserView::SetMetroSnapMode(bool enable) {
977  HISTOGRAM_COUNTS("Metro.SnapModeToggle", enable);
978  ProcessFullscreen(enable, FOR_METRO, GURL(), FEB_TYPE_NONE);
979}
980
981bool BrowserView::IsInMetroSnapMode() const {
982  return false;
983}
984#endif  // defined(OS_WIN)
985
986void BrowserView::RestoreFocus() {
987  WebContents* selected_web_contents = GetActiveWebContents();
988  if (selected_web_contents)
989    selected_web_contents->GetView()->RestoreFocus();
990}
991
992void BrowserView::SetWindowSwitcherButton(views::Button* button) {
993  if (window_switcher_button_)
994    RemoveChildView(window_switcher_button_);
995  window_switcher_button_ = button;
996}
997
998void BrowserView::FullscreenStateChanged() {
999  CHECK(!IsFullscreen());
1000  ProcessFullscreen(false, FOR_DESKTOP, GURL(), FEB_TYPE_NONE);
1001}
1002
1003void BrowserView::ToolbarSizeChanged(bool is_animating) {
1004  // The call to InfoBarContainer::SetMaxTopArrowHeight() below can result in
1005  // reentrancy; |call_state| tracks whether we're reentrant.  We can't just
1006  // early-return in this case because we need to layout again so the infobar
1007  // container's bounds are set correctly.
1008  static CallState call_state = NORMAL;
1009
1010  // A reentrant call can (and should) use the fast resize path unless both it
1011  // and the normal call are both non-animating.
1012  bool use_fast_resize =
1013      is_animating || (call_state == REENTRANT_FORCE_FAST_RESIZE);
1014  if (use_fast_resize)
1015    contents_web_view_->SetFastResize(true);
1016  UpdateUIForContents(GetActiveWebContents());
1017  if (use_fast_resize)
1018    contents_web_view_->SetFastResize(false);
1019
1020  // Inform the InfoBarContainer that the distance to the location icon may have
1021  // changed.  We have to do this after the block above so that the toolbars are
1022  // laid out correctly for calculating the maximum arrow height below.
1023  {
1024    base::AutoReset<CallState> resetter(&call_state,
1025        is_animating ? REENTRANT_FORCE_FAST_RESIZE : REENTRANT);
1026    infobar_container_->SetMaxTopArrowHeight(GetMaxTopInfoBarArrowHeight());
1027  }
1028
1029  // When transitioning from animating to not animating we need to make sure the
1030  // contents_container_ gets layed out. If we don't do this and the bounds
1031  // haven't changed contents_container_ won't get a Layout out and we'll end up
1032  // with a gray rect because the clip wasn't updated.  Note that a reentrant
1033  // call never needs to do this, because after it returns, the normal call
1034  // wrapping it will do it.
1035  if ((call_state == NORMAL) && !is_animating) {
1036    contents_web_view_->InvalidateLayout();
1037    contents_container_->Layout();
1038  }
1039}
1040
1041LocationBar* BrowserView::GetLocationBar() const {
1042  return GetLocationBarView();
1043}
1044
1045void BrowserView::SetFocusToLocationBar(bool select_all) {
1046  // On Windows, changing focus to the location bar causes the browser
1047  // window to become active. This can steal focus if the user has
1048  // another window open already. On ChromeOS, changing focus makes a
1049  // view believe it has a focus even if the widget doens't have a
1050  // focus. Either cases, we need to ignore this when the browser
1051  // window isn't active.
1052  if (!force_location_bar_focus_ && !IsActive())
1053    return;
1054
1055  // Temporarily reveal the top-of-window views (if not already revealed) so
1056  // that the location bar view is visible and is considered focusable. If the
1057  // location bar view gains focus, |immersive_mode_controller_| will keep the
1058  // top-of-window views revealed.
1059  scoped_ptr<ImmersiveRevealedLock> focus_reveal_lock(
1060      immersive_mode_controller_->GetRevealedLock(
1061          ImmersiveModeController::ANIMATE_REVEAL_YES));
1062
1063  LocationBarView* location_bar = GetLocationBarView();
1064  if (location_bar->omnibox_view()->IsFocusable()) {
1065    // Location bar got focus.
1066    //
1067    // select_all is true when it's expected that the user may want to copy
1068    // the URL to the clipboard. If the URL is not being shown because the
1069    // origin chip is enabled, show it now to support the same functionality.
1070    if (select_all &&
1071        location_bar->GetToolbarModel()->WouldOmitURLDueToOriginChip())
1072      location_bar->omnibox_view()->ShowURL();
1073    else
1074      location_bar->FocusLocation(select_all);
1075  } else {
1076    // If none of location bar got focus, then clear focus.
1077    views::FocusManager* focus_manager = GetFocusManager();
1078    DCHECK(focus_manager);
1079    focus_manager->ClearFocus();
1080  }
1081}
1082
1083void BrowserView::UpdateReloadStopState(bool is_loading, bool force) {
1084  toolbar_->reload_button()->ChangeMode(
1085      is_loading ? ReloadButton::MODE_STOP : ReloadButton::MODE_RELOAD, force);
1086}
1087
1088void BrowserView::UpdateToolbar(content::WebContents* contents) {
1089  // We may end up here during destruction.
1090  if (toolbar_)
1091    toolbar_->Update(contents);
1092}
1093
1094void BrowserView::FocusToolbar() {
1095  // Temporarily reveal the top-of-window views (if not already revealed) so
1096  // that the toolbar is visible and is considered focusable. If the
1097  // toolbar gains focus, |immersive_mode_controller_| will keep the
1098  // top-of-window views revealed.
1099  scoped_ptr<ImmersiveRevealedLock> focus_reveal_lock(
1100      immersive_mode_controller_->GetRevealedLock(
1101          ImmersiveModeController::ANIMATE_REVEAL_YES));
1102
1103  // Start the traversal within the main toolbar. SetPaneFocus stores
1104  // the current focused view before changing focus.
1105  toolbar_->SetPaneFocus(NULL);
1106}
1107
1108void BrowserView::FocusBookmarksToolbar() {
1109  DCHECK(!immersive_mode_controller_->IsEnabled());
1110  if (bookmark_bar_view_.get() &&
1111      bookmark_bar_view_->visible() &&
1112      bookmark_bar_view_->GetPreferredSize().height() != 0) {
1113    bookmark_bar_view_->SetPaneFocusAndFocusDefault();
1114  }
1115}
1116
1117void BrowserView::FocusInfobars() {
1118  if (infobar_container_->child_count() > 0)
1119    infobar_container_->SetPaneFocusAndFocusDefault();
1120}
1121
1122void BrowserView::FocusAppMenu() {
1123  // Chrome doesn't have a traditional menu bar, but it has a menu button in the
1124  // main toolbar that plays the same role.  If the user presses a key that
1125  // would typically focus the menu bar, tell the toolbar to focus the menu
1126  // button.  If the user presses the key again, return focus to the previous
1127  // location.
1128  //
1129  // Not used on the Mac, which has a normal menu bar.
1130  if (toolbar_->IsAppMenuFocused()) {
1131    RestoreFocus();
1132  } else {
1133    DCHECK(!immersive_mode_controller_->IsEnabled());
1134    toolbar_->SetPaneFocusAndFocusAppMenu();
1135  }
1136}
1137
1138void BrowserView::RotatePaneFocus(bool forwards) {
1139  GetWidget()->GetFocusManager()->RotatePaneFocus(
1140      forwards ?
1141          views::FocusManager::kForward : views::FocusManager::kBackward,
1142      views::FocusManager::kWrap);
1143}
1144
1145void BrowserView::DestroyBrowser() {
1146  // After this returns other parts of Chrome are going to be shutdown. Close
1147  // the window now so that we are deleted immediately and aren't left holding
1148  // references to deleted objects.
1149  GetWidget()->RemoveObserver(this);
1150  GetLocationBar()->GetOmniboxView()->model()->popup_model()->RemoveObserver(
1151      this);
1152  frame_->CloseNow();
1153}
1154
1155bool BrowserView::IsBookmarkBarVisible() const {
1156  if (!browser_->SupportsWindowFeature(Browser::FEATURE_BOOKMARKBAR))
1157    return false;
1158  if (!bookmark_bar_view_.get())
1159    return false;
1160  if (bookmark_bar_view_->GetPreferredSize().height() == 0)
1161    return false;
1162  // New tab page needs visible bookmarks even when top-views are hidden.
1163  if (immersive_mode_controller_->ShouldHideTopViews() &&
1164      !bookmark_bar_view_->IsDetached())
1165    return false;
1166  return true;
1167}
1168
1169bool BrowserView::IsBookmarkBarAnimating() const {
1170  return bookmark_bar_view_.get() && bookmark_bar_view_->is_animating();
1171}
1172
1173bool BrowserView::IsTabStripEditable() const {
1174  return tabstrip_->IsTabStripEditable();
1175}
1176
1177bool BrowserView::IsToolbarVisible() const {
1178  if (immersive_mode_controller_->ShouldHideTopViews())
1179    return false;
1180  return browser_->SupportsWindowFeature(Browser::FEATURE_TOOLBAR) ||
1181         browser_->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR);
1182}
1183
1184gfx::Rect BrowserView::GetRootWindowResizerRect() const {
1185  // Views does not support resizer rects because they caused page cycler
1186  // performance regressions when they were added. See crrev.com/9654
1187  return gfx::Rect();
1188}
1189
1190void BrowserView::ConfirmAddSearchProvider(TemplateURL* template_url,
1191                                           Profile* profile) {
1192  chrome::EditSearchEngine(GetWidget()->GetNativeWindow(), template_url, NULL,
1193                           profile);
1194}
1195
1196void BrowserView::ShowUpdateChromeDialog() {
1197  UpdateRecommendedMessageBox::Show(GetWidget()->GetNativeWindow());
1198}
1199
1200void BrowserView::ShowBookmarkBubble(const GURL& url, bool already_bookmarked) {
1201  scoped_ptr<BookmarkBubbleDelegate> delegate;
1202  delegate.reset(new BookmarkBubbleSignInDelegate(browser_.get()));
1203
1204  BookmarkBubbleView::ShowBubble(GetToolbarView()->GetBookmarkBubbleAnchor(),
1205                                 bookmark_bar_view_.get(),
1206                                 delegate.Pass(),
1207                                 browser_->profile(),
1208                                 url,
1209                                 !already_bookmarked);
1210}
1211
1212void BrowserView::ShowBookmarkAppBubble(
1213    const WebApplicationInfo& web_app_info,
1214    const std::string& extension_id) {
1215  BookmarkAppBubbleView::ShowBubble(GetToolbarView(),
1216                                    browser_->profile(),
1217                                    web_app_info,
1218                                    extension_id);
1219}
1220
1221void BrowserView::ShowBookmarkPrompt() {
1222  GetLocationBarView()->ShowBookmarkPrompt();
1223}
1224
1225void BrowserView::ShowTranslateBubble(content::WebContents* web_contents,
1226                                      TranslateTabHelper::TranslateStep step,
1227                                      TranslateErrors::Type error_type) {
1228  TranslateTabHelper* translate_tab_helper =
1229      TranslateTabHelper::FromWebContents(web_contents);
1230  LanguageState& language_state = translate_tab_helper->GetLanguageState();
1231  language_state.SetTranslateEnabled(true);
1232
1233  TranslateBubbleView::ShowBubble(
1234      GetToolbarView()->GetTranslateBubbleAnchor(), web_contents, step,
1235      error_type);
1236}
1237
1238#if defined(ENABLE_ONE_CLICK_SIGNIN)
1239void BrowserView::ShowOneClickSigninBubble(
1240    OneClickSigninBubbleType type,
1241    const base::string16& email,
1242    const base::string16& error_message,
1243    const StartSyncCallback& start_sync_callback) {
1244  scoped_ptr<OneClickSigninBubbleDelegate> delegate;
1245  delegate.reset(new OneClickSigninBubbleLinksDelegate(browser()));
1246
1247  views::View* anchor_view;
1248  if (type == BrowserWindow::ONE_CLICK_SIGNIN_BUBBLE_TYPE_BUBBLE)
1249    anchor_view = toolbar_->app_menu();
1250  else
1251    anchor_view = toolbar_->location_bar();
1252
1253  OneClickSigninBubbleView::ShowBubble(type, email, error_message,
1254                                       delegate.Pass(), anchor_view,
1255                                       start_sync_callback);
1256}
1257#endif
1258
1259void BrowserView::SetDownloadShelfVisible(bool visible) {
1260  // This can be called from the superclass destructor, when it destroys our
1261  // child views. At that point, browser_ is already gone.
1262  if (browser_ == NULL)
1263    return;
1264
1265  if (visible && IsDownloadShelfVisible() != visible) {
1266    // Invoke GetDownloadShelf to force the shelf to be created.
1267    GetDownloadShelf();
1268  }
1269
1270  if (browser_ != NULL)
1271    browser_->UpdateDownloadShelfVisibility(visible);
1272
1273  // SetDownloadShelfVisible can force-close the shelf, so make sure we lay out
1274  // everything correctly, as if the animation had finished. This doesn't
1275  // matter for showing the shelf, as the show animation will do it.
1276  ToolbarSizeChanged(false);
1277}
1278
1279bool BrowserView::IsDownloadShelfVisible() const {
1280  return download_shelf_.get() && download_shelf_->IsShowing();
1281}
1282
1283DownloadShelf* BrowserView::GetDownloadShelf() {
1284  if (!download_shelf_.get()) {
1285    download_shelf_.reset(new DownloadShelfView(browser_.get(), this));
1286    download_shelf_->set_owned_by_client();
1287    GetBrowserViewLayout()->set_download_shelf(download_shelf_.get());
1288  }
1289  return download_shelf_.get();
1290}
1291
1292void BrowserView::ConfirmBrowserCloseWithPendingDownloads(
1293    int download_count,
1294    Browser::DownloadClosePreventionType dialog_type,
1295    bool app_modal,
1296    const base::Callback<void(bool)>& callback) {
1297  DownloadInProgressDialogView::Show(
1298      GetNativeWindow(), download_count, dialog_type, app_modal, callback);
1299}
1300
1301void BrowserView::UserChangedTheme() {
1302  frame_->FrameTypeChanged();
1303}
1304
1305int BrowserView::GetExtraRenderViewHeight() const {
1306  // Currently this is only used on linux.
1307  return 0;
1308}
1309
1310void BrowserView::WebContentsFocused(WebContents* contents) {
1311  if (contents_web_view_->GetWebContents() == contents)
1312    contents_web_view_->OnWebContentsFocused(contents);
1313  else
1314    devtools_web_view_->OnWebContentsFocused(contents);
1315}
1316
1317void BrowserView::ShowWebsiteSettings(Profile* profile,
1318                                      content::WebContents* web_contents,
1319                                      const GURL& url,
1320                                      const content::SSLStatus& ssl) {
1321  WebsiteSettingsPopupView::ShowPopup(
1322      GetLocationBarView()->GetLocationIconView(), profile,
1323      web_contents, url, ssl, browser_.get());
1324}
1325
1326void BrowserView::ShowAppMenu() {
1327  // Keep the top-of-window views revealed as long as the app menu is visible.
1328  scoped_ptr<ImmersiveRevealedLock> revealed_lock(
1329      immersive_mode_controller_->GetRevealedLock(
1330          ImmersiveModeController::ANIMATE_REVEAL_NO));
1331
1332  toolbar_->app_menu()->Activate();
1333}
1334
1335bool BrowserView::PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
1336                                         bool* is_keyboard_shortcut) {
1337  *is_keyboard_shortcut = false;
1338
1339  if ((event.type != blink::WebInputEvent::RawKeyDown) &&
1340      (event.type != blink::WebInputEvent::KeyUp)) {
1341    return false;
1342  }
1343
1344  views::FocusManager* focus_manager = GetFocusManager();
1345  DCHECK(focus_manager);
1346
1347  if (focus_manager->shortcut_handling_suspended())
1348    return false;
1349
1350  ui::Accelerator accelerator(
1351      static_cast<ui::KeyboardCode>(event.windowsKeyCode),
1352      content::GetModifiersFromNativeWebKeyboardEvent(event));
1353  if (event.type == blink::WebInputEvent::KeyUp)
1354    accelerator.set_type(ui::ET_KEY_RELEASED);
1355
1356  // What we have to do here is as follows:
1357  // - If the |browser_| is for an app, do nothing.
1358  // - If the |browser_| is not for an app, and the |accelerator| is not
1359  //   associated with the browser (e.g. an Ash shortcut), process it.
1360  // - If the |browser_| is not for an app, and the |accelerator| is associated
1361  //   with the browser, and it is a reserved one (e.g. Ctrl+w), process it.
1362  // - If the |browser_| is not for an app, and the |accelerator| is associated
1363  //   with the browser, and it is not a reserved one, do nothing.
1364
1365  if (browser_->is_app()) {
1366    // Let all keys fall through to a v1 app's web content, even accelerators.
1367    // We don't have to flip |is_keyboard_shortcut| here. If we do that, the app
1368    // might not be able to see a subsequent Char event. See OnHandleInputEvent
1369    // in content/renderer/render_widget.cc for details.
1370    return false;
1371  }
1372
1373  chrome::BrowserCommandController* controller = browser_->command_controller();
1374
1375  // Here we need to retrieve the command id (if any) associated to the
1376  // keyboard event. Instead of looking up the command id in the
1377  // |accelerator_table_| by ourselves, we block the command execution of
1378  // the |browser_| object then send the keyboard event to the
1379  // |focus_manager| as if we are activating an accelerator key.
1380  // Then we can retrieve the command id from the |browser_| object.
1381  bool original_block_command_state = controller->block_command_execution();
1382  controller->SetBlockCommandExecution(true);
1383  // If the |accelerator| is a non-browser shortcut (e.g. Ash shortcut), the
1384  // command execution cannot be blocked and true is returned. However, it is
1385  // okay as long as is_app() is false. See comments in this function.
1386  const bool processed = focus_manager->ProcessAccelerator(accelerator);
1387  const int id = controller->GetLastBlockedCommand(NULL);
1388  controller->SetBlockCommandExecution(original_block_command_state);
1389
1390  // Executing the command may cause |this| object to be destroyed.
1391  if (controller->IsReservedCommandOrKey(id, event)) {
1392    UpdateAcceleratorMetrics(accelerator, id);
1393    return chrome::ExecuteCommand(browser_.get(), id);
1394  }
1395
1396  if (id != -1) {
1397    // |accelerator| is a non-reserved browser shortcut (e.g. Ctrl+f).
1398    if (event.type == blink::WebInputEvent::RawKeyDown)
1399      *is_keyboard_shortcut = true;
1400  } else if (processed) {
1401    // |accelerator| is a non-browser shortcut (e.g. F4-F10 on Ash). Report
1402    // that we handled it.
1403    return true;
1404  }
1405
1406  return false;
1407}
1408
1409void BrowserView::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
1410  unhandled_keyboard_event_handler_.HandleKeyboardEvent(event,
1411                                                        GetFocusManager());
1412}
1413
1414// TODO(devint): http://b/issue?id=1117225 Cut, Copy, and Paste are always
1415// enabled in the page menu regardless of whether the command will do
1416// anything. When someone selects the menu item, we just act as if they hit
1417// the keyboard shortcut for the command by sending the associated key press
1418// to windows. The real fix to this bug is to disable the commands when they
1419// won't do anything. We'll need something like an overall clipboard command
1420// manager to do that.
1421void BrowserView::Cut() {
1422  // If a WebContent is focused, call RenderWidgetHost::Cut. Otherwise, e.g. if
1423  // Omnibox is focused, send a Ctrl+x key event to Chrome. Using RWH interface
1424  // rather than the fake key event for a WebContent is important since the fake
1425  // event might be consumed by the web content (crbug.com/137908).
1426  DoCutCopyPaste(&content::RenderWidgetHost::Cut, IDS_APP_CUT);
1427}
1428
1429void BrowserView::Copy() {
1430  DoCutCopyPaste(&content::RenderWidgetHost::Copy, IDS_APP_COPY);
1431}
1432
1433void BrowserView::Paste() {
1434  DoCutCopyPaste(&content::RenderWidgetHost::Paste, IDS_APP_PASTE);
1435}
1436
1437WindowOpenDisposition BrowserView::GetDispositionForPopupBounds(
1438    const gfx::Rect& bounds) {
1439#if defined(OS_WIN)
1440  // If we are in Win8's single window Metro mode, we can't allow popup windows.
1441  return win8::IsSingleWindowMetroMode() ? NEW_BACKGROUND_TAB : NEW_POPUP;
1442#else
1443  return NEW_POPUP;
1444#endif
1445}
1446
1447FindBar* BrowserView::CreateFindBar() {
1448  return chrome::CreateFindBar(this);
1449}
1450
1451WebContentsModalDialogHost* BrowserView::GetWebContentsModalDialogHost() {
1452  return GetBrowserViewLayout()->GetWebContentsModalDialogHost();
1453}
1454
1455///////////////////////////////////////////////////////////////////////////////
1456// BrowserView, BrowserWindowTesting implementation:
1457
1458BookmarkBarView* BrowserView::GetBookmarkBarView() const {
1459  return bookmark_bar_view_.get();
1460}
1461
1462LocationBarView* BrowserView::GetLocationBarView() const {
1463  return toolbar_ ? toolbar_->location_bar() : NULL;
1464}
1465
1466views::View* BrowserView::GetTabContentsContainerView() const {
1467  return contents_web_view_;
1468}
1469
1470ToolbarView* BrowserView::GetToolbarView() const {
1471  return toolbar_;
1472}
1473
1474///////////////////////////////////////////////////////////////////////////////
1475// BrowserView, TabStripModelObserver implementation:
1476
1477void BrowserView::TabDetachedAt(WebContents* contents, int index) {
1478  if (PermissionBubbleManager::FromWebContents(contents))
1479    PermissionBubbleManager::FromWebContents(contents)->SetView(NULL);
1480
1481  // We use index here rather than comparing |contents| because by this time
1482  // the model has already removed |contents| from its list, so
1483  // browser_->GetActiveWebContents() will return NULL or something else.
1484  if (index == browser_->tab_strip_model()->active_index()) {
1485    // We need to reset the current tab contents to NULL before it gets
1486    // freed. This is because the focus manager performs some operations
1487    // on the selected WebContents when it is removed.
1488    contents_web_view_->SetWebContents(NULL);
1489    infobar_container_->ChangeInfoBarService(NULL);
1490    UpdateDevToolsForContents(NULL, true);
1491  }
1492}
1493
1494void BrowserView::TabDeactivated(WebContents* contents) {
1495  if (PermissionBubbleManager::FromWebContents(contents))
1496    PermissionBubbleManager::FromWebContents(contents)->SetView(NULL);
1497
1498  // We do not store the focus when closing the tab to work-around bug 4633.
1499  // Some reports seem to show that the focus manager and/or focused view can
1500  // be garbage at that point, it is not clear why.
1501  if (!contents->IsBeingDestroyed())
1502    contents->GetView()->StoreFocus();
1503}
1504
1505void BrowserView::TabStripEmpty() {
1506  // Make sure all optional UI is removed before we are destroyed, otherwise
1507  // there will be consequences (since our view hierarchy will still have
1508  // references to freed views).
1509  UpdateUIForContents(NULL);
1510}
1511
1512///////////////////////////////////////////////////////////////////////////////
1513// BrowserView, ui::AcceleratorProvider implementation:
1514
1515bool BrowserView::GetAcceleratorForCommandId(int command_id,
1516                                             ui::Accelerator* accelerator) {
1517  // Let's let the ToolbarView own the canonical implementation of this method.
1518  return toolbar_->GetAcceleratorForCommandId(command_id, accelerator);
1519}
1520
1521///////////////////////////////////////////////////////////////////////////////
1522// BrowserView, views::WidgetDelegate implementation:
1523
1524bool BrowserView::CanResize() const {
1525  return true;
1526}
1527
1528bool BrowserView::CanMaximize() const {
1529  return true;
1530}
1531
1532bool BrowserView::CanActivate() const {
1533  if (!AppModalDialogQueue::GetInstance()->active_dialog() ||
1534      !AppModalDialogQueue::GetInstance()->active_dialog()->native_dialog())
1535    return true;
1536
1537#if defined(USE_AURA) && defined(OS_CHROMEOS)
1538  // On Aura window manager controls all windows so settings focus via PostTask
1539  // will make only worse because posted task will keep trying to steal focus.
1540  AppModalDialogQueue::GetInstance()->ActivateModalDialog();
1541#else
1542  // If another browser is app modal, flash and activate the modal browser. This
1543  // has to be done in a post task, otherwise if the user clicked on a window
1544  // that doesn't have the modal dialog the windows keep trying to get the focus
1545  // from each other on Windows. http://crbug.com/141650.
1546  base::MessageLoop::current()->PostTask(
1547      FROM_HERE,
1548      base::Bind(&BrowserView::ActivateAppModalDialog,
1549                 activate_modal_dialog_factory_.GetWeakPtr()));
1550#endif
1551  return false;
1552}
1553
1554base::string16 BrowserView::GetWindowTitle() const {
1555  return browser_->GetWindowTitleForCurrentTab();
1556}
1557
1558base::string16 BrowserView::GetAccessibleWindowTitle() const {
1559  if (IsOffTheRecord()) {
1560    return l10n_util::GetStringFUTF16(
1561        IDS_ACCESSIBLE_INCOGNITO_WINDOW_TITLE_FORMAT,
1562        GetWindowTitle());
1563  }
1564  return GetWindowTitle();
1565}
1566
1567views::View* BrowserView::GetInitiallyFocusedView() {
1568  return NULL;
1569}
1570
1571bool BrowserView::ShouldShowWindowTitle() const {
1572  // For Ash only, app host windows do not show an icon, crbug.com/119411.
1573  // Child windows (i.e. popups) do show an icon.
1574  if (browser_->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH &&
1575      browser_->is_app() && browser_->app_type() == Browser::APP_TYPE_HOST)
1576    return false;
1577
1578  return browser_->SupportsWindowFeature(Browser::FEATURE_TITLEBAR);
1579}
1580
1581gfx::ImageSkia BrowserView::GetWindowAppIcon() {
1582  if (browser_->is_app()) {
1583    WebContents* contents = browser_->tab_strip_model()->GetActiveWebContents();
1584    extensions::TabHelper* extensions_tab_helper =
1585        contents ? extensions::TabHelper::FromWebContents(contents) : NULL;
1586    if (extensions_tab_helper && extensions_tab_helper->GetExtensionAppIcon())
1587      return gfx::ImageSkia::CreateFrom1xBitmap(
1588          *extensions_tab_helper->GetExtensionAppIcon());
1589  }
1590
1591  return GetWindowIcon();
1592}
1593
1594gfx::ImageSkia BrowserView::GetWindowIcon() {
1595  if (browser_->is_app() || browser_->is_type_popup())
1596    return browser_->GetCurrentPageIcon().AsImageSkia();
1597  return gfx::ImageSkia();
1598}
1599
1600bool BrowserView::ShouldShowWindowIcon() const {
1601  // For Ash only, app host windows do not show an icon, crbug.com/119411.
1602  // Child windows (i.e. popups) do show an icon.
1603  if (browser_->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH &&
1604      browser_->is_app() && browser_->app_type() == Browser::APP_TYPE_HOST)
1605    return false;
1606
1607  return browser_->SupportsWindowFeature(Browser::FEATURE_TITLEBAR);
1608}
1609
1610bool BrowserView::ExecuteWindowsCommand(int command_id) {
1611  // This function handles WM_SYSCOMMAND, WM_APPCOMMAND, and WM_COMMAND.
1612#if defined(OS_WIN)
1613  if (command_id == IDC_DEBUG_FRAME_TOGGLE)
1614    GetWidget()->DebugToggleFrameType();
1615
1616  // In Windows 8 metro mode prevent sizing and moving.
1617  if (win8::IsSingleWindowMetroMode()) {
1618    // Windows uses the 4 lower order bits of |notification_code| for type-
1619    // specific information so we must exclude this when comparing.
1620    static const int sc_mask = 0xFFF0;
1621    if (((command_id & sc_mask) == SC_MOVE) ||
1622        ((command_id & sc_mask) == SC_SIZE) ||
1623        ((command_id & sc_mask) == SC_MAXIMIZE))
1624      return true;
1625  }
1626#endif
1627  // Translate WM_APPCOMMAND command ids into a command id that the browser
1628  // knows how to handle.
1629  int command_id_from_app_command = GetCommandIDForAppCommandID(command_id);
1630  if (command_id_from_app_command != -1)
1631    command_id = command_id_from_app_command;
1632
1633  return chrome::ExecuteCommand(browser_.get(), command_id);
1634}
1635
1636std::string BrowserView::GetWindowName() const {
1637  return chrome::GetWindowPlacementKey(browser_.get());
1638}
1639
1640void BrowserView::SaveWindowPlacement(const gfx::Rect& bounds,
1641                                      ui::WindowShowState show_state) {
1642  // If IsFullscreen() is true, we've just changed into fullscreen mode, and
1643  // we're catching the going-into-fullscreen sizing and positioning calls,
1644  // which we want to ignore.
1645  if (!ShouldSaveOrRestoreWindowPos())
1646    return;
1647
1648  if (!IsFullscreen() && chrome::ShouldSaveWindowPlacement(browser_.get())) {
1649    WidgetDelegate::SaveWindowPlacement(bounds, show_state);
1650    chrome::SaveWindowPlacement(browser_.get(), bounds, show_state);
1651  }
1652}
1653
1654bool BrowserView::GetSavedWindowPlacement(
1655    const views::Widget* widget,
1656    gfx::Rect* bounds,
1657    ui::WindowShowState* show_state) const {
1658  if (!ShouldSaveOrRestoreWindowPos())
1659    return false;
1660  chrome::GetSavedWindowBoundsAndShowState(browser_.get(), bounds, show_state);
1661
1662  if (browser_->is_type_popup() &&
1663      !browser_->is_app() &&
1664      !browser_->is_devtools()) {
1665    // This is non-app popup window. The value passed in |bounds| represents
1666    // two pieces of information:
1667    // - the position of the window, in screen coordinates (outer position).
1668    // - the size of the content area (inner size).
1669    // We need to use these values to determine the appropriate size and
1670    // position of the resulting window.
1671    if (IsToolbarVisible()) {
1672      // If we're showing the toolbar, we need to adjust |*bounds| to include
1673      // its desired height, since the toolbar is considered part of the
1674      // window's client area as far as GetWindowBoundsForClientBounds is
1675      // concerned...
1676      bounds->set_height(
1677          bounds->height() + toolbar_->GetPreferredSize().height());
1678    }
1679
1680    gfx::Rect window_rect = frame_->non_client_view()->
1681        GetWindowBoundsForClientBounds(*bounds);
1682    window_rect.set_origin(bounds->origin());
1683
1684    // When we are given x/y coordinates of 0 on a created popup window,
1685    // assume none were given by the window.open() command.
1686    if (window_rect.x() == 0 && window_rect.y() == 0) {
1687      gfx::Size size = window_rect.size();
1688      window_rect.set_origin(
1689          WindowSizer::GetDefaultPopupOrigin(size,
1690                                             browser_->host_desktop_type()));
1691    }
1692
1693    *bounds = window_rect;
1694    *show_state = ui::SHOW_STATE_NORMAL;
1695  }
1696
1697  // We return true because we can _always_ locate reasonable bounds using the
1698  // WindowSizer, and we don't want to trigger the Window's built-in "size to
1699  // default" handling because the browser window has no default preferred
1700  // size.
1701  return true;
1702}
1703
1704views::View* BrowserView::GetContentsView() {
1705  return contents_web_view_;
1706}
1707
1708views::ClientView* BrowserView::CreateClientView(views::Widget* widget) {
1709  return this;
1710}
1711
1712void BrowserView::OnWidgetActivationChanged(views::Widget* widget,
1713                                            bool active) {
1714  if (active)
1715    BrowserList::SetLastActive(browser_.get());
1716}
1717
1718void BrowserView::OnWindowBeginUserBoundsChange() {
1719  WebContents* web_contents = GetActiveWebContents();
1720  if (!web_contents)
1721    return;
1722  web_contents->GetRenderViewHost()->NotifyMoveOrResizeStarted();
1723}
1724
1725void BrowserView::OnWidgetMove() {
1726  if (!initialized_) {
1727    // Creating the widget can trigger a move. Ignore it until we've initialized
1728    // things.
1729    return;
1730  }
1731
1732  // Cancel any tabstrip animations, some of them may be invalidated by the
1733  // window being repositioned.
1734  // Comment out for one cycle to see if this fixes dist tests.
1735  // tabstrip_->DestroyDragController();
1736
1737  // status_bubble_ may be NULL if this is invoked during construction.
1738  if (status_bubble_.get())
1739    status_bubble_->Reposition();
1740
1741  BookmarkBubbleView::Hide();
1742
1743  // Close the omnibox popup, if any.
1744  LocationBarView* location_bar_view = GetLocationBarView();
1745  if (location_bar_view)
1746    location_bar_view->GetOmniboxView()->CloseOmniboxPopup();
1747}
1748
1749views::Widget* BrowserView::GetWidget() {
1750  return View::GetWidget();
1751}
1752
1753const views::Widget* BrowserView::GetWidget() const {
1754  return View::GetWidget();
1755}
1756
1757void BrowserView::GetAccessiblePanes(std::vector<views::View*>* panes) {
1758  // This should be in the order of pane traversal of the panes using F6
1759  // (Windows) or Ctrl+Back/Forward (Chrome OS).  If one of these is
1760  // invisible or has no focusable children, it will be automatically
1761  // skipped.
1762  panes->push_back(toolbar_);
1763  if (bookmark_bar_view_.get())
1764    panes->push_back(bookmark_bar_view_.get());
1765  if (infobar_container_)
1766    panes->push_back(infobar_container_);
1767  if (download_shelf_.get())
1768    panes->push_back(download_shelf_.get());
1769  panes->push_back(GetTabContentsContainerView());
1770  if (devtools_web_view_->visible())
1771    panes->push_back(devtools_web_view_);
1772}
1773
1774///////////////////////////////////////////////////////////////////////////////
1775// BrowserView, views::ClientView overrides:
1776
1777bool BrowserView::CanClose() {
1778  // You cannot close a frame for which there is an active originating drag
1779  // session.
1780  if (tabstrip_ && !tabstrip_->IsTabStripCloseable())
1781    return false;
1782
1783  // Give beforeunload handlers the chance to cancel the close before we hide
1784  // the window below.
1785  if (!browser_->ShouldCloseWindow())
1786    return false;
1787
1788  bool fast_tab_closing_enabled =
1789    CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableFastUnload);
1790
1791  if (!browser_->tab_strip_model()->empty()) {
1792    // Tab strip isn't empty.  Hide the frame (so it appears to have closed
1793    // immediately) and close all the tabs, allowing the renderers to shut
1794    // down. When the tab strip is empty we'll be called back again.
1795    frame_->Hide();
1796    browser_->OnWindowClosing();
1797    if (fast_tab_closing_enabled)
1798      browser_->tab_strip_model()->CloseAllTabs();
1799    return false;
1800  } else if (fast_tab_closing_enabled &&
1801        !browser_->HasCompletedUnloadProcessing()) {
1802    // The browser needs to finish running unload handlers.
1803    // Hide the frame (so it appears to have closed immediately), and
1804    // the browser will call us back again when it is ready to close.
1805    frame_->Hide();
1806    return false;
1807  }
1808
1809  // Empty TabStripModel, it's now safe to allow the Window to be closed.
1810  content::NotificationService::current()->Notify(
1811      chrome::NOTIFICATION_WINDOW_CLOSED,
1812      content::Source<gfx::NativeWindow>(frame_->GetNativeWindow()),
1813      content::NotificationService::NoDetails());
1814  return true;
1815}
1816
1817int BrowserView::NonClientHitTest(const gfx::Point& point) {
1818  return GetBrowserViewLayout()->NonClientHitTest(point);
1819}
1820
1821gfx::Size BrowserView::GetMinimumSize() {
1822  return GetBrowserViewLayout()->GetMinimumSize();
1823}
1824
1825///////////////////////////////////////////////////////////////////////////////
1826// BrowserView, views::View overrides:
1827
1828const char* BrowserView::GetClassName() const {
1829  return kViewClassName;
1830}
1831
1832void BrowserView::Layout() {
1833  if (!initialized_ || in_process_fullscreen_)
1834    return;
1835
1836  views::View::Layout();
1837
1838  // TODO(jamescook): Why was this in the middle of layout code?
1839  toolbar_->location_bar()->omnibox_view()->SetFocusable(IsToolbarVisible());
1840}
1841
1842void BrowserView::PaintChildren(gfx::Canvas* canvas) {
1843  // Paint the |infobar_container_| last so that it may paint its
1844  // overlapping tabs.
1845  for (int i = 0; i < child_count(); ++i) {
1846    View* child = child_at(i);
1847    if (child != infobar_container_ && !child->layer())
1848      child->Paint(canvas);
1849  }
1850
1851  infobar_container_->Paint(canvas);
1852}
1853
1854void BrowserView::ViewHierarchyChanged(
1855    const ViewHierarchyChangedDetails& details) {
1856  if (!initialized_ && details.is_add && details.child == this && GetWidget()) {
1857    InitViews();
1858    initialized_ = true;
1859  }
1860}
1861
1862void BrowserView::ChildPreferredSizeChanged(View* child) {
1863  Layout();
1864}
1865
1866void BrowserView::GetAccessibleState(ui::AccessibleViewState* state) {
1867  state->role = ui::AccessibilityTypes::ROLE_CLIENT;
1868}
1869
1870///////////////////////////////////////////////////////////////////////////////
1871// BrowserView, ui::AcceleratorTarget overrides:
1872
1873bool BrowserView::AcceleratorPressed(const ui::Accelerator& accelerator) {
1874#if defined(OS_CHROMEOS)
1875  // If accessibility is enabled, stop speech and return false so that key
1876  // combinations involving Search can be used for extra accessibility
1877  // functionality.
1878  if (accelerator.key_code() == ui::VKEY_LWIN &&
1879      g_browser_process->local_state()->GetBoolean(
1880          prefs::kSpokenFeedbackEnabled)) {
1881    TtsController::GetInstance()->Stop();
1882    return false;
1883  }
1884#endif
1885
1886  std::map<ui::Accelerator, int>::const_iterator iter =
1887      accelerator_table_.find(accelerator);
1888  DCHECK(iter != accelerator_table_.end());
1889  int command_id = iter->second;
1890
1891  chrome::BrowserCommandController* controller = browser_->command_controller();
1892  if (!controller->block_command_execution())
1893    UpdateAcceleratorMetrics(accelerator, command_id);
1894  return chrome::ExecuteCommand(browser_.get(), command_id);
1895}
1896
1897///////////////////////////////////////////////////////////////////////////////
1898// BrowserView, OmniboxPopupModelObserver overrides:
1899void BrowserView::OnOmniboxPopupShownOrHidden() {
1900  infobar_container_->SetMaxTopArrowHeight(GetMaxTopInfoBarArrowHeight());
1901}
1902
1903///////////////////////////////////////////////////////////////////////////////
1904// BrowserView, InfoBarContainer::Delegate overrides:
1905
1906SkColor BrowserView::GetInfoBarSeparatorColor() const {
1907  // NOTE: Keep this in sync with ToolbarView::OnPaint()!
1908  return (IsTabStripVisible() || !frame_->ShouldUseNativeFrame()) ?
1909      ThemeProperties::GetDefaultColor(
1910          ThemeProperties::COLOR_TOOLBAR_SEPARATOR) :
1911      SK_ColorBLACK;
1912}
1913
1914void BrowserView::InfoBarContainerStateChanged(bool is_animating) {
1915  ToolbarSizeChanged(is_animating);
1916}
1917
1918bool BrowserView::DrawInfoBarArrows(int* x) const {
1919  if (x) {
1920    gfx::Point anchor(toolbar_->location_bar()->GetLocationBarAnchorPoint());
1921    ConvertPointToTarget(toolbar_->location_bar(), this, &anchor);
1922    *x = anchor.x();
1923  }
1924  return true;
1925}
1926
1927void BrowserView::OnSysColorChange() {
1928  chrome::MaybeShowInvertBubbleView(browser_.get(), contents_container_);
1929}
1930
1931void BrowserView::InitViews() {
1932  GetWidget()->AddObserver(this);
1933
1934  // Stow a pointer to this object onto the window handle so that we can get at
1935  // it later when all we have is a native view.
1936  GetWidget()->SetNativeWindowProperty(kBrowserViewKey, this);
1937
1938  // Stow a pointer to the browser's profile onto the window handle so that we
1939  // can get it later when all we have is a native view.
1940  GetWidget()->SetNativeWindowProperty(Profile::kProfileKey,
1941                                       browser_->profile());
1942
1943  // Start a hung plugin window detector for this browser object (as long as
1944  // hang detection is not disabled).
1945  if (!CommandLine::ForCurrentProcess()->HasSwitch(
1946          switches::kDisableHangMonitor)) {
1947    InitHangMonitor();
1948  }
1949
1950  LoadAccelerators();
1951
1952  infobar_container_ = new InfoBarContainerView(this);
1953  AddChildView(infobar_container_);
1954
1955  contents_web_view_ = new ContentsWebView(browser_->profile());
1956  contents_web_view_->set_id(VIEW_ID_TAB_CONTAINER);
1957  contents_web_view_->SetEmbedFullscreenWidgetMode(
1958      implicit_cast<content::WebContentsDelegate*>(browser_.get())->
1959          EmbedsFullscreenWidget());
1960
1961  devtools_web_view_ = new views::WebView(browser_->profile());
1962  devtools_web_view_->set_id(VIEW_ID_DEV_TOOLS_DOCKED);
1963  devtools_web_view_->SetVisible(false);
1964
1965  contents_container_ = new views::View();
1966  contents_container_->set_background(views::Background::CreateSolidBackground(
1967      ThemeProperties::GetDefaultColor(
1968          ThemeProperties::COLOR_CONTROL_BACKGROUND)));
1969  contents_container_->AddChildView(devtools_web_view_);
1970  contents_container_->AddChildView(contents_web_view_);
1971  contents_container_->SetLayoutManager(new ContentsLayoutManager(
1972      devtools_web_view_, contents_web_view_));
1973  AddChildView(contents_container_);
1974  set_contents_view(contents_container_);
1975
1976  // Top container holds tab strip and toolbar and lives at the front of the
1977  // view hierarchy.
1978  top_container_ = new TopContainerView(this);
1979  AddChildView(top_container_);
1980
1981  // TabStrip takes ownership of the controller.
1982  BrowserTabStripController* tabstrip_controller =
1983      new BrowserTabStripController(browser_.get(),
1984                                    browser_->tab_strip_model());
1985  tabstrip_ = new TabStrip(tabstrip_controller);
1986  top_container_->AddChildView(tabstrip_);
1987  tabstrip_controller->InitFromModel(tabstrip_);
1988
1989  toolbar_ = new ToolbarView(browser_.get());
1990  top_container_->AddChildView(toolbar_);
1991  toolbar_->Init();
1992
1993  InitStatusBubble();
1994  InitPermissionBubbleView();
1995
1996  // Create do-nothing view for the sake of controlling the z-order of the find
1997  // bar widget.
1998  find_bar_host_view_ = new View();
1999  AddChildView(find_bar_host_view_);
2000
2001  if (window_switcher_button_)
2002    AddChildView(window_switcher_button_);
2003
2004  immersive_mode_controller_->Init(this);
2005
2006  BrowserViewLayout* browser_view_layout = new BrowserViewLayout;
2007  browser_view_layout->Init(new BrowserViewLayoutDelegateImpl(this),
2008                            browser(),
2009                            this,
2010                            top_container_,
2011                            tabstrip_,
2012                            toolbar_,
2013                            infobar_container_,
2014                            contents_container_,
2015                            GetContentsLayoutManager(),
2016                            immersive_mode_controller_.get());
2017  SetLayoutManager(browser_view_layout);
2018
2019#if defined(OS_WIN)
2020  // Create a custom JumpList and add it to an observer of TabRestoreService
2021  // so we can update the custom JumpList when a tab is added or removed.
2022  if (JumpList::Enabled()) {
2023    load_complete_listener_.reset(new LoadCompleteListener(this));
2024  }
2025#endif
2026
2027  GetLocationBar()->GetOmniboxView()->model()->popup_model()->AddObserver(this);
2028}
2029
2030void BrowserView::LoadingAnimationCallback() {
2031  base::TimeTicks now = base::TimeTicks::Now();
2032  if (!last_animation_time_.is_null()) {
2033    UMA_HISTOGRAM_TIMES(
2034        "Tabs.LoadingAnimationTime",
2035        now - last_animation_time_);
2036  }
2037  last_animation_time_ = now;
2038  if (browser_->is_type_tabbed()) {
2039    // Loading animations are shown in the tab for tabbed windows.  We check the
2040    // browser type instead of calling IsTabStripVisible() because the latter
2041    // will return false for fullscreen windows, but we still need to update
2042    // their animations (so that when they come out of fullscreen mode they'll
2043    // be correct).
2044    tabstrip_->UpdateLoadingAnimations();
2045  } else if (ShouldShowWindowIcon()) {
2046    // ... or in the window icon area for popups and app windows.
2047    WebContents* web_contents =
2048        browser_->tab_strip_model()->GetActiveWebContents();
2049    // GetActiveWebContents can return NULL for example under Purify when
2050    // the animations are running slowly and this function is called on a timer
2051    // through LoadingAnimationCallback.
2052    frame_->UpdateThrobber(web_contents && web_contents->IsLoading());
2053  }
2054}
2055
2056void BrowserView::OnLoadCompleted() {
2057#if defined(OS_WIN)
2058  DCHECK(!jumplist_);
2059  jumplist_ = new JumpList();
2060  jumplist_->AddObserver(browser_->profile());
2061#endif
2062}
2063
2064BrowserViewLayout* BrowserView::GetBrowserViewLayout() const {
2065  return static_cast<BrowserViewLayout*>(GetLayoutManager());
2066}
2067
2068ContentsLayoutManager* BrowserView::GetContentsLayoutManager() const {
2069  return static_cast<ContentsLayoutManager*>(
2070      contents_container_->GetLayoutManager());
2071}
2072
2073bool BrowserView::MaybeShowBookmarkBar(WebContents* contents) {
2074  bool show_bookmark_bar = contents &&
2075      browser_->SupportsWindowFeature(Browser::FEATURE_BOOKMARKBAR);
2076  if (!show_bookmark_bar && !bookmark_bar_view_.get())
2077    return false;
2078  if (!bookmark_bar_view_.get()) {
2079    bookmark_bar_view_.reset(new BookmarkBarView(browser_.get(), this));
2080    bookmark_bar_view_->set_owned_by_client();
2081    bookmark_bar_view_->set_background(
2082        new BookmarkExtensionBackground(this,
2083                                        bookmark_bar_view_.get(),
2084                                        browser_.get()));
2085    bookmark_bar_view_->SetBookmarkBarState(
2086        browser_->bookmark_bar_state(),
2087        BookmarkBar::DONT_ANIMATE_STATE_CHANGE);
2088    GetBrowserViewLayout()->set_bookmark_bar(bookmark_bar_view_.get());
2089  }
2090  bookmark_bar_view_->SetVisible(show_bookmark_bar);
2091  bookmark_bar_view_->SetPageNavigator(contents);
2092
2093  // Update parenting for the bookmark bar. This may detach it from all views.
2094  bool needs_layout = false;
2095  views::View* new_parent = NULL;
2096  if (show_bookmark_bar) {
2097    if (bookmark_bar_view_->IsDetached())
2098      new_parent = this;
2099    else
2100      new_parent = top_container_;
2101  }
2102  if (new_parent != bookmark_bar_view_->parent()) {
2103    SetBookmarkBarParent(new_parent);
2104    needs_layout = true;
2105  }
2106
2107  // Check for updates to the desired size.
2108  if (bookmark_bar_view_->GetPreferredSize().height() !=
2109      bookmark_bar_view_->height())
2110    needs_layout = true;
2111
2112  return needs_layout;
2113}
2114
2115void BrowserView::SetBookmarkBarParent(views::View* new_parent) {
2116  if (new_parent == this) {
2117    // Add it underneath |top_container_| or at the end if top container isn't
2118    // found.
2119    int top_container_index = GetIndexOf(top_container_);
2120    if (top_container_index >= 0)
2121      AddChildViewAt(bookmark_bar_view_.get(), top_container_index);
2122    else
2123      AddChildView(bookmark_bar_view_.get());
2124  } else if (new_parent) {
2125    // No special stacking is required for other parents.
2126    new_parent->AddChildView(bookmark_bar_view_.get());
2127  } else {
2128    // Bookmark bar is being detached from all views because it is hidden.
2129    bookmark_bar_view_->parent()->RemoveChildView(bookmark_bar_view_.get());
2130  }
2131}
2132
2133bool BrowserView::MaybeShowInfoBar(WebContents* contents) {
2134  // TODO(beng): Remove this function once the interface between
2135  //             InfoBarContainer, DownloadShelfView and WebContents and this
2136  //             view is sorted out.
2137  return true;
2138}
2139
2140void BrowserView::UpdateDevToolsForContents(
2141    WebContents* web_contents, bool update_devtools_web_contents) {
2142  DevToolsWindow* new_devtools_window = web_contents ?
2143      DevToolsWindow::GetDockedInstanceForInspectedTab(web_contents) : NULL;
2144
2145  // Replace devtools WebContents.
2146  WebContents* new_contents = new_devtools_window ?
2147      new_devtools_window->web_contents() : NULL;
2148  if (devtools_web_view_->web_contents() != new_contents &&
2149      update_devtools_web_contents) {
2150    devtools_web_view_->SetWebContents(new_contents);
2151  }
2152
2153  if (!devtools_window_ && new_devtools_window &&
2154      !devtools_focus_tracker_.get()) {
2155    // Install devtools focus tracker when dev tools window is shown for the
2156    // first time.
2157    devtools_focus_tracker_.reset(
2158        new views::ExternalFocusTracker(devtools_web_view_,
2159                                        GetFocusManager()));
2160  }
2161
2162  // Restore focus to the last focused view when hiding devtools window.
2163  if (devtools_window_ && !new_devtools_window &&
2164      devtools_focus_tracker_.get()) {
2165    devtools_focus_tracker_->FocusLastFocusedExternalView();
2166    devtools_focus_tracker_.reset();
2167  }
2168
2169  devtools_window_ = new_devtools_window;
2170  if (devtools_window_) {
2171    devtools_web_view_->SetPreferredSize(devtools_window_->GetMinimumSize());
2172    devtools_web_view_->SetVisible(true);
2173    GetContentsLayoutManager()->SetContentsResizingStrategy(
2174        devtools_window_->GetContentsResizingStrategy());
2175  } else {
2176    devtools_web_view_->SetVisible(false);
2177    GetContentsLayoutManager()->SetContentsResizingStrategy(
2178        DevToolsContentsResizingStrategy());
2179  }
2180  contents_container_->Layout();
2181}
2182
2183void BrowserView::UpdateUIForContents(WebContents* contents) {
2184  bool needs_layout = MaybeShowBookmarkBar(contents);
2185  // TODO(jamescook): This function always returns true. Remove it and figure
2186  // out when layout is actually required.
2187  needs_layout |= MaybeShowInfoBar(contents);
2188  if (needs_layout)
2189    Layout();
2190}
2191
2192void BrowserView::ProcessFullscreen(bool fullscreen,
2193                                    FullscreenType type,
2194                                    const GURL& url,
2195                                    FullscreenExitBubbleType bubble_type) {
2196  if (in_process_fullscreen_)
2197    return;
2198  in_process_fullscreen_ = true;
2199
2200  // Reduce jankiness during the following position changes by:
2201  //   * Hiding the window until it's in the final position
2202  //   * Ignoring all intervening Layout() calls, which resize the webpage and
2203  //     thus are slow and look ugly (enforced via |in_process_fullscreen_|).
2204  LocationBarView* location_bar = GetLocationBarView();
2205
2206  if (type == FOR_METRO || !fullscreen) {
2207    // Hide the fullscreen bubble as soon as possible, since the mode toggle can
2208    // take enough time for the user to notice.
2209    fullscreen_bubble_.reset();
2210  }
2211
2212  if (fullscreen) {
2213    // Move focus out of the location bar if necessary.
2214    views::FocusManager* focus_manager = GetFocusManager();
2215    DCHECK(focus_manager);
2216    // Look for focus in the location bar itself or any child view.
2217    if (location_bar->Contains(focus_manager->GetFocusedView()))
2218      focus_manager->ClearFocus();
2219  }
2220#if defined(OS_WIN) && !defined(USE_AURA)
2221  views::ScopedFullscreenVisibility visibility(frame_->GetNativeView());
2222#endif
2223
2224  if (type == FOR_METRO) {
2225#if defined(OS_WIN) && !defined(USE_AURA)
2226    // Enter metro snap mode.
2227    static_cast<views::NativeWidgetWin*>(
2228        frame_->native_widget())->SetMetroSnapFullscreen(fullscreen);
2229#endif
2230  } else {
2231    // Toggle fullscreen mode.
2232    frame_->SetFullscreen(fullscreen);
2233  }
2234
2235  // Enable immersive before the browser refreshes its list of enabled commands.
2236  if (ShouldUseImmersiveFullscreenForUrl(url))
2237    immersive_mode_controller_->SetEnabled(fullscreen);
2238
2239  browser_->WindowFullscreenStateChanged();
2240
2241  if (fullscreen && !chrome::IsRunningInAppMode() && type != FOR_METRO)
2242    UpdateFullscreenExitBubbleContent(url, bubble_type);
2243
2244  // Undo our anti-jankiness hacks and force a re-layout. We also need to
2245  // recompute the height of the infobar top arrow because toggling in and out
2246  // of fullscreen changes it. Calling ToolbarSizeChanged() will do both these
2247  // things since it computes the arrow height directly and forces a layout
2248  // indirectly via UpdateUIForContents(). Reset |in_process_fullscreen_| in
2249  // order to let the layout occur.
2250  in_process_fullscreen_ = false;
2251  ToolbarSizeChanged(false);
2252}
2253
2254bool BrowserView::ShouldUseImmersiveFullscreenForUrl(const GURL& url) const {
2255#if defined(OS_CHROMEOS)
2256  // Kiosk mode needs the whole screen.
2257  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kKioskMode))
2258    return false;
2259  bool is_browser_fullscreen = url.is_empty();
2260  if (ash::switches::UseImmersiveFullscreenForAllWindows())
2261    return is_browser_fullscreen;
2262  else
2263    return is_browser_fullscreen && IsBrowserTypeNormal();
2264#else
2265  return false;
2266#endif
2267}
2268
2269void BrowserView::LoadAccelerators() {
2270  views::FocusManager* focus_manager = GetFocusManager();
2271  DCHECK(focus_manager);
2272
2273  // Let's fill our own accelerator table.
2274  const bool is_app_mode = chrome::IsRunningInForcedAppMode();
2275  const std::vector<chrome::AcceleratorMapping> accelerator_list(
2276      chrome::GetAcceleratorList());
2277  for (std::vector<chrome::AcceleratorMapping>::const_iterator it =
2278           accelerator_list.begin(); it != accelerator_list.end(); ++it) {
2279    // In app mode, only allow accelerators of white listed commands to pass
2280    // through.
2281    if (is_app_mode && !chrome::IsCommandAllowedInAppMode(it->command_id))
2282      continue;
2283
2284    ui::Accelerator accelerator(it->keycode, it->modifiers);
2285    accelerator_table_[accelerator] = it->command_id;
2286
2287    // Also register with the focus manager.
2288    focus_manager->RegisterAccelerator(
2289        accelerator, ui::AcceleratorManager::kNormalPriority, this);
2290  }
2291}
2292
2293int BrowserView::GetCommandIDForAppCommandID(int app_command_id) const {
2294#if defined(OS_WIN)
2295  switch (app_command_id) {
2296    // NOTE: The order here matches the APPCOMMAND declaration order in the
2297    // Windows headers.
2298    case APPCOMMAND_BROWSER_BACKWARD: return IDC_BACK;
2299    case APPCOMMAND_BROWSER_FORWARD:  return IDC_FORWARD;
2300    case APPCOMMAND_BROWSER_REFRESH:  return IDC_RELOAD;
2301    case APPCOMMAND_BROWSER_HOME:     return IDC_HOME;
2302    case APPCOMMAND_BROWSER_STOP:     return IDC_STOP;
2303    case APPCOMMAND_BROWSER_SEARCH:   return IDC_FOCUS_SEARCH;
2304    case APPCOMMAND_HELP:             return IDC_HELP_PAGE_VIA_KEYBOARD;
2305    case APPCOMMAND_NEW:              return IDC_NEW_TAB;
2306    case APPCOMMAND_OPEN:             return IDC_OPEN_FILE;
2307    case APPCOMMAND_CLOSE:            return IDC_CLOSE_TAB;
2308    case APPCOMMAND_SAVE:             return IDC_SAVE_PAGE;
2309    case APPCOMMAND_PRINT:            return IDC_PRINT;
2310    case APPCOMMAND_COPY:             return IDC_COPY;
2311    case APPCOMMAND_CUT:              return IDC_CUT;
2312    case APPCOMMAND_PASTE:            return IDC_PASTE;
2313
2314      // TODO(pkasting): http://b/1113069 Handle these.
2315    case APPCOMMAND_UNDO:
2316    case APPCOMMAND_REDO:
2317    case APPCOMMAND_SPELL_CHECK:
2318    default:                          return -1;
2319  }
2320#else
2321  // App commands are Windows-specific so there's nothing to do here.
2322  return -1;
2323#endif
2324}
2325
2326void BrowserView::InitHangMonitor() {
2327#if defined(OS_WIN)
2328  PrefService* pref_service = g_browser_process->local_state();
2329  if (!pref_service)
2330    return;
2331
2332  int plugin_message_response_timeout =
2333      pref_service->GetInteger(prefs::kPluginMessageResponseTimeout);
2334  int hung_plugin_detect_freq =
2335      pref_service->GetInteger(prefs::kHungPluginDetectFrequency);
2336#if defined(USE_AURA)
2337  HWND window = GetWidget()->GetNativeView()->GetDispatcher()->host()->
2338      GetAcceleratedWidget();
2339#else
2340  HWND window = GetWidget()->GetNativeView();
2341#endif
2342  if ((hung_plugin_detect_freq > 0) &&
2343      hung_window_detector_.Initialize(window,
2344                                       plugin_message_response_timeout)) {
2345    ticker_.set_tick_interval(hung_plugin_detect_freq);
2346    ticker_.RegisterTickHandler(&hung_window_detector_);
2347    ticker_.Start();
2348
2349    pref_service->SetInteger(prefs::kPluginMessageResponseTimeout,
2350                             plugin_message_response_timeout);
2351    pref_service->SetInteger(prefs::kHungPluginDetectFrequency,
2352                             hung_plugin_detect_freq);
2353  }
2354#endif
2355}
2356
2357void BrowserView::UpdateAcceleratorMetrics(const ui::Accelerator& accelerator,
2358                                           int command_id) {
2359  const ui::KeyboardCode key_code = accelerator.key_code();
2360  if (command_id == IDC_HELP_PAGE_VIA_KEYBOARD && key_code == ui::VKEY_F1)
2361    content::RecordAction(UserMetricsAction("ShowHelpTabViaF1"));
2362
2363  if (command_id == IDC_BOOKMARK_PAGE)
2364    UMA_HISTOGRAM_ENUMERATION("Bookmarks.EntryPoint",
2365                              BOOKMARK_ENTRY_POINT_ACCELERATOR,
2366                              BOOKMARK_ENTRY_POINT_LIMIT);
2367
2368#if defined(OS_CHROMEOS)
2369  // Collect information about the relative popularity of various accelerators
2370  // on Chrome OS.
2371  switch (command_id) {
2372    case IDC_BACK:
2373      if (key_code == ui::VKEY_BACK)
2374        content::RecordAction(UserMetricsAction("Accel_Back_Backspace"));
2375      else if (key_code == ui::VKEY_BROWSER_BACK)
2376        content::RecordAction(UserMetricsAction("Accel_Back_F1"));
2377      else if (key_code == ui::VKEY_LEFT)
2378        content::RecordAction(UserMetricsAction("Accel_Back_Left"));
2379      break;
2380    case IDC_FORWARD:
2381      if (key_code == ui::VKEY_BACK)
2382        content::RecordAction(UserMetricsAction("Accel_Forward_Backspace"));
2383      else if (key_code == ui::VKEY_BROWSER_FORWARD)
2384        content::RecordAction(UserMetricsAction("Accel_Forward_F2"));
2385      else if (key_code == ui::VKEY_RIGHT)
2386        content::RecordAction(UserMetricsAction("Accel_Forward_Right"));
2387      break;
2388    case IDC_RELOAD:
2389    case IDC_RELOAD_IGNORING_CACHE:
2390      if (key_code == ui::VKEY_R)
2391        content::RecordAction(UserMetricsAction("Accel_Reload_R"));
2392      else if (key_code == ui::VKEY_BROWSER_REFRESH)
2393        content::RecordAction(UserMetricsAction("Accel_Reload_F3"));
2394      break;
2395    case IDC_FOCUS_LOCATION:
2396      if (key_code == ui::VKEY_D)
2397        content::RecordAction(UserMetricsAction("Accel_FocusLocation_D"));
2398      else if (key_code == ui::VKEY_L)
2399        content::RecordAction(UserMetricsAction("Accel_FocusLocation_L"));
2400      break;
2401    case IDC_FOCUS_SEARCH:
2402      if (key_code == ui::VKEY_E)
2403        content::RecordAction(UserMetricsAction("Accel_FocusSearch_E"));
2404      else if (key_code == ui::VKEY_K)
2405        content::RecordAction(UserMetricsAction("Accel_FocusSearch_K"));
2406      break;
2407    default:
2408      // Do nothing.
2409      break;
2410  }
2411#endif
2412}
2413
2414// static
2415BrowserWindow* BrowserWindow::CreateBrowserWindow(Browser* browser) {
2416  // Create the view and the frame. The frame will attach itself via the view
2417  // so we don't need to do anything with the pointer.
2418  BrowserView* view = new BrowserView();
2419  view->Init(browser);
2420  (new BrowserFrame(view))->InitBrowserFrame();
2421  view->GetWidget()->non_client_view()->SetAccessibleName(
2422      l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
2423  return view;
2424}
2425
2426// static
2427chrome::HostDesktopType BrowserWindow::AdjustHostDesktopType(
2428    chrome::HostDesktopType desktop_type) {
2429  return NativeBrowserFrameFactory::AdjustHostDesktopType(desktop_type);
2430}
2431
2432void BrowserView::ShowAvatarBubble(WebContents* web_contents,
2433                                   const gfx::Rect& rect) {
2434  gfx::Point origin(rect.origin());
2435  views::View::ConvertPointToScreen(GetTabContentsContainerView(), &origin);
2436  gfx::Rect bounds(origin, rect.size());
2437
2438  AvatarMenuBubbleView::ShowBubble(this, views::BubbleBorder::TOP_RIGHT,
2439      views::BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, bounds, browser_.get());
2440}
2441
2442void BrowserView::ShowAvatarBubbleFromAvatarButton() {
2443  if (switches::IsNewProfileManagement()) {
2444    NewAvatarButton* button = frame_->GetNewAvatarMenuButton();
2445    if (button) {
2446      gfx::Point origin;
2447      views::View::ConvertPointToScreen(button, &origin);
2448      gfx::Rect bounds(origin, size());
2449
2450      ProfileChooserView::ShowBubble(
2451          button, views::BubbleBorder::TOP_RIGHT,
2452          views::BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, bounds, browser());
2453    }
2454  } else {
2455    AvatarMenuButton* button = frame_->GetAvatarMenuButton();
2456    if (button)
2457      button->ShowAvatarBubble();
2458  }
2459}
2460
2461void BrowserView::ShowPasswordGenerationBubble(
2462    const gfx::Rect& rect,
2463    const autofill::PasswordForm& form,
2464    autofill::PasswordGenerator* password_generator) {
2465  // Create a rect in the content bounds that the bubble will point to.
2466  gfx::Point origin(rect.origin());
2467  views::View::ConvertPointToScreen(GetTabContentsContainerView(), &origin);
2468  gfx::Rect bounds(origin, rect.size());
2469
2470  // Create the bubble.
2471  WebContents* web_contents = GetActiveWebContents();
2472  if (!web_contents)
2473    return;
2474
2475  PasswordGenerationBubbleView* bubble = new PasswordGenerationBubbleView(
2476      form,
2477      bounds,
2478      this,
2479      web_contents->GetRenderViewHost(),
2480      ChromePasswordManagerClient::GetManagerFromWebContents(web_contents),
2481      password_generator,
2482      browser_.get(),
2483      GetWidget()->GetThemeProvider());
2484
2485  views::BubbleDelegateView::CreateBubble(bubble);
2486  bubble->SetAlignment(views::BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR);
2487  bubble->GetWidget()->Show();
2488}
2489
2490void BrowserView::OverscrollUpdate(int delta_y) {
2491  if (scroll_end_effect_controller_)
2492    scroll_end_effect_controller_->OverscrollUpdate(delta_y);
2493}
2494
2495int BrowserView::GetRenderViewHeightInsetWithDetachedBookmarkBar() {
2496  if (browser_->bookmark_bar_state() != BookmarkBar::DETACHED ||
2497      !bookmark_bar_view_.get() || !bookmark_bar_view_->IsDetached()) {
2498    return 0;
2499  }
2500  // Don't use bookmark_bar_view_->height() which won't be the final height if
2501  // the bookmark bar is animating.
2502  return chrome::kNTPBookmarkBarHeight -
2503      bookmark_bar_view_->GetFullyDetachedToolbarOverlap();
2504}
2505
2506void BrowserView::DoCutCopyPaste(void (content::RenderWidgetHost::*method)(),
2507                                 int command_id) {
2508  WebContents* contents = browser_->tab_strip_model()->GetActiveWebContents();
2509  if (!contents)
2510    return;
2511  if (DoCutCopyPasteForWebContents(contents, method))
2512    return;
2513
2514  DevToolsWindow* devtools_window =
2515      DevToolsWindow::GetDockedInstanceForInspectedTab(contents);
2516  if (devtools_window &&
2517      DoCutCopyPasteForWebContents(devtools_window->web_contents(), method)) {
2518    return;
2519  }
2520
2521  views::FocusManager* focus_manager = GetFocusManager();
2522  views::View* focused = focus_manager->GetFocusedView();
2523  if (focused &&
2524      (!strcmp(focused->GetClassName(), views::Textfield::kViewClassName) ||
2525       !strcmp(focused->GetClassName(), OmniboxViewViews::kViewClassName))) {
2526    views::Textfield* textfield = static_cast<views::Textfield*>(focused);
2527    textfield->ExecuteCommand(command_id);
2528  }
2529}
2530
2531bool BrowserView::DoCutCopyPasteForWebContents(
2532    WebContents* contents,
2533    void (content::RenderWidgetHost::*method)()) {
2534  gfx::NativeView native_view = contents->GetView()->GetContentNativeView();
2535  if (!native_view)
2536    return false;
2537#if defined(USE_AURA)
2538  if (native_view->HasFocus()) {
2539#elif defined(OS_WIN)
2540  if (native_view == ::GetFocus()) {
2541#endif
2542    (contents->GetRenderViewHost()->*method)();
2543    return true;
2544  }
2545
2546  return false;
2547}
2548
2549void BrowserView::ActivateAppModalDialog() const {
2550  // If another browser is app modal, flash and activate the modal browser.
2551  AppModalDialog* active_dialog =
2552      AppModalDialogQueue::GetInstance()->active_dialog();
2553  if (!active_dialog)
2554    return;
2555
2556  Browser* modal_browser =
2557      chrome::FindBrowserWithWebContents(active_dialog->web_contents());
2558  if (modal_browser && (browser_ != modal_browser)) {
2559    modal_browser->window()->FlashFrame(true);
2560    modal_browser->window()->Activate();
2561  }
2562
2563  AppModalDialogQueue::GetInstance()->ActivateModalDialog();
2564}
2565
2566int BrowserView::GetMaxTopInfoBarArrowHeight() {
2567  int top_arrow_height = 0;
2568  // Only show the arrows when not in fullscreen and when there's no omnibox
2569  // popup.
2570  if (!IsFullscreen() &&
2571      !GetLocationBar()->GetOmniboxView()->model()->popup_model()->IsOpen()) {
2572    gfx::Point icon_bottom(
2573        toolbar_->location_bar()->GetLocationBarAnchorPoint());
2574    ConvertPointToTarget(toolbar_->location_bar(), this, &icon_bottom);
2575    gfx::Point infobar_top(0, infobar_container_->GetVerticalOverlap(NULL));
2576    ConvertPointToTarget(infobar_container_, this, &infobar_top);
2577    top_arrow_height = infobar_top.y() - icon_bottom.y();
2578  }
2579  return top_arrow_height;
2580}
2581