browser_non_client_frame_view.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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/themes/theme_properties.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#include "ui/views/controls/label.h"
25
26#if defined(ENABLE_MANAGED_USERS)
27#include "chrome/browser/managed_mode/managed_user_service.h"
28#include "chrome/browser/managed_mode/managed_user_service_factory.h"
29#endif
30
31BrowserNonClientFrameView::BrowserNonClientFrameView(BrowserFrame* frame,
32                                                     BrowserView* browser_view)
33    : frame_(frame),
34      browser_view_(browser_view) {
35}
36
37BrowserNonClientFrameView::~BrowserNonClientFrameView() {
38}
39
40void BrowserNonClientFrameView::VisibilityChanged(views::View* starting_from,
41                                                  bool is_visible) {
42  if (!is_visible)
43    return;
44  // The first time UpdateAvatarInfo() is called the window is not visible so
45  // DrawTaskBarDecoration() has no effect. Therefore we need to call it again
46  // once the window is visible.
47  UpdateAvatarInfo();
48}
49
50void BrowserNonClientFrameView::OnThemeChanged() {
51  UpdateAvatarLabelStyle();
52}
53
54void BrowserNonClientFrameView::UpdateAvatarLabelStyle() {
55  if (!avatar_label_.get())
56    return;
57
58  ui::ThemeProvider* tp = frame_->GetThemeProvider();
59  SkColor color_background = tp->GetColor(
60      ThemeProperties::COLOR_MANAGED_USER_LABEL_BACKGROUND);
61  avatar_label_->set_background(
62      views::Background::CreateSolidBackground(color_background));
63  avatar_label_->SetBackgroundColor(color_background);
64  SkColor color_label = tp->GetColor(
65      ThemeProperties::COLOR_MANAGED_USER_LABEL);
66  avatar_label_->SetEnabledColor(color_label);
67}
68
69void BrowserNonClientFrameView::UpdateAvatarInfo() {
70  if (browser_view_->ShouldShowAvatar()) {
71    if (!avatar_button_.get()) {
72      avatar_button_.reset(
73          new AvatarMenuButton(browser_view_->browser(),
74                               browser_view_->IsOffTheRecord()));
75      AddChildView(avatar_button_.get());
76#if defined(ENABLE_MANAGED_USERS)
77      Profile* profile = browser_view_->browser()->profile();
78      ManagedUserService* service =
79          ManagedUserServiceFactory::GetForProfile(profile);
80      if (service->ProfileIsManaged() && !avatar_label_.get()) {
81        ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
82        avatar_label_.reset(new views::Label(
83            l10n_util::GetStringUTF16(IDS_MANAGED_USER_AVATAR_LABEL),
84            rb.GetFont(ui::ResourceBundle::BoldFont)));
85        UpdateAvatarLabelStyle();
86        AddChildView(avatar_label_.get());
87      }
88#endif
89      frame_->GetRootView()->Layout();
90    }
91  } else if (avatar_button_.get()) {
92    // The avatar label can just be there if there is also an avatar button.
93    if (avatar_label_.get()) {
94      RemoveChildView(avatar_label_.get());
95      avatar_label_.reset();
96    }
97    RemoveChildView(avatar_button_.get());
98    avatar_button_.reset();
99    frame_->GetRootView()->Layout();
100  }
101
102  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
103  gfx::Image avatar;
104  string16 text;
105  bool is_gaia_picture = false;
106  if (browser_view_->IsOffTheRecord()) {
107    avatar = rb.GetImageNamed(browser_view_->GetOTRIconResourceID());
108  } else if (AvatarMenuModel::ShouldShowAvatarMenu()) {
109    ProfileInfoCache& cache =
110        g_browser_process->profile_manager()->GetProfileInfoCache();
111    Profile* profile = browser_view_->browser()->profile();
112    size_t index = cache.GetIndexOfProfileWithPath(profile->GetPath());
113    if (index == std::string::npos)
114      return;
115    is_gaia_picture =
116        cache.IsUsingGAIAPictureOfProfileAtIndex(index) &&
117        cache.GetGAIAPictureOfProfileAtIndex(index);
118    avatar = cache.GetAvatarIconOfProfileAtIndex(index);
119    text = cache.GetNameOfProfileAtIndex(index);
120  }
121  if (avatar_button_.get()) {
122    avatar_button_->SetAvatarIcon(avatar, is_gaia_picture);
123    if (!text.empty())
124      avatar_button_->SetText(text);
125  }
126
127  // For popups and panels which don't have the avatar button, we still
128  // need to draw the taskbar decoration.
129  chrome::DrawTaskbarDecoration(
130      frame_->GetNativeWindow(),
131      AvatarMenuModel::ShouldShowAvatarMenu() ? &avatar : NULL);
132}
133