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