1// Copyright (c) 2011 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/glass_browser_frame_view.h"
6
7#include "base/command_line.h"
8#include "base/utf_string_conversions.h"
9#include "chrome/app/chrome_command_ids.h"
10#include "chrome/app/chrome_dll_resource.h"
11#include "chrome/browser/prefs/pref_service.h"
12#include "chrome/browser/profiles/profile.h"
13#include "chrome/browser/themes/theme_service.h"
14#include "chrome/browser/ui/views/frame/browser_view.h"
15#include "chrome/browser/ui/views/profile_menu_button.h"
16#include "chrome/browser/ui/views/profile_menu_model.h"
17#include "chrome/browser/ui/views/profile_tag_view.h"
18#include "chrome/browser/ui/views/tabs/side_tab_strip.h"
19#include "chrome/browser/ui/views/tabs/tab.h"
20#include "chrome/browser/ui/views/tabs/tab_strip.h"
21#include "chrome/common/chrome_switches.h"
22#include "chrome/common/pref_names.h"
23#include "content/common/notification_service.h"
24#include "grit/app_resources.h"
25#include "grit/theme_resources.h"
26#include "ui/base/resource/resource_bundle.h"
27#include "ui/base/theme_provider.h"
28#include "ui/gfx/canvas_skia.h"
29#include "ui/gfx/icon_util.h"
30#include "views/window/client_view.h"
31#include "views/window/window_resources.h"
32
33HICON GlassBrowserFrameView::throbber_icons_[
34    GlassBrowserFrameView::kThrobberIconCount];
35
36namespace {
37// There are 3 px of client edge drawn inside the outer frame borders.
38const int kNonClientBorderThickness = 3;
39// Vertical tabs have 4 px border.
40const int kNonClientVerticalTabStripBorderThickness = 4;
41// Besides the frame border, there's another 11 px of empty space atop the
42// window in restored mode, to use to drag the window around.
43const int kNonClientRestoredExtraThickness = 11;
44// In the window corners, the resize areas don't actually expand bigger, but the
45// 16 px at the end of the top and bottom edges triggers diagonal resizing.
46const int kResizeAreaCornerSize = 16;
47// The OTR avatar ends 2 px above the bottom of the tabstrip (which, given the
48// way the tabstrip draws its bottom edge, will appear like a 1 px gap to the
49// user).
50const int kOTRBottomSpacing = 2;
51// There are 2 px on each side of the OTR avatar (between the frame border and
52// it on the left, and between it and the tabstrip on the right).
53const int kOTRSideSpacing = 2;
54// The content left/right images have a shadow built into them.
55const int kContentEdgeShadowThickness = 2;
56// The top 1 px of the tabstrip is shadow; in maximized mode we push this off
57// the top of the screen so the tabs appear flush against the screen edge.
58const int kTabstripTopShadowThickness = 1;
59// In restored mode, the New Tab button isn't at the same height as the caption
60// buttons, but the space will look cluttered if it actually slides under them,
61// so we stop it when the gap between the two is down to 5 px.
62const int kNewTabCaptionRestoredSpacing = 5;
63// In maximized mode, where the New Tab button and the caption buttons are at
64// similar vertical coordinates, we need to reserve a larger, 16 px gap to avoid
65// looking too cluttered.
66const int kNewTabCaptionMaximizedSpacing = 16;
67// Menu should display below the profile button tag image on the frame. This
68// offset size depends on whether the frame is in glass or opaque mode.
69const int kMenuDisplayOffset = 7;
70// Y position for profile button inside the frame.
71const int kProfileButtonYPosition = 2;
72// Y position for profile tag inside the frame.
73const int kProfileTagYPosition = 1;
74// Offset y position of profile button and tag by this amount when maximized.
75const int kProfileElementMaximizedYOffset = 6;
76}
77
78///////////////////////////////////////////////////////////////////////////////
79// GlassBrowserFrameView, public:
80
81GlassBrowserFrameView::GlassBrowserFrameView(BrowserFrame* frame,
82                                             BrowserView* browser_view)
83    : BrowserNonClientFrameView(),
84      frame_(frame),
85      browser_view_(browser_view),
86      throbber_running_(false),
87      throbber_frame_(0) {
88  if (frame_->GetWindow()->window_delegate()->ShouldShowWindowIcon())
89    InitThrobberIcons();
90  // If multi-profile is enabled set up profile button and login notifications.
91  const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
92  if (browser_command_line.HasSwitch(switches::kMultiProfiles) &&
93      !browser_view->ShouldShowOffTheRecordAvatar()) {
94    RegisterLoginNotifications();
95    profile_button_.reset(new views::ProfileMenuButton(NULL, std::wstring(),
96                                                       this));
97    profile_button_->SetVisible(false);
98    profile_tag_.reset(new views::ProfileTagView(frame_,
99                                                 profile_button_.get()));
100    profile_tag_->SetVisible(false);
101    AddChildView(profile_tag_.get());
102    AddChildView(profile_button_.get());
103  }
104}
105
106GlassBrowserFrameView::~GlassBrowserFrameView() {
107}
108
109///////////////////////////////////////////////////////////////////////////////
110// GlassBrowserFrameView, BrowserNonClientFrameView implementation:
111
112gfx::Rect GlassBrowserFrameView::GetBoundsForTabStrip(
113    views::View* tabstrip) const {
114  if (browser_view_->UseVerticalTabs()) {
115    gfx::Size ps = tabstrip->GetPreferredSize();
116    return gfx::Rect(NonClientBorderThickness(),
117        NonClientTopBorderHeight(false, false), ps.width(),
118        browser_view_->height());
119  }
120  int minimize_button_offset =
121      std::min(frame_->GetMinimizeButtonOffset(), width());
122  int tabstrip_x = browser_view_->ShouldShowOffTheRecordAvatar() ?
123      (otr_avatar_bounds_.right() + kOTRSideSpacing) :
124      NonClientBorderThickness();
125  // In RTL languages, we have moved an avatar icon left by the size of window
126  // controls to prevent it from being rendered over them. So, we use its x
127  // position to move this tab strip left when maximized. Also, we can render
128  // a tab strip until the left end of this window without considering the size
129  // of window controls in RTL languages.
130  if (base::i18n::IsRTL()) {
131    if (!browser_view_->ShouldShowOffTheRecordAvatar() &&
132        frame_->GetWindow()->IsMaximized())
133      tabstrip_x += otr_avatar_bounds_.x();
134    minimize_button_offset = width();
135  }
136  int maximized_spacing =
137      kNewTabCaptionMaximizedSpacing +
138      (show_profile_button() && profile_button_->IsVisible() ?
139          profile_button_->GetPreferredSize().width() +
140              views::ProfileMenuButton::kProfileTagHorizontalSpacing : 0);
141  int tabstrip_width = minimize_button_offset - tabstrip_x -
142      (frame_->GetWindow()->IsMaximized() ?
143          maximized_spacing : kNewTabCaptionRestoredSpacing);
144  return gfx::Rect(tabstrip_x, GetHorizontalTabStripVerticalOffset(false),
145                   std::max(0, tabstrip_width),
146                   tabstrip->GetPreferredSize().height());
147}
148
149int GlassBrowserFrameView::GetHorizontalTabStripVerticalOffset(
150    bool restored) const {
151  return NonClientTopBorderHeight(restored, true);
152}
153
154void GlassBrowserFrameView::UpdateThrobber(bool running) {
155  if (throbber_running_) {
156    if (running) {
157      DisplayNextThrobberFrame();
158    } else {
159      StopThrobber();
160    }
161  } else if (running) {
162    StartThrobber();
163  }
164}
165
166///////////////////////////////////////////////////////////////////////////////
167// GlassBrowserFrameView, views::NonClientFrameView implementation:
168
169gfx::Rect GlassBrowserFrameView::GetBoundsForClientView() const {
170  return client_view_bounds_;
171}
172
173bool GlassBrowserFrameView::AlwaysUseNativeFrame() const {
174  return frame_->AlwaysUseNativeFrame();
175}
176
177gfx::Rect GlassBrowserFrameView::GetWindowBoundsForClientBounds(
178    const gfx::Rect& client_bounds) const {
179  HWND hwnd = frame_->GetWindow()->GetNativeWindow();
180  if (!browser_view_->IsTabStripVisible() && hwnd) {
181    // If we don't have a tabstrip, we're either a popup or an app window, in
182    // which case we have a standard size non-client area and can just use
183    // AdjustWindowRectEx to obtain it. We check for a non-NULL window handle in
184    // case this gets called before the window is actually created.
185    RECT rect = client_bounds.ToRECT();
186    AdjustWindowRectEx(&rect, GetWindowLong(hwnd, GWL_STYLE), FALSE,
187                       GetWindowLong(hwnd, GWL_EXSTYLE));
188    return gfx::Rect(rect);
189  }
190
191  int top_height = NonClientTopBorderHeight(false, false);
192  int border_thickness = NonClientBorderThickness();
193  return gfx::Rect(std::max(0, client_bounds.x() - border_thickness),
194                   std::max(0, client_bounds.y() - top_height),
195                   client_bounds.width() + (2 * border_thickness),
196                   client_bounds.height() + top_height + border_thickness);
197}
198
199int GlassBrowserFrameView::NonClientHitTest(const gfx::Point& point) {
200  // If the browser isn't in normal mode, we haven't customized the frame, so
201  // Windows can figure this out.  If the point isn't within our bounds, then
202  // it's in the native portion of the frame, so again Windows can figure it
203  // out.
204  if (!browser_view_->IsBrowserTypeNormal() || !bounds().Contains(point))
205    return HTNOWHERE;
206
207  int frame_component =
208      frame_->GetWindow()->client_view()->NonClientHitTest(point);
209
210  // See if we're in the sysmenu region.  We still have to check the tabstrip
211  // first so that clicks in a tab don't get treated as sysmenu clicks.
212  int nonclient_border_thickness = NonClientBorderThickness();
213  if (gfx::Rect(nonclient_border_thickness, GetSystemMetrics(SM_CXSIZEFRAME),
214                GetSystemMetrics(SM_CXSMICON),
215                GetSystemMetrics(SM_CYSMICON)).Contains(point))
216    return (frame_component == HTCLIENT) ? HTCLIENT : HTSYSMENU;
217
218  if (frame_component != HTNOWHERE)
219    return frame_component;
220
221  // See if the point is within the profile menu button.
222  if (show_profile_button() && profile_button_->IsVisible() &&
223      profile_button_->GetMirroredBounds().Contains(point))
224    return HTCLIENT;
225
226  int frame_border_thickness = FrameBorderThickness();
227  int window_component = GetHTComponentForFrame(point, frame_border_thickness,
228      nonclient_border_thickness, frame_border_thickness,
229      kResizeAreaCornerSize - frame_border_thickness,
230      frame_->GetWindow()->window_delegate()->CanResize());
231  // Fall back to the caption if no other component matches.
232  return (window_component == HTNOWHERE) ? HTCAPTION : window_component;
233}
234
235///////////////////////////////////////////////////////////////////////////////
236// GlassBrowserFrameView, views::ViewMenuDelegate implementation:
237void GlassBrowserFrameView::RunMenu(views::View *source, const gfx::Point &pt) {
238  if (profile_menu_model_ == NULL)
239    profile_menu_model_.reset(new views::ProfileMenuModel);
240  gfx::Point menu_point(pt.x(),
241                        pt.y() + kMenuDisplayOffset);
242  profile_menu_model_->RunMenuAt(menu_point);
243}
244
245///////////////////////////////////////////////////////////////////////////////
246// GlassBrowserFrameView, views::View overrides:
247
248void GlassBrowserFrameView::OnPaint(gfx::Canvas* canvas) {
249  if (!browser_view_->IsTabStripVisible())
250    return;  // Nothing is visible, so don't bother to paint.
251
252  PaintToolbarBackground(canvas);
253  if (browser_view_->ShouldShowOffTheRecordAvatar())
254    PaintOTRAvatar(canvas);
255  if (!frame_->GetWindow()->IsMaximized())
256    PaintRestoredClientEdge(canvas);
257}
258
259void GlassBrowserFrameView::Layout() {
260  LayoutOTRAvatar();
261  LayoutClientView();
262  LayoutProfileTag();
263}
264
265bool GlassBrowserFrameView::HitTest(const gfx::Point& l) const {
266  // The ProfileMenuButton intrudes into the client area when the window is
267  // maximized.
268  return (frame_->GetWindow()->IsMaximized() && show_profile_button() &&
269          profile_button_->IsVisible() &&
270          profile_button_->GetMirroredBounds().Contains(l)) ||
271      !GetWindow()->client_view()->bounds().Contains(l);
272}
273
274///////////////////////////////////////////////////////////////////////////////
275// GlassBrowserFrameView, private:
276
277int GlassBrowserFrameView::FrameBorderThickness() const {
278  views::Window* window = frame_->GetWindow();
279  return (window->IsMaximized() || window->IsFullscreen()) ?
280      0 : GetSystemMetrics(SM_CXSIZEFRAME);
281}
282
283int GlassBrowserFrameView::NonClientBorderThickness() const {
284  views::Window* window = frame_->GetWindow();
285  if (window->IsMaximized() || window->IsFullscreen())
286    return 0;
287
288  return browser_view_->UseVerticalTabs() ?
289      kNonClientVerticalTabStripBorderThickness :
290      kNonClientBorderThickness;
291}
292
293int GlassBrowserFrameView::NonClientTopBorderHeight(
294    bool restored,
295    bool ignore_vertical_tabs) const {
296  if (!restored && frame_->GetWindow()->IsFullscreen())
297    return 0;
298  // We'd like to use FrameBorderThickness() here, but the maximized Aero glass
299  // frame has a 0 frame border around most edges and a CYSIZEFRAME-thick border
300  // at the top (see AeroGlassFrame::OnGetMinMaxInfo()).
301  if (browser_view_->IsTabStripVisible() && !ignore_vertical_tabs &&
302      browser_view_->UseVerticalTabs())
303    return GetSystemMetrics(SM_CYSIZEFRAME) + GetSystemMetrics(SM_CYCAPTION);
304  return GetSystemMetrics(SM_CYSIZEFRAME) +
305      ((!restored && browser_view_->IsMaximized()) ?
306      -kTabstripTopShadowThickness : kNonClientRestoredExtraThickness);
307}
308
309void GlassBrowserFrameView::PaintToolbarBackground(gfx::Canvas* canvas) {
310  ui::ThemeProvider* tp = GetThemeProvider();
311
312  gfx::Rect toolbar_bounds(browser_view_->GetToolbarBounds());
313  gfx::Point toolbar_origin(toolbar_bounds.origin());
314  View::ConvertPointToView(browser_view_, this, &toolbar_origin);
315  toolbar_bounds.set_origin(toolbar_origin);
316  int x = toolbar_bounds.x();
317  int w = toolbar_bounds.width();
318  int left_x = x - kContentEdgeShadowThickness;
319
320  SkBitmap* theme_toolbar = tp->GetBitmapNamed(IDR_THEME_TOOLBAR);
321  SkBitmap* toolbar_left = tp->GetBitmapNamed(IDR_CONTENT_TOP_LEFT_CORNER);
322  SkBitmap* toolbar_center = tp->GetBitmapNamed(IDR_CONTENT_TOP_CENTER);
323
324  if (browser_view_->UseVerticalTabs()) {
325    gfx::Point tabstrip_origin(browser_view_->tabstrip()->bounds().origin());
326    View::ConvertPointToView(browser_view_, this, &tabstrip_origin);
327    int y = tabstrip_origin.y();
328
329    // Tile the toolbar image starting at the frame edge on the left and where
330    // the horizontal tabstrip would be on the top.
331    canvas->TileImageInt(*theme_toolbar, x,
332                         y - GetHorizontalTabStripVerticalOffset(false), x, y,
333                         w, theme_toolbar->height());
334
335    // Draw left edge.
336    int dest_y = y - kNonClientBorderThickness;
337    canvas->DrawBitmapInt(*toolbar_left, 0, 0, kNonClientBorderThickness,
338                          kNonClientBorderThickness, left_x, dest_y,
339                          kNonClientBorderThickness, kNonClientBorderThickness,
340                          false);
341
342    // Draw center edge. We need to draw a while line above the toolbar for the
343    // image to overlay nicely.
344    int center_offset =
345        -kContentEdgeShadowThickness + kNonClientBorderThickness;
346    canvas->FillRectInt(SK_ColorWHITE, x + center_offset, y - 1,
347                        w - (2 * center_offset), 1);
348    canvas->TileImageInt(*toolbar_center, x + center_offset, dest_y,
349                         w - (2 * center_offset), toolbar_center->height());
350
351    // Right edge.
352    SkBitmap* toolbar_right = tp->GetBitmapNamed(IDR_CONTENT_TOP_RIGHT_CORNER);
353    canvas->DrawBitmapInt(*toolbar_right,
354        toolbar_right->width() - kNonClientBorderThickness, 0,
355        kNonClientBorderThickness, kNonClientBorderThickness,
356        x + w - center_offset, dest_y, kNonClientBorderThickness,
357        kNonClientBorderThickness, false);
358  } else {
359    // Tile the toolbar image starting at the frame edge on the left and where
360    // the tabstrip is on the top.
361    int y = toolbar_bounds.y();
362    int dest_y = y + (kFrameShadowThickness * 2);
363    canvas->TileImageInt(*theme_toolbar, x,
364                         dest_y - GetHorizontalTabStripVerticalOffset(false), x,
365                         dest_y, w, theme_toolbar->height());
366
367    // Draw rounded corners for the tab.
368    SkBitmap* toolbar_left_mask =
369        tp->GetBitmapNamed(IDR_CONTENT_TOP_LEFT_CORNER_MASK);
370    SkBitmap* toolbar_right_mask =
371        tp->GetBitmapNamed(IDR_CONTENT_TOP_RIGHT_CORNER_MASK);
372
373    // We mask out the corners by using the DestinationIn transfer mode,
374    // which keeps the RGB pixels from the destination and the alpha from
375    // the source.
376    SkPaint paint;
377    paint.setXfermodeMode(SkXfermode::kDstIn_Mode);
378
379    // Mask out the top left corner.
380    canvas->DrawBitmapInt(*toolbar_left_mask, left_x, y, paint);
381
382    // Mask out the top right corner.
383    int right_x =
384        x + w + kContentEdgeShadowThickness - toolbar_right_mask->width();
385    canvas->DrawBitmapInt(*toolbar_right_mask, right_x, y, paint);
386
387    // Draw left edge.
388    canvas->DrawBitmapInt(*toolbar_left, left_x, y);
389
390    // Draw center edge.
391    canvas->TileImageInt(*toolbar_center, left_x + toolbar_left->width(), y,
392        right_x - (left_x + toolbar_left->width()), toolbar_center->height());
393
394    // Right edge.
395    canvas->DrawBitmapInt(*tp->GetBitmapNamed(IDR_CONTENT_TOP_RIGHT_CORNER),
396                          right_x, y);
397  }
398
399  // Draw the content/toolbar separator.
400  canvas->FillRectInt(ResourceBundle::toolbar_separator_color,
401      x + kClientEdgeThickness, toolbar_bounds.bottom() - kClientEdgeThickness,
402      w - (2 * kClientEdgeThickness), kClientEdgeThickness);
403}
404
405void GlassBrowserFrameView::PaintOTRAvatar(gfx::Canvas* canvas) {
406  // In RTL mode, the avatar icon should be looking the opposite direction.
407  canvas->Save();
408  if (base::i18n::IsRTL()) {
409    canvas->TranslateInt(width(), 0);
410    canvas->ScaleInt(-1, 1);
411  }
412
413  SkBitmap otr_avatar_icon = browser_view_->GetOTRAvatarIcon();
414  int w = otr_avatar_bounds_.width();
415  int h = otr_avatar_bounds_.height();
416  canvas->DrawBitmapInt(otr_avatar_icon, 0,
417      // Bias the rounding to select a region that's lower rather than higher,
418      // as the shadows at the image top mean the apparent center is below the
419      // real center.
420      ((otr_avatar_icon.height() - otr_avatar_bounds_.height()) + 1) / 2, w, h,
421      otr_avatar_bounds_.x(), otr_avatar_bounds_.y(), w, h, false);
422
423  canvas->Restore();
424}
425
426void GlassBrowserFrameView::PaintRestoredClientEdge(gfx::Canvas* canvas) {
427  ui::ThemeProvider* tp = GetThemeProvider();
428  gfx::Rect client_area_bounds = CalculateClientAreaBounds(width(), height());
429
430  // The client edges start below the toolbar upper corner images regardless
431  // of how tall the toolbar itself is.
432  int client_area_top = browser_view_->UseVerticalTabs() ?
433      client_area_bounds.y() :
434      (frame_->GetWindow()->client_view()->y() +
435      browser_view_->GetToolbarBounds().y() +
436      tp->GetBitmapNamed(IDR_CONTENT_TOP_LEFT_CORNER)->height());
437  int client_area_bottom =
438      std::max(client_area_top, height() - NonClientBorderThickness());
439  int client_area_height = client_area_bottom - client_area_top;
440
441  // Draw the client edge images.
442  SkBitmap* right = tp->GetBitmapNamed(IDR_CONTENT_RIGHT_SIDE);
443  canvas->TileImageInt(*right, client_area_bounds.right(), client_area_top,
444                       right->width(), client_area_height);
445  canvas->DrawBitmapInt(
446      *tp->GetBitmapNamed(IDR_CONTENT_BOTTOM_RIGHT_CORNER),
447      client_area_bounds.right(), client_area_bottom);
448  SkBitmap* bottom = tp->GetBitmapNamed(IDR_CONTENT_BOTTOM_CENTER);
449  canvas->TileImageInt(*bottom, client_area_bounds.x(),
450      client_area_bottom, client_area_bounds.width(),
451      bottom->height());
452  SkBitmap* bottom_left =
453      tp->GetBitmapNamed(IDR_CONTENT_BOTTOM_LEFT_CORNER);
454  canvas->DrawBitmapInt(*bottom_left,
455      client_area_bounds.x() - bottom_left->width(), client_area_bottom);
456  SkBitmap* left = tp->GetBitmapNamed(IDR_CONTENT_LEFT_SIDE);
457  canvas->TileImageInt(*left, client_area_bounds.x() - left->width(),
458      client_area_top, left->width(), client_area_height);
459
460  // Draw the toolbar color so that the client edges show the right color even
461  // where not covered by the toolbar image.  NOTE: We do this after drawing the
462  // images because the images are meant to alpha-blend atop the frame whereas
463  // these rects are meant to be fully opaque, without anything overlaid.
464  SkColor toolbar_color = tp->GetColor(ThemeService::COLOR_TOOLBAR);
465  canvas->FillRectInt(toolbar_color,
466      client_area_bounds.x() - kClientEdgeThickness, client_area_top,
467      kClientEdgeThickness,
468      client_area_bottom + kClientEdgeThickness - client_area_top);
469  canvas->FillRectInt(toolbar_color, client_area_bounds.x(), client_area_bottom,
470                      client_area_bounds.width(), kClientEdgeThickness);
471  canvas->FillRectInt(toolbar_color, client_area_bounds.right(),
472      client_area_top, kClientEdgeThickness,
473      client_area_bottom + kClientEdgeThickness - client_area_top);
474}
475
476void GlassBrowserFrameView::LayoutOTRAvatar() {
477  int otr_x = NonClientBorderThickness() + kOTRSideSpacing;
478  // Move this avatar icon by the size of window controls to prevent it from
479  // being rendered over them in RTL languages. This code also needs to adjust
480  // the width of a tab strip to avoid decreasing this size twice. (See the
481  // comment in GetBoundsForTabStrip().)
482  if (base::i18n::IsRTL())
483    otr_x += width() - frame_->GetMinimizeButtonOffset();
484
485  SkBitmap otr_avatar_icon = browser_view_->GetOTRAvatarIcon();
486  int otr_bottom, otr_restored_y;
487  if (browser_view_->UseVerticalTabs()) {
488    otr_bottom = NonClientTopBorderHeight(false, false) - kOTRBottomSpacing;
489    otr_restored_y = kFrameShadowThickness;
490  } else {
491    otr_bottom = GetHorizontalTabStripVerticalOffset(false) +
492        browser_view_->GetTabStripHeight() - kOTRBottomSpacing;
493    otr_restored_y = otr_bottom - otr_avatar_icon.height();
494  }
495  int otr_y = frame_->GetWindow()->IsMaximized() ?
496      (NonClientTopBorderHeight(false, true) + kTabstripTopShadowThickness) :
497      otr_restored_y;
498  otr_avatar_bounds_.SetRect(otr_x, otr_y, otr_avatar_icon.width(),
499      browser_view_->ShouldShowOffTheRecordAvatar() ? (otr_bottom - otr_y) : 0);
500}
501
502void GlassBrowserFrameView::LayoutClientView() {
503  client_view_bounds_ = CalculateClientAreaBounds(width(), height());
504}
505
506void GlassBrowserFrameView::LayoutProfileTag() {
507  if (!show_profile_button())
508    return;
509
510  string16 profile_name = ASCIIToUTF16(browser_view_->browser()->profile()->
511      GetPrefs()->GetString(prefs::kGoogleServicesUsername));
512  if (!profile_name.empty()) {
513    profile_button_->SetText(profile_name);
514    profile_button_->ClearMaxTextSize();
515    profile_button_->SetVisible(true);
516    int x_tag =
517        // The x position of minimize button in the frame
518        frame_->GetMinimizeButtonOffset() -
519            // - the space between the minimize button and the profile button
520            views::ProfileMenuButton::kProfileTagHorizontalSpacing -
521            // - the width of the profile button
522            profile_button_->GetPreferredSize().width();
523    profile_button_->SetBounds(
524        x_tag,
525        kProfileButtonYPosition +
526            (frame_->GetWindow()->IsMaximized() ?
527                kProfileElementMaximizedYOffset : 0),
528        profile_button_->GetPreferredSize().width(),
529        profile_button_->GetPreferredSize().height());
530    profile_tag_->SetVisible(true);
531    profile_tag_->SetBounds(
532        x_tag,
533        kProfileTagYPosition +
534            (frame_->GetWindow()->IsMaximized() ?
535                kProfileElementMaximizedYOffset : 0),
536        profile_button_->GetPreferredSize().width(),
537        views::ProfileTagView::kProfileTagHeight);
538  } else {
539    profile_button_->SetVisible(false);
540    profile_tag_->SetVisible(false);
541  }
542}
543
544gfx::Rect GlassBrowserFrameView::CalculateClientAreaBounds(int width,
545                                                           int height) const {
546  if (!browser_view_->IsTabStripVisible())
547    return gfx::Rect(0, 0, this->width(), this->height());
548
549  int top_height = NonClientTopBorderHeight(false, false);
550  int border_thickness = NonClientBorderThickness();
551  return gfx::Rect(border_thickness, top_height,
552                   std::max(0, width - (2 * border_thickness)),
553                   std::max(0, height - top_height - border_thickness));
554}
555
556void GlassBrowserFrameView::StartThrobber() {
557  if (!throbber_running_) {
558    throbber_running_ = true;
559    throbber_frame_ = 0;
560    InitThrobberIcons();
561    SendMessage(frame_->GetWindow()->GetNativeWindow(), WM_SETICON,
562                static_cast<WPARAM>(ICON_SMALL),
563                reinterpret_cast<LPARAM>(throbber_icons_[throbber_frame_]));
564  }
565}
566
567void GlassBrowserFrameView::StopThrobber() {
568  if (throbber_running_) {
569    throbber_running_ = false;
570
571    HICON frame_icon = NULL;
572
573    // Check if hosted BrowserView has a window icon to use.
574    if (browser_view_->ShouldShowWindowIcon()) {
575      SkBitmap icon = browser_view_->GetWindowIcon();
576      if (!icon.isNull())
577        frame_icon = IconUtil::CreateHICONFromSkBitmap(icon);
578    }
579
580    // Fallback to class icon.
581    if (!frame_icon) {
582      frame_icon = reinterpret_cast<HICON>(GetClassLongPtr(
583          frame_->GetWindow()->GetNativeWindow(), GCLP_HICONSM));
584    }
585
586    // This will reset the small icon which we set in the throbber code.
587    // WM_SETICON with NULL icon restores the icon for title bar but not
588    // for taskbar. See http://crbug.com/29996
589    SendMessage(frame_->GetWindow()->GetNativeWindow(), WM_SETICON,
590                static_cast<WPARAM>(ICON_SMALL),
591                reinterpret_cast<LPARAM>(frame_icon));
592  }
593}
594
595void GlassBrowserFrameView::DisplayNextThrobberFrame() {
596  throbber_frame_ = (throbber_frame_ + 1) % kThrobberIconCount;
597  SendMessage(frame_->GetWindow()->GetNativeWindow(), WM_SETICON,
598              static_cast<WPARAM>(ICON_SMALL),
599              reinterpret_cast<LPARAM>(throbber_icons_[throbber_frame_]));
600}
601
602void GlassBrowserFrameView::Observe(NotificationType type,
603                                    const NotificationSource& source,
604                                    const NotificationDetails& details) {
605  DCHECK_EQ(NotificationType::PREF_CHANGED, type.value);
606  std::string* name = Details<std::string>(details).ptr();
607  if (prefs::kGoogleServicesUsername == *name)
608    LayoutProfileTag();
609}
610
611void GlassBrowserFrameView::RegisterLoginNotifications() {
612  PrefService* pref_service = browser_view_->browser()->profile()->GetPrefs();
613  DCHECK(pref_service);
614  username_pref_.Init(prefs::kGoogleServicesUsername, pref_service, this);
615}
616
617// static
618void GlassBrowserFrameView::InitThrobberIcons() {
619  static bool initialized = false;
620  if (!initialized) {
621    ResourceBundle &rb = ResourceBundle::GetSharedInstance();
622    for (int i = 0; i < kThrobberIconCount; ++i) {
623      throbber_icons_[i] = rb.LoadThemeIcon(IDI_THROBBER_01 + i);
624      DCHECK(throbber_icons_[i]);
625    }
626    initialized = true;
627  }
628}
629