[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / views / frame / browser_non_client_frame_view.cc
blobd83a38280fae210240d1b01a4f4f04f1d7b5b5fb
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.
5 #include "chrome/browser/ui/views/frame/browser_non_client_frame_view.h"
7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/profiles/avatar_menu.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/profiles/profile_avatar_icon_util.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/frame/browser_view.h"
16 #include "chrome/browser/ui/views/frame/taskbar_decorator.h"
17 #include "chrome/browser/ui/views/profiles/avatar_label.h"
18 #include "chrome/browser/ui/views/profiles/avatar_menu_button.h"
19 #include "chrome/browser/ui/views/profiles/new_avatar_button.h"
20 #include "components/signin/core/common/profile_management_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"
30 BrowserNonClientFrameView::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) {
39 BrowserNonClientFrameView::~BrowserNonClientFrameView() {
42 void 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 !switches::IsNewProfileManagement())
51 UpdateAvatarInfo();
54 void BrowserNonClientFrameView::OnThemeChanged() {
55 if (avatar_label_)
56 avatar_label_->UpdateLabelStyle();
59 void 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_);
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 // Invalidate here because adding a child does not invalidate the layout.
73 InvalidateLayout();
74 frame_->GetRootView()->Layout();
76 } else if (avatar_button_) {
77 // The avatar label can just be there if there is also an avatar button.
78 if (avatar_label_) {
79 RemoveChildView(avatar_label_);
80 delete avatar_label_;
81 avatar_label_ = NULL;
83 RemoveChildView(avatar_button_);
84 delete avatar_button_;
85 avatar_button_ = NULL;
86 frame_->GetRootView()->Layout();
89 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
90 gfx::Image avatar;
91 gfx::Image taskbar_badge_avatar;
92 base::string16 text;
93 bool is_rectangle = false;
94 if (browser_view_->IsGuestSession()) {
95 avatar = rb.
96 GetImageNamed(profiles::GetPlaceholderAvatarIconResourceID());
97 } else if (browser_view_->IsOffTheRecord()) {
98 avatar = rb.GetImageNamed(IDR_OTR_ICON);
99 // TODO(nkostylev): Allow this on ChromeOS once the ChromeOS test
100 // environment handles profile directories correctly.
101 #if !defined(OS_CHROMEOS)
102 bool is_badge_rectangle = false;
103 // The taskbar badge should be the profile avatar, not the OTR avatar.
104 AvatarMenu::GetImageForMenuButton(browser_view_->browser()->profile(),
105 &taskbar_badge_avatar,
106 &is_badge_rectangle);
107 #endif
108 } else if (avatar_button_ || AvatarMenu::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 text = cache.GetNameOfProfileAtIndex(index);
117 AvatarMenu::GetImageForMenuButton(browser_view_->browser()->profile(),
118 &avatar,
119 &is_rectangle);
120 // Disable the menu when we should not show the menu.
121 if (avatar_button_ && !AvatarMenu::ShouldShowAvatarMenu())
122 avatar_button_->SetEnabled(false);
124 if (avatar_button_) {
125 avatar_button_->SetAvatarIcon(avatar, is_rectangle);
126 if (!text.empty())
127 avatar_button_->SetText(text);
130 // For popups and panels which don't have the avatar button, we still
131 // need to draw the taskbar decoration. Even though we have an icon on the
132 // window's relaunch details, we draw over it because the user may have pinned
133 // the badge-less Chrome shortcut which will cause windows to ignore the
134 // relaunch details.
135 // TODO(calamity): ideally this should not be necessary but due to issues with
136 // the default shortcut being pinned, we add the runtime badge for safety.
137 // See crbug.com/313800.
138 chrome::DrawTaskbarDecoration(
139 frame_->GetNativeWindow(),
140 AvatarMenu::ShouldShowAvatarMenu()
141 ? (taskbar_badge_avatar.IsEmpty() ? &avatar : &taskbar_badge_avatar)
142 : NULL);
145 void BrowserNonClientFrameView::UpdateNewStyleAvatarInfo(
146 views::ButtonListener* listener,
147 const NewAvatarButton::AvatarButtonStyle style) {
148 DCHECK(switches::IsNewAvatarMenu());
149 // This should never be called in incognito mode.
150 DCHECK(browser_view_->IsRegularOrGuestSession());
152 if (browser_view_->ShouldShowAvatar()) {
153 if (!new_avatar_button_) {
154 base::string16 profile_name = profiles::GetAvatarNameForProfile(
155 browser_view_->browser()->profile());
156 new_avatar_button_ = new NewAvatarButton(
157 listener, profile_name, style, browser_view_->browser());
158 new_avatar_button_->set_id(VIEW_ID_NEW_AVATAR_BUTTON);
159 AddChildView(new_avatar_button_);
160 frame_->GetRootView()->Layout();
162 } else if (new_avatar_button_) {
163 delete new_avatar_button_;
164 new_avatar_button_ = NULL;
165 frame_->GetRootView()->Layout();