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