1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/ui/views/frame/glass_browser_frame_view.h"
6
7#include "base/prefs/pref_service.h"
8#include "base/strings/utf_string_conversions.h"
9#include "chrome/app/chrome_command_ids.h"
10#include "chrome/app/chrome_dll_resource.h"
11#include "chrome/browser/chrome_notification_types.h"
12#include "chrome/browser/profiles/profile.h"
13#include "chrome/browser/signin/signin_header_helper.h"
14#include "chrome/browser/themes/theme_properties.h"
15#include "chrome/browser/ui/views/frame/browser_view.h"
16#include "chrome/browser/ui/views/profiles/avatar_menu_button.h"
17#include "chrome/browser/ui/views/profiles/new_avatar_button.h"
18#include "chrome/browser/ui/views/tabs/tab.h"
19#include "chrome/browser/ui/views/tabs/tab_strip.h"
20#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
21#include "chrome/common/pref_names.h"
22#include "components/signin/core/common/profile_management_switches.h"
23#include "content/public/browser/notification_service.h"
24#include "grit/theme_resources.h"
25#include "ui/base/resource/resource_bundle_win.h"
26#include "ui/base/theme_provider.h"
27#include "ui/gfx/canvas.h"
28#include "ui/gfx/icon_util.h"
29#include "ui/gfx/image/image.h"
30#include "ui/gfx/win/dpi.h"
31#include "ui/resources/grit/ui_resources.h"
32#include "ui/views/controls/label.h"
33#include "ui/views/layout/layout_constants.h"
34#include "ui/views/win/hwnd_util.h"
35#include "ui/views/window/client_view.h"
36
37HICON GlassBrowserFrameView::throbber_icons_[
38    GlassBrowserFrameView::kThrobberIconCount];
39
40namespace {
41// There are 3 px of client edge drawn inside the outer frame borders.
42const int kNonClientBorderThickness = 3;
43// Besides the frame border, there's another 9 px of empty space atop the
44// window in restored mode, to use to drag the window around.
45const int kNonClientRestoredExtraThickness = 9;
46// In the window corners, the resize areas don't actually expand bigger, but the
47// 16 px at the end of the top and bottom edges triggers diagonal resizing.
48const int kResizeAreaCornerSize = 16;
49// The avatar ends 2 px above the bottom of the tabstrip (which, given the
50// way the tabstrip draws its bottom edge, will appear like a 1 px gap to the
51// user).
52const int kAvatarBottomSpacing = 2;
53// Space between the frame border and the left edge of the avatar.
54const int kAvatarLeftSpacing = 2;
55// Space between the right edge of the avatar and the tabstrip.
56const int kAvatarRightSpacing = -2;
57// How far the new avatar button is from the left of the minimize button.
58const int kNewAvatarButtonOffset = 5;
59// The content left/right images have a shadow built into them.
60const int kContentEdgeShadowThickness = 2;
61// The top 3 px of the tabstrip is shadow; in maximized mode we push this off
62// the top of the screen so the tabs appear flush against the screen edge.
63const int kTabstripTopShadowThickness = 3;
64// In restored mode, the New Tab button isn't at the same height as the caption
65// buttons, but the space will look cluttered if it actually slides under them,
66// so we stop it when the gap between the two is down to 5 px.
67const int kNewTabCaptionRestoredSpacing = 5;
68// In maximized mode, where the New Tab button and the caption buttons are at
69// similar vertical coordinates, we need to reserve a larger, 16 px gap to avoid
70// looking too cluttered.
71const int kNewTabCaptionMaximizedSpacing = 16;
72// How far to indent the tabstrip from the left side of the screen when there
73// is no avatar icon.
74const int kTabStripIndent = -6;
75
76}  // namespace
77
78///////////////////////////////////////////////////////////////////////////////
79// GlassBrowserFrameView, public:
80
81GlassBrowserFrameView::GlassBrowserFrameView(BrowserFrame* frame,
82                                             BrowserView* browser_view)
83    : BrowserNonClientFrameView(frame, browser_view),
84      throbber_running_(false),
85      throbber_frame_(0) {
86  if (browser_view->ShouldShowWindowIcon())
87    InitThrobberIcons();
88
89  if (browser_view->IsRegularOrGuestSession() && switches::IsNewAvatarMenu())
90    UpdateNewStyleAvatarInfo(this, NewAvatarButton::NATIVE_BUTTON);
91  else
92    UpdateAvatarInfo();
93
94  if (!browser_view->IsOffTheRecord()) {
95    registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
96                   content::NotificationService::AllSources());
97  }
98}
99
100GlassBrowserFrameView::~GlassBrowserFrameView() {
101}
102
103///////////////////////////////////////////////////////////////////////////////
104// GlassBrowserFrameView, BrowserNonClientFrameView implementation:
105
106gfx::Rect GlassBrowserFrameView::GetBoundsForTabStrip(
107    views::View* tabstrip) const {
108  int minimize_button_offset =
109      std::min(frame()->GetMinimizeButtonOffset(), width());
110
111  // The new avatar button is optionally displayed to the left of the
112  // minimize button.
113  if (new_avatar_button()) {
114    DCHECK(switches::IsNewAvatarMenu());
115    minimize_button_offset -=
116        new_avatar_button()->width() + kNewAvatarButtonOffset;
117
118    // In non-maximized mode, allow the new tab button to completely slide under
119    // the avatar button.
120    if (!frame()->IsMaximized() && !base::i18n::IsRTL()) {
121      minimize_button_offset +=
122          TabStrip::kNewTabButtonAssetWidth + kNewTabCaptionRestoredSpacing;
123    }
124  }
125
126  int tabstrip_x = browser_view()->ShouldShowAvatar() ?
127      (avatar_bounds_.right() + kAvatarRightSpacing) :
128      NonClientBorderThickness() + kTabStripIndent;
129  // In RTL languages, we have moved an avatar icon left by the size of window
130  // controls to prevent it from being rendered over them. So, we use its x
131  // position to move this tab strip left when maximized. Also, we can render
132  // a tab strip until the left end of this window without considering the size
133  // of window controls in RTL languages.
134  if (base::i18n::IsRTL()) {
135    if (!browser_view()->ShouldShowAvatar() && frame()->IsMaximized()) {
136      tabstrip_x += avatar_bounds_.x();
137    } else if (browser_view()->IsRegularOrGuestSession() &&
138               switches::IsNewAvatarMenu()) {
139      tabstrip_x = width() - minimize_button_offset;
140    }
141
142    minimize_button_offset = width();
143  }
144  int tabstrip_width = minimize_button_offset - tabstrip_x -
145      (frame()->IsMaximized() ?
146          kNewTabCaptionMaximizedSpacing : kNewTabCaptionRestoredSpacing);
147  return gfx::Rect(tabstrip_x, NonClientTopBorderHeight(),
148                   std::max(0, tabstrip_width),
149                   tabstrip->GetPreferredSize().height());
150}
151
152int GlassBrowserFrameView::GetTopInset() const {
153  return GetClientAreaInsets().top();
154}
155
156int GlassBrowserFrameView::GetThemeBackgroundXInset() const {
157  return 0;
158}
159
160void GlassBrowserFrameView::UpdateThrobber(bool running) {
161  if (throbber_running_) {
162    if (running) {
163      DisplayNextThrobberFrame();
164    } else {
165      StopThrobber();
166    }
167  } else if (running) {
168    StartThrobber();
169  }
170}
171
172gfx::Size GlassBrowserFrameView::GetMinimumSize() const {
173  gfx::Size min_size(browser_view()->GetMinimumSize());
174
175  // Account for the client area insets.
176  gfx::Insets insets = GetClientAreaInsets();
177  min_size.Enlarge(insets.width(), insets.height());
178  // Client area insets do not include the shadow thickness.
179  min_size.Enlarge(2 * kContentEdgeShadowThickness, 0);
180
181  // Ensure that the minimum width is enough to hold a tab strip with minimum
182  // width at its usual insets.
183  if (browser_view()->IsTabStripVisible()) {
184    TabStrip* tabstrip = browser_view()->tabstrip();
185    int min_tabstrip_width = tabstrip->GetMinimumSize().width();
186    int min_tabstrip_area_width =
187        width() - GetBoundsForTabStrip(tabstrip).width() + min_tabstrip_width;
188    min_size.set_width(std::max(min_tabstrip_area_width, min_size.width()));
189  }
190
191  return min_size;
192}
193
194///////////////////////////////////////////////////////////////////////////////
195// GlassBrowserFrameView, views::NonClientFrameView implementation:
196
197gfx::Rect GlassBrowserFrameView::GetBoundsForClientView() const {
198  return client_view_bounds_;
199}
200
201gfx::Rect GlassBrowserFrameView::GetWindowBoundsForClientBounds(
202    const gfx::Rect& client_bounds) const {
203  HWND hwnd = views::HWNDForWidget(frame());
204  if (!browser_view()->IsTabStripVisible() && hwnd) {
205    // If we don't have a tabstrip, we're either a popup or an app window, in
206    // which case we have a standard size non-client area and can just use
207    // AdjustWindowRectEx to obtain it. We check for a non-NULL window handle in
208    // case this gets called before the window is actually created.
209    RECT rect = client_bounds.ToRECT();
210    AdjustWindowRectEx(&rect, GetWindowLong(hwnd, GWL_STYLE), FALSE,
211                       GetWindowLong(hwnd, GWL_EXSTYLE));
212    return gfx::Rect(rect);
213  }
214
215  gfx::Insets insets = GetClientAreaInsets();
216  return gfx::Rect(std::max(0, client_bounds.x() - insets.left()),
217                   std::max(0, client_bounds.y() - insets.top()),
218                   client_bounds.width() + insets.width(),
219                   client_bounds.height() + insets.height());
220}
221
222int GlassBrowserFrameView::NonClientHitTest(const gfx::Point& point) {
223  // If the browser isn't in normal mode, we haven't customized the frame, so
224  // Windows can figure this out.  If the point isn't within our bounds, then
225  // it's in the native portion of the frame, so again Windows can figure it
226  // out.
227  if (!browser_view()->IsBrowserTypeNormal() || !bounds().Contains(point))
228    return HTNOWHERE;
229
230  // See if the point is within the avatar menu button or within the avatar
231  // label.
232  if (avatar_button() && avatar_button()->GetMirroredBounds().Contains(point))
233    return HTCLIENT;
234
235  if (new_avatar_button() &&
236     new_avatar_button()->GetMirroredBounds().Contains(point))
237   return HTCLIENT;
238
239  int frame_component = frame()->client_view()->NonClientHitTest(point);
240
241  // See if we're in the sysmenu region.  We still have to check the tabstrip
242  // first so that clicks in a tab don't get treated as sysmenu clicks.
243  int nonclient_border_thickness = NonClientBorderThickness();
244  if (gfx::Rect(nonclient_border_thickness,
245                gfx::win::GetSystemMetricsInDIP(SM_CXSIZEFRAME),
246                gfx::win::GetSystemMetricsInDIP(SM_CXSMICON),
247                gfx::win::GetSystemMetricsInDIP(SM_CYSMICON)).Contains(point))
248    return (frame_component == HTCLIENT) ? HTCLIENT : HTSYSMENU;
249
250  if (frame_component != HTNOWHERE)
251    return frame_component;
252
253  int frame_border_thickness = FrameBorderThickness();
254  int window_component = GetHTComponentForFrame(point, frame_border_thickness,
255      nonclient_border_thickness, frame_border_thickness,
256      kResizeAreaCornerSize - frame_border_thickness,
257      frame()->widget_delegate()->CanResize());
258  // Fall back to the caption if no other component matches.
259  return (window_component == HTNOWHERE) ? HTCAPTION : window_component;
260}
261
262///////////////////////////////////////////////////////////////////////////////
263// GlassBrowserFrameView, views::View overrides:
264
265void GlassBrowserFrameView::OnPaint(gfx::Canvas* canvas) {
266  if (browser_view()->IsToolbarVisible() &&
267      browser_view()->toolbar()->ShouldPaintBackground())
268    PaintToolbarBackground(canvas);
269  if (!frame()->IsMaximized())
270    PaintRestoredClientEdge(canvas);
271}
272
273void GlassBrowserFrameView::Layout() {
274  if (browser_view()->IsRegularOrGuestSession() && switches::IsNewAvatarMenu())
275    LayoutNewStyleAvatar();
276  else
277    LayoutAvatar();
278
279  LayoutClientView();
280}
281
282///////////////////////////////////////////////////////////////////////////////
283// GlassBrowserFrameView, views::ButtonListener overrides:
284void GlassBrowserFrameView::ButtonPressed(views::Button* sender,
285                                          const ui::Event& event) {
286  if (sender == new_avatar_button()) {
287    browser_view()->ShowAvatarBubbleFromAvatarButton(
288        BrowserWindow::AVATAR_BUBBLE_MODE_DEFAULT,
289        signin::ManageAccountsParams());
290  }
291}
292
293///////////////////////////////////////////////////////////////////////////////
294// GlassBrowserFrameView, private:
295
296// views::NonClientFrameView:
297bool GlassBrowserFrameView::DoesIntersectRect(const views::View* target,
298                                              const gfx::Rect& rect) const {
299  CHECK_EQ(target, this);
300  bool hit_avatar_button = avatar_button() &&
301      avatar_button()->GetMirroredBounds().Intersects(rect);
302  bool hit_new_avatar_button = new_avatar_button() &&
303      new_avatar_button()->GetMirroredBounds().Intersects(rect);
304  return hit_avatar_button || hit_new_avatar_button ||
305         !frame()->client_view()->bounds().Intersects(rect);
306}
307
308int GlassBrowserFrameView::FrameBorderThickness() const {
309  return (frame()->IsMaximized() || frame()->IsFullscreen()) ?
310      0 : gfx::win::GetSystemMetricsInDIP(SM_CXSIZEFRAME);
311}
312
313int GlassBrowserFrameView::NonClientBorderThickness() const {
314  if (frame()->IsMaximized() || frame()->IsFullscreen())
315    return 0;
316
317  return kNonClientBorderThickness;
318}
319
320int GlassBrowserFrameView::NonClientTopBorderHeight() const {
321  if (frame()->IsFullscreen())
322    return 0;
323
324  // We'd like to use FrameBorderThickness() here, but the maximized Aero glass
325  // frame has a 0 frame border around most edges and a CYSIZEFRAME-thick border
326  // at the top (see AeroGlassFrame::OnGetMinMaxInfo()).
327  return gfx::win::GetSystemMetricsInDIP(SM_CYSIZEFRAME) +
328      (!frame()->ShouldLeaveOffsetNearTopBorder() ?
329      -kTabstripTopShadowThickness : kNonClientRestoredExtraThickness);
330}
331
332void GlassBrowserFrameView::PaintToolbarBackground(gfx::Canvas* canvas) {
333  ui::ThemeProvider* tp = GetThemeProvider();
334
335  gfx::Rect toolbar_bounds(browser_view()->GetToolbarBounds());
336  gfx::Point toolbar_origin(toolbar_bounds.origin());
337  View::ConvertPointToTarget(browser_view(), this, &toolbar_origin);
338  toolbar_bounds.set_origin(toolbar_origin);
339  int x = toolbar_bounds.x();
340  int w = toolbar_bounds.width();
341  int left_x = x - kContentEdgeShadowThickness;
342
343  gfx::ImageSkia* theme_toolbar = tp->GetImageSkiaNamed(IDR_THEME_TOOLBAR);
344  gfx::ImageSkia* toolbar_left = tp->GetImageSkiaNamed(
345      IDR_CONTENT_TOP_LEFT_CORNER);
346  gfx::ImageSkia* toolbar_center = tp->GetImageSkiaNamed(
347      IDR_CONTENT_TOP_CENTER);
348
349  // Tile the toolbar image starting at the frame edge on the left and where
350  // the tabstrip is on the top.
351  int y = toolbar_bounds.y();
352  int dest_y = browser_view()->IsTabStripVisible()
353                   ? y + (kFrameShadowThickness * 2)
354                   : y;
355  canvas->TileImageInt(*theme_toolbar,
356                       x + GetThemeBackgroundXInset(),
357                       dest_y - GetTopInset(), x,
358                       dest_y, w, theme_toolbar->height());
359
360  if (browser_view()->IsTabStripVisible()) {
361    // Draw rounded corners for the tab.
362    gfx::ImageSkia* toolbar_left_mask =
363        tp->GetImageSkiaNamed(IDR_CONTENT_TOP_LEFT_CORNER_MASK);
364    gfx::ImageSkia* toolbar_right_mask =
365        tp->GetImageSkiaNamed(IDR_CONTENT_TOP_RIGHT_CORNER_MASK);
366
367    // We mask out the corners by using the DestinationIn transfer mode,
368    // which keeps the RGB pixels from the destination and the alpha from
369    // the source.
370    SkPaint paint;
371    paint.setXfermodeMode(SkXfermode::kDstIn_Mode);
372
373    // Mask out the top left corner.
374    canvas->DrawImageInt(*toolbar_left_mask, left_x, y, paint);
375
376    // Mask out the top right corner.
377    int right_x =
378        x + w + kContentEdgeShadowThickness - toolbar_right_mask->width();
379    canvas->DrawImageInt(*toolbar_right_mask, right_x, y, paint);
380
381    // Draw left edge.
382    canvas->DrawImageInt(*toolbar_left, left_x, y);
383
384    // Draw center edge.
385    canvas->TileImageInt(*toolbar_center, left_x + toolbar_left->width(), y,
386        right_x - (left_x + toolbar_left->width()), toolbar_center->height());
387
388    // Right edge.
389    canvas->DrawImageInt(*tp->GetImageSkiaNamed(IDR_CONTENT_TOP_RIGHT_CORNER),
390                         right_x, y);
391  }
392
393  // Draw the content/toolbar separator.
394  canvas->FillRect(
395      gfx::Rect(x + kClientEdgeThickness,
396                toolbar_bounds.bottom() - kClientEdgeThickness,
397                w - (2 * kClientEdgeThickness),
398                kClientEdgeThickness),
399      ThemeProperties::GetDefaultColor(
400          ThemeProperties::COLOR_TOOLBAR_SEPARATOR));
401}
402
403void GlassBrowserFrameView::PaintRestoredClientEdge(gfx::Canvas* canvas) {
404  ui::ThemeProvider* tp = GetThemeProvider();
405  gfx::Rect client_area_bounds = CalculateClientAreaBounds(width(), height());
406
407  // The client edges start below the toolbar upper corner images regardless
408  // of how tall the toolbar itself is.
409  int client_area_top = frame()->client_view()->y() +
410      browser_view()->GetToolbarBounds().y() +
411      tp->GetImageSkiaNamed(IDR_CONTENT_TOP_LEFT_CORNER)->height();
412  int client_area_bottom =
413      std::max(client_area_top, height() - NonClientBorderThickness());
414  int client_area_height = client_area_bottom - client_area_top;
415
416  // Draw the client edge images.
417  gfx::ImageSkia* right = tp->GetImageSkiaNamed(IDR_CONTENT_RIGHT_SIDE);
418  canvas->TileImageInt(*right, client_area_bounds.right(), client_area_top,
419                       right->width(), client_area_height);
420  canvas->DrawImageInt(
421      *tp->GetImageSkiaNamed(IDR_CONTENT_BOTTOM_RIGHT_CORNER),
422      client_area_bounds.right(), client_area_bottom);
423  gfx::ImageSkia* bottom = tp->GetImageSkiaNamed(IDR_CONTENT_BOTTOM_CENTER);
424  canvas->TileImageInt(*bottom, client_area_bounds.x(),
425      client_area_bottom, client_area_bounds.width(),
426      bottom->height());
427  gfx::ImageSkia* bottom_left =
428      tp->GetImageSkiaNamed(IDR_CONTENT_BOTTOM_LEFT_CORNER);
429  canvas->DrawImageInt(*bottom_left,
430      client_area_bounds.x() - bottom_left->width(), client_area_bottom);
431  gfx::ImageSkia* left = tp->GetImageSkiaNamed(IDR_CONTENT_LEFT_SIDE);
432  canvas->TileImageInt(*left, client_area_bounds.x() - left->width(),
433      client_area_top, left->width(), client_area_height);
434
435  // Draw the toolbar color so that the client edges show the right color even
436  // where not covered by the toolbar image.  NOTE: We do this after drawing the
437  // images because the images are meant to alpha-blend atop the frame whereas
438  // these rects are meant to be fully opaque, without anything overlaid.
439  SkColor toolbar_color = tp->GetColor(ThemeProperties::COLOR_TOOLBAR);
440  canvas->FillRect(gfx::Rect(client_area_bounds.x() - kClientEdgeThickness,
441      client_area_top, kClientEdgeThickness,
442      client_area_bottom + kClientEdgeThickness - client_area_top),
443      toolbar_color);
444  canvas->FillRect(gfx::Rect(client_area_bounds.x(), client_area_bottom,
445                             client_area_bounds.width(), kClientEdgeThickness),
446                   toolbar_color);
447  canvas->FillRect(gfx::Rect(client_area_bounds.right(), client_area_top,
448       kClientEdgeThickness,
449       client_area_bottom + kClientEdgeThickness - client_area_top),
450       toolbar_color);
451}
452
453void GlassBrowserFrameView::LayoutNewStyleAvatar() {
454  DCHECK(switches::IsNewAvatarMenu());
455  if (!new_avatar_button())
456    return;
457
458  gfx::Size label_size = new_avatar_button()->GetPreferredSize();
459
460  int button_x = frame()->GetMinimizeButtonOffset() -
461      kNewAvatarButtonOffset - label_size.width();
462  if (base::i18n::IsRTL())
463    button_x = width() - frame()->GetMinimizeButtonOffset() +
464        kNewAvatarButtonOffset;
465
466  // We need to offset the button correctly in maximized mode, so that the
467  // custom glass style aligns with the native control glass style. The
468  // glass shadow is off by 1px, which was determined by visual inspection.
469  int button_y = !frame()->IsMaximized() ? 1 :
470      NonClientTopBorderHeight() + kTabstripTopShadowThickness - 1;
471
472  new_avatar_button()->SetBounds(
473      button_x,
474      button_y,
475      label_size.width(),
476      gfx::win::GetSystemMetricsInDIP(SM_CYMENUSIZE) + 1);
477}
478
479void GlassBrowserFrameView::LayoutAvatar() {
480  // Even though the avatar is used for both incognito and profiles we always
481  // use the incognito icon to layout the avatar button. The profile icon
482  // can be customized so we can't depend on its size to perform layout.
483  gfx::ImageSkia incognito_icon = browser_view()->GetOTRAvatarIcon();
484
485  int avatar_x = NonClientBorderThickness() + kAvatarLeftSpacing;
486  // Move this avatar icon by the size of window controls to prevent it from
487  // being rendered over them in RTL languages. This code also needs to adjust
488  // the width of a tab strip to avoid decreasing this size twice. (See the
489  // comment in GetBoundsForTabStrip().)
490  if (base::i18n::IsRTL())
491    avatar_x += width() - frame()->GetMinimizeButtonOffset();
492
493  int avatar_bottom = GetTopInset() +
494      browser_view()->GetTabStripHeight() - kAvatarBottomSpacing;
495  int avatar_restored_y = avatar_bottom - incognito_icon.height();
496  int avatar_y = frame()->IsMaximized() ?
497      (NonClientTopBorderHeight() + kTabstripTopShadowThickness) :
498      avatar_restored_y;
499  avatar_bounds_.SetRect(avatar_x, avatar_y, incognito_icon.width(),
500      browser_view()->ShouldShowAvatar() ? (avatar_bottom - avatar_y) : 0);
501  if (avatar_button())
502    avatar_button()->SetBoundsRect(avatar_bounds_);
503}
504
505void GlassBrowserFrameView::LayoutClientView() {
506  client_view_bounds_ = CalculateClientAreaBounds(width(), height());
507}
508
509gfx::Insets GlassBrowserFrameView::GetClientAreaInsets() const {
510  if (!browser_view()->IsTabStripVisible())
511    return gfx::Insets();
512
513  const int top_height = NonClientTopBorderHeight();
514  const int border_thickness = NonClientBorderThickness();
515  return gfx::Insets(top_height,
516                     border_thickness,
517                     border_thickness,
518                     border_thickness);
519}
520
521gfx::Rect GlassBrowserFrameView::CalculateClientAreaBounds(int width,
522                                                           int height) const {
523  gfx::Rect bounds(0, 0, width, height);
524  bounds.Inset(GetClientAreaInsets());
525  return bounds;
526}
527
528void GlassBrowserFrameView::StartThrobber() {
529  if (!throbber_running_) {
530    throbber_running_ = true;
531    throbber_frame_ = 0;
532    InitThrobberIcons();
533    SendMessage(views::HWNDForWidget(frame()), WM_SETICON,
534                static_cast<WPARAM>(ICON_SMALL),
535                reinterpret_cast<LPARAM>(throbber_icons_[throbber_frame_]));
536  }
537}
538
539void GlassBrowserFrameView::StopThrobber() {
540  if (throbber_running_) {
541    throbber_running_ = false;
542
543    HICON frame_icon = NULL;
544
545    // Check if hosted BrowserView has a window icon to use.
546    if (browser_view()->ShouldShowWindowIcon()) {
547      gfx::ImageSkia icon = browser_view()->GetWindowIcon();
548      if (!icon.isNull())
549        frame_icon = IconUtil::CreateHICONFromSkBitmap(*icon.bitmap());
550    }
551
552    // Fallback to class icon.
553    if (!frame_icon) {
554      frame_icon = reinterpret_cast<HICON>(GetClassLongPtr(
555          views::HWNDForWidget(frame()), GCLP_HICONSM));
556    }
557
558    // This will reset the small icon which we set in the throbber code.
559    // WM_SETICON with NULL icon restores the icon for title bar but not
560    // for taskbar. See http://crbug.com/29996
561    SendMessage(views::HWNDForWidget(frame()), WM_SETICON,
562                static_cast<WPARAM>(ICON_SMALL),
563                reinterpret_cast<LPARAM>(frame_icon));
564  }
565}
566
567void GlassBrowserFrameView::DisplayNextThrobberFrame() {
568  throbber_frame_ = (throbber_frame_ + 1) % kThrobberIconCount;
569  SendMessage(views::HWNDForWidget(frame()), WM_SETICON,
570              static_cast<WPARAM>(ICON_SMALL),
571              reinterpret_cast<LPARAM>(throbber_icons_[throbber_frame_]));
572}
573
574void GlassBrowserFrameView::Observe(
575    int type,
576    const content::NotificationSource& source,
577    const content::NotificationDetails& details) {
578  switch (type) {
579    case chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED:
580      if (browser_view()->IsRegularOrGuestSession() &&
581          switches::IsNewAvatarMenu()) {
582        UpdateNewStyleAvatarInfo(this, NewAvatarButton::NATIVE_BUTTON);
583      } else {
584        UpdateAvatarInfo();
585      }
586      break;
587    default:
588      NOTREACHED() << "Got a notification we didn't register for!";
589      break;
590  }
591}
592
593// static
594void GlassBrowserFrameView::InitThrobberIcons() {
595  static bool initialized = false;
596  if (!initialized) {
597    for (int i = 0; i < kThrobberIconCount; ++i) {
598      throbber_icons_[i] =
599          ui::LoadThemeIconFromResourcesDataDLL(IDI_THROBBER_01 + i);
600      DCHECK(throbber_icons_[i]);
601    }
602    initialized = true;
603  }
604}
605