browser_non_client_frame_view.cc revision 3551c9c881056c480085172ff9840cab31610854
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/ui/views/frame/browser_non_client_frame_view.h"
6
7#include "chrome/browser/browser_process.h"
8#include "chrome/browser/profiles/avatar_menu_model.h"
9#include "chrome/browser/profiles/profile.h"
10#include "chrome/browser/profiles/profile_info_cache.h"
11#include "chrome/browser/profiles/profile_manager.h"
12#include "chrome/browser/ui/views/avatar_label.h"
13#include "chrome/browser/ui/views/avatar_menu_button.h"
14#include "chrome/browser/ui/views/frame/browser_view.h"
15#include "chrome/browser/ui/views/frame/taskbar_decorator.h"
16#include "grit/generated_resources.h"
17#include "grit/theme_resources.h"
18#include "third_party/skia/include/core/SkColor.h"
19#include "ui/base/l10n/l10n_util.h"
20#include "ui/base/resource/resource_bundle.h"
21#include "ui/base/theme_provider.h"
22#include "ui/gfx/image/image.h"
23#include "ui/views/background.h"
24
25BrowserNonClientFrameView::BrowserNonClientFrameView(BrowserFrame* frame,
26                                                     BrowserView* browser_view)
27    : frame_(frame),
28      browser_view_(browser_view),
29      avatar_button_(NULL),
30      avatar_label_(NULL) {
31}
32
33BrowserNonClientFrameView::~BrowserNonClientFrameView() {
34}
35
36void BrowserNonClientFrameView::VisibilityChanged(views::View* starting_from,
37                                                  bool is_visible) {
38  if (!is_visible)
39    return;
40  // The first time UpdateAvatarInfo() is called the window is not visible so
41  // DrawTaskBarDecoration() has no effect. Therefore we need to call it again
42  // once the window is visible.
43  UpdateAvatarInfo();
44}
45
46void BrowserNonClientFrameView::OnThemeChanged() {
47  if (avatar_label_)
48    avatar_label_->UpdateLabelStyle();
49}
50
51void BrowserNonClientFrameView::UpdateAvatarInfo() {
52  if (browser_view_->ShouldShowAvatar()) {
53    if (!avatar_button_) {
54      Profile* profile = browser_view_->browser()->profile();
55      if (profile->IsManaged() && !avatar_label_) {
56        avatar_label_ = new AvatarLabel(browser_view_);
57        AddChildView(avatar_label_);
58      }
59      avatar_button_ = new AvatarMenuButton(
60          browser_view_->browser(),
61          browser_view_->IsOffTheRecord() && !browser_view_->IsGuestSession());
62      AddChildView(avatar_button_);
63      frame_->GetRootView()->Layout();
64    }
65  } else if (avatar_button_) {
66    // The avatar label can just be there if there is also an avatar button.
67    if (avatar_label_) {
68      RemoveChildView(avatar_label_);
69      delete avatar_label_;
70      avatar_label_ = NULL;
71    }
72    RemoveChildView(avatar_button_);
73    delete avatar_button_;
74    avatar_button_ = NULL;
75    frame_->GetRootView()->Layout();
76  }
77
78  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
79  gfx::Image avatar;
80  string16 text;
81  bool is_gaia_picture = false;
82  if (browser_view_->IsGuestSession()) {
83    avatar = rb.GetImageNamed(browser_view_->GetGuestIconResourceID());
84  } else if (browser_view_->IsOffTheRecord()) {
85    avatar = rb.GetImageNamed(browser_view_->GetOTRIconResourceID());
86  } else if (AvatarMenuModel::ShouldShowAvatarMenu()) {
87    ProfileInfoCache& cache =
88        g_browser_process->profile_manager()->GetProfileInfoCache();
89    Profile* profile = browser_view_->browser()->profile();
90    size_t index = cache.GetIndexOfProfileWithPath(profile->GetPath());
91    if (index == std::string::npos)
92      return;
93    is_gaia_picture =
94        cache.IsUsingGAIAPictureOfProfileAtIndex(index) &&
95        cache.GetGAIAPictureOfProfileAtIndex(index);
96    avatar = cache.GetAvatarIconOfProfileAtIndex(index);
97    text = cache.GetNameOfProfileAtIndex(index);
98  }
99  if (avatar_button_) {
100    avatar_button_->SetAvatarIcon(avatar, is_gaia_picture);
101    if (!text.empty())
102      avatar_button_->SetText(text);
103  }
104
105  // For popups and panels which don't have the avatar button, we still
106  // need to draw the taskbar decoration.
107  chrome::DrawTaskbarDecoration(
108      frame_->GetNativeWindow(),
109      AvatarMenuModel::ShouldShowAvatarMenu() ? &avatar : NULL);
110}
111