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.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/theme_resources.h" 22 #include "third_party/skia/include/core/SkColor.h" 23 #include "ui/base/resource/resource_bundle.h" 24 #include "ui/base/theme_provider.h" 25 #include "ui/gfx/image/image.h" 26 #include "ui/views/background.h" 27 28 BrowserNonClientFrameView::BrowserNonClientFrameView(BrowserFrame* frame, 29 BrowserView* browser_view) 30 : frame_(frame), 31 browser_view_(browser_view), 32 avatar_button_(NULL), 33 avatar_label_(NULL), 34 new_avatar_button_(NULL) { 35 } 36 37 BrowserNonClientFrameView::~BrowserNonClientFrameView() { 38 } 39 40 void 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 if (!browser_view_->IsRegularOrGuestSession() || 48 !switches::IsNewAvatarMenu()) 49 UpdateAvatarInfo(); 50 } 51 52 void BrowserNonClientFrameView::OnThemeChanged() { 53 if (avatar_label_) 54 avatar_label_->UpdateLabelStyle(); 55 } 56 57 void BrowserNonClientFrameView::UpdateAvatarInfo() { 58 if (browser_view_->ShouldShowAvatar()) { 59 if (!avatar_button_) { 60 Profile* profile = browser_view_->browser()->profile(); 61 if (profile->IsSupervised() && !avatar_label_) { 62 avatar_label_ = new AvatarLabel(browser_view_); 63 avatar_label_->set_id(VIEW_ID_AVATAR_LABEL); 64 AddChildView(avatar_label_); 65 } 66 avatar_button_ = new AvatarMenuButton( 67 browser_view_->browser(), !browser_view_->IsRegularOrGuestSession()); 68 avatar_button_->set_id(VIEW_ID_AVATAR_BUTTON); 69 AddChildView(avatar_button_); 70 // Invalidate here because adding a child does not invalidate the layout. 71 InvalidateLayout(); 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 gfx::Image taskbar_badge_avatar; 90 base::string16 text; 91 bool is_rectangle = false; 92 if (browser_view_->IsGuestSession()) { 93 avatar = rb. 94 GetImageNamed(profiles::GetPlaceholderAvatarIconResourceID()); 95 } else if (browser_view_->IsOffTheRecord()) { 96 avatar = rb.GetImageNamed(IDR_OTR_ICON); 97 // TODO(nkostylev): Allow this on ChromeOS once the ChromeOS test 98 // environment handles profile directories correctly. 99 #if !defined(OS_CHROMEOS) 100 bool is_badge_rectangle = false; 101 // The taskbar badge should be the profile avatar, not the OTR avatar. 102 AvatarMenu::GetImageForMenuButton(browser_view_->browser()->profile(), 103 &taskbar_badge_avatar, 104 &is_badge_rectangle); 105 #endif 106 } else if (avatar_button_ || AvatarMenu::ShouldShowAvatarMenu()) { 107 ProfileInfoCache& cache = 108 g_browser_process->profile_manager()->GetProfileInfoCache(); 109 Profile* profile = browser_view_->browser()->profile(); 110 size_t index = cache.GetIndexOfProfileWithPath(profile->GetPath()); 111 if (index == std::string::npos) 112 return; 113 text = cache.GetNameOfProfileAtIndex(index); 114 115 AvatarMenu::GetImageForMenuButton(browser_view_->browser()->profile(), 116 &avatar, 117 &is_rectangle); 118 // Disable the menu when we should not show the menu. 119 if (avatar_button_ && !AvatarMenu::ShouldShowAvatarMenu()) 120 avatar_button_->SetEnabled(false); 121 } 122 if (avatar_button_) 123 avatar_button_->SetAvatarIcon(avatar, is_rectangle); 124 125 // For popups and panels which don't have the avatar button, we still 126 // need to draw the taskbar decoration. Even though we have an icon on the 127 // window's relaunch details, we draw over it because the user may have pinned 128 // the badge-less Chrome shortcut which will cause windows to ignore the 129 // relaunch details. 130 // TODO(calamity): ideally this should not be necessary but due to issues with 131 // the default shortcut being pinned, we add the runtime badge for safety. 132 // See crbug.com/313800. 133 chrome::DrawTaskbarDecoration( 134 frame_->GetNativeWindow(), 135 AvatarMenu::ShouldShowAvatarMenu() 136 ? (taskbar_badge_avatar.IsEmpty() ? &avatar : &taskbar_badge_avatar) 137 : NULL); 138 } 139 140 void BrowserNonClientFrameView::UpdateNewStyleAvatarInfo( 141 views::ButtonListener* listener, 142 const NewAvatarButton::AvatarButtonStyle style) { 143 DCHECK(switches::IsNewAvatarMenu()); 144 // This should never be called in incognito mode. 145 DCHECK(browser_view_->IsRegularOrGuestSession()); 146 147 if (browser_view_->ShouldShowAvatar()) { 148 if (!new_avatar_button_) { 149 new_avatar_button_ = 150 new NewAvatarButton(listener, style, browser_view_->browser()); 151 new_avatar_button_->set_id(VIEW_ID_NEW_AVATAR_BUTTON); 152 AddChildView(new_avatar_button_); 153 frame_->GetRootView()->Layout(); 154 } 155 } else if (new_avatar_button_) { 156 delete new_avatar_button_; 157 new_avatar_button_ = NULL; 158 frame_->GetRootView()->Layout(); 159 } 160 } 161