browser_non_client_frame_view.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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      avatar_button_ = new AvatarMenuButton(browser_view_->browser(),
60                                            browser_view_->IsOffTheRecord());
61      AddChildView(avatar_button_);
62#if defined(ENABLE_MANAGED_USERS)
63      Profile* profile = browser_view_->browser()->profile();
64      ManagedUserService* service =
65          ManagedUserServiceFactory::GetForProfile(profile);
66      if (service->ProfileIsManaged() && !avatar_label_) {
67        avatar_label_ =
68            new AvatarLabel(browser_view_, frame_->GetThemeProvider());
69        AddChildView(avatar_label_);
70      }
71#endif
72      frame_->GetRootView()->Layout();
73    }
74  } else if (avatar_button_) {
75    // The avatar label can just be there if there is also an avatar button.
76    if (avatar_label_) {
77      RemoveChildView(avatar_label_);
78      delete avatar_label_;
79      avatar_label_ = NULL;
80    }
81    RemoveChildView(avatar_button_);
82    delete avatar_button_;
83    avatar_button_ = NULL;
84    frame_->GetRootView()->Layout();
85  }
86
87  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
88  gfx::Image avatar;
89  string16 text;
90  bool is_gaia_picture = false;
91  if (browser_view_->IsOffTheRecord()) {
92    avatar = rb.GetImageNamed(browser_view_->GetOTRIconResourceID());
93  } else if (AvatarMenuModel::ShouldShowAvatarMenu()) {
94    ProfileInfoCache& cache =
95        g_browser_process->profile_manager()->GetProfileInfoCache();
96    Profile* profile = browser_view_->browser()->profile();
97    size_t index = cache.GetIndexOfProfileWithPath(profile->GetPath());
98    if (index == std::string::npos)
99      return;
100    is_gaia_picture =
101        cache.IsUsingGAIAPictureOfProfileAtIndex(index) &&
102        cache.GetGAIAPictureOfProfileAtIndex(index);
103    avatar = cache.GetAvatarIconOfProfileAtIndex(index);
104    text = cache.GetNameOfProfileAtIndex(index);
105  }
106  if (avatar_button_) {
107    avatar_button_->SetAvatarIcon(avatar, is_gaia_picture);
108    if (!text.empty())
109      avatar_button_->SetText(text);
110  }
111
112  // For popups and panels which don't have the avatar button, we still
113  // need to draw the taskbar decoration.
114  chrome::DrawTaskbarDecoration(
115      frame_->GetNativeWindow(),
116      AvatarMenuModel::ShouldShowAvatarMenu() ? &avatar : NULL);
117}
118