browser_non_client_frame_view.cc revision f2477e01787aa58f445919b809d89e252beef54f
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 "base/command_line.h"
8#include "chrome/browser/browser_process.h"
9#include "chrome/browser/profiles/avatar_menu.h"
10#include "chrome/browser/profiles/profile.h"
11#include "chrome/browser/profiles/profile_info_cache.h"
12#include "chrome/browser/profiles/profile_manager.h"
13#include "chrome/browser/profiles/profiles_state.h"
14#include "chrome/browser/ui/view_ids.h"
15#include "chrome/browser/ui/views/avatar_label.h"
16#include "chrome/browser/ui/views/avatar_menu_button.h"
17#include "chrome/browser/ui/views/frame/browser_view.h"
18#include "chrome/browser/ui/views/frame/taskbar_decorator.h"
19#include "chrome/browser/ui/views/new_avatar_button.h"
20#include "chrome/common/chrome_switches.h"
21#include "grit/generated_resources.h"
22#include "grit/theme_resources.h"
23#include "third_party/skia/include/core/SkColor.h"
24#include "ui/base/l10n/l10n_util.h"
25#include "ui/base/resource/resource_bundle.h"
26#include "ui/base/theme_provider.h"
27#include "ui/gfx/image/image.h"
28#include "ui/views/background.h"
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      new_avatar_button_(NULL) {
37}
38
39BrowserNonClientFrameView::~BrowserNonClientFrameView() {
40}
41
42void BrowserNonClientFrameView::VisibilityChanged(views::View* starting_from,
43                                                  bool is_visible) {
44  if (!is_visible)
45    return;
46  // The first time UpdateAvatarInfo() is called the window is not visible so
47  // DrawTaskBarDecoration() has no effect. Therefore we need to call it again
48  // once the window is visible.
49  if (!browser_view_->IsRegularOrGuestSession() ||
50      !profiles::IsNewProfileManagementEnabled())
51    UpdateAvatarInfo();
52}
53
54void BrowserNonClientFrameView::OnThemeChanged() {
55  if (avatar_label_)
56    avatar_label_->UpdateLabelStyle();
57}
58
59void BrowserNonClientFrameView::UpdateAvatarInfo() {
60  if (browser_view_->ShouldShowAvatar()) {
61    if (!avatar_button_) {
62      Profile* profile = browser_view_->browser()->profile();
63      if (profile->IsManaged() && !avatar_label_) {
64        avatar_label_ = new AvatarLabel(browser_view_);
65        avatar_label_->set_id(VIEW_ID_AVATAR_LABEL);
66        AddChildView(avatar_label_);
67      }
68      avatar_button_ = new AvatarMenuButton(
69          browser_view_->browser(), !browser_view_->IsRegularOrGuestSession());
70      avatar_button_->set_id(VIEW_ID_AVATAR_BUTTON);
71      AddChildView(avatar_button_);
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_rectangle = false;
91  if (browser_view_->IsGuestSession()) {
92    avatar = rb.GetImageNamed(browser_view_->GetGuestIconResourceID());
93  } else if (browser_view_->IsOffTheRecord()) {
94    avatar = rb.GetImageNamed(browser_view_->GetOTRIconResourceID());
95  } else if (avatar_button_ || AvatarMenu::ShouldShowAvatarMenu()) {
96    ProfileInfoCache& cache =
97        g_browser_process->profile_manager()->GetProfileInfoCache();
98    Profile* profile = browser_view_->browser()->profile();
99    size_t index = cache.GetIndexOfProfileWithPath(profile->GetPath());
100    if (index == std::string::npos)
101      return;
102    text = cache.GetNameOfProfileAtIndex(index);
103
104    AvatarMenu::GetImageForMenuButton(browser_view_->browser()->profile(),
105                                      &avatar,
106                                      &is_rectangle);
107    // Disable the menu when we should not show the menu.
108    if (avatar_button_ && !AvatarMenu::ShouldShowAvatarMenu())
109      avatar_button_->SetEnabled(false);
110  }
111  if (avatar_button_) {
112    avatar_button_->SetAvatarIcon(avatar, is_rectangle);
113    if (!text.empty())
114      avatar_button_->SetText(text);
115  }
116
117  // For popups and panels which don't have the avatar button, we still
118  // need to draw the taskbar decoration. Even though we have an icon on the
119  // window's relaunch details, we draw over it because the user may have pinned
120  // the badge-less Chrome shortcut which will cause windows to ignore the
121  // relaunch details.
122  // TODO(calamity): ideally this should not be necessary but due to issues with
123  // the default shortcut being pinned, we add the runtime badge for safety.
124  // See crbug.com/313800.
125  chrome::DrawTaskbarDecoration(
126      frame_->GetNativeWindow(),
127      AvatarMenu::ShouldShowAvatarMenu() ? &avatar : NULL);
128}
129
130void BrowserNonClientFrameView::UpdateNewStyleAvatarInfo(
131    views::ButtonListener* listener,
132    const NewAvatarButton::AvatarButtonStyle style) {
133  DCHECK(profiles::IsNewProfileManagementEnabled());
134  // This should never be called in incognito mode.
135  DCHECK(browser_view_->IsRegularOrGuestSession());
136
137  if (browser_view_->ShouldShowAvatar()) {
138    if (!new_avatar_button_) {
139      string16 profile_name =
140          profiles::GetActiveProfileDisplayName(browser_view_->browser());
141      new_avatar_button_ = new NewAvatarButton(
142          listener, profile_name, style, browser_view_->browser());
143      new_avatar_button_->set_id(VIEW_ID_NEW_AVATAR_BUTTON);
144      AddChildView(new_avatar_button_);
145      frame_->GetRootView()->Layout();
146    }
147  } else if (new_avatar_button_) {
148    delete new_avatar_button_;
149    new_avatar_button_ = NULL;
150    frame_->GetRootView()->Layout();
151  }
152}
153