Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / frame / browser_non_client_frame_view.cc
index 232ad19..a6a3872 100644 (file)
@@ -14,7 +14,6 @@
 #include "chrome/browser/ui/view_ids.h"
 #include "chrome/browser/ui/views/frame/browser_view.h"
 #include "chrome/browser/ui/views/frame/taskbar_decorator.h"
-#include "chrome/browser/ui/views/profiles/avatar_label.h"
 #include "chrome/browser/ui/views/profiles/avatar_menu_button.h"
 #include "chrome/browser/ui/views/profiles/new_avatar_button.h"
 #include "components/signin/core/common/profile_management_switches.h"
 #include "ui/gfx/image/image.h"
 #include "ui/views/background.h"
 
+#if defined(ENABLE_MANAGED_USERS)
+#include "chrome/browser/ui/views/profiles/supervised_user_avatar_label.h"
+#endif
+
 BrowserNonClientFrameView::BrowserNonClientFrameView(BrowserFrame* frame,
                                                      BrowserView* browser_view)
     : frame_(frame),
       browser_view_(browser_view),
       avatar_button_(NULL),
-      avatar_label_(NULL),
+#if defined(ENABLE_MANAGED_USERS)
+      supervised_user_avatar_label_(NULL),
+#endif
       new_avatar_button_(NULL) {
+  // The profile manager may by NULL in tests.
+  if (g_browser_process->profile_manager()) {
+    ProfileInfoCache& cache =
+        g_browser_process->profile_manager()->GetProfileInfoCache();
+    cache.AddObserver(this);
+  }
 }
 
 BrowserNonClientFrameView::~BrowserNonClientFrameView() {
+  // The profile manager may by NULL in tests.
+  if (g_browser_process->profile_manager()) {
+    ProfileInfoCache& cache =
+        g_browser_process->profile_manager()->GetProfileInfoCache();
+    cache.RemoveObserver(this);
+  }
 }
 
 void BrowserNonClientFrameView::VisibilityChanged(views::View* starting_from,
                                                   bool is_visible) {
   if (!is_visible)
     return;
+
   // The first time UpdateAvatarInfo() is called the window is not visible so
   // DrawTaskBarDecoration() has no effect. Therefore we need to call it again
   // once the window is visible.
   if (!browser_view_->IsRegularOrGuestSession() ||
-      !switches::IsNewAvatarMenu())
+      !switches::IsNewAvatarMenu()) {
     UpdateAvatarInfo();
+  }
+
+  // Make sure the task bar icon is correctly updated call
+  // |OnProfileAvatarChanged()| in this case, but only for non guest profiles.
+  if (!browser_view_->IsGuestSession() || !switches::IsNewAvatarMenu())
+    OnProfileAvatarChanged(base::FilePath());
 }
 
+#if defined(ENABLE_MANAGED_USERS)
 void BrowserNonClientFrameView::OnThemeChanged() {
-  if (avatar_label_)
-    avatar_label_->UpdateLabelStyle();
+  if (supervised_user_avatar_label_)
+    supervised_user_avatar_label_->UpdateLabelStyle();
 }
+#endif
 
 void BrowserNonClientFrameView::UpdateAvatarInfo() {
   if (browser_view_->ShouldShowAvatar()) {
     if (!avatar_button_) {
+#if defined(ENABLE_MANAGED_USERS)
       Profile* profile = browser_view_->browser()->profile();
-      if (profile->IsSupervised() && !avatar_label_) {
-        avatar_label_ = new AvatarLabel(browser_view_);
-        avatar_label_->set_id(VIEW_ID_AVATAR_LABEL);
-        AddChildView(avatar_label_);
+      if (profile->IsSupervised() && !supervised_user_avatar_label_) {
+        supervised_user_avatar_label_ =
+            new SupervisedUserAvatarLabel(browser_view_);
+        supervised_user_avatar_label_->set_id(
+            VIEW_ID_SUPERVISED_USER_AVATAR_LABEL);
+        AddChildView(supervised_user_avatar_label_);
       }
+#endif
       avatar_button_ = new AvatarMenuButton(
           browser_view_->browser(), !browser_view_->IsRegularOrGuestSession());
       avatar_button_->set_id(VIEW_ID_AVATAR_BUTTON);
@@ -72,69 +102,39 @@ void BrowserNonClientFrameView::UpdateAvatarInfo() {
       frame_->GetRootView()->Layout();
     }
   } else if (avatar_button_) {
+#if defined(ENABLE_MANAGED_USERS)
     // The avatar label can just be there if there is also an avatar button.
-    if (avatar_label_) {
-      RemoveChildView(avatar_label_);
-      delete avatar_label_;
-      avatar_label_ = NULL;
+    if (supervised_user_avatar_label_) {
+      RemoveChildView(supervised_user_avatar_label_);
+      delete supervised_user_avatar_label_;
+      supervised_user_avatar_label_ = NULL;
     }
+#endif
     RemoveChildView(avatar_button_);
     delete avatar_button_;
     avatar_button_ = NULL;
     frame_->GetRootView()->Layout();
   }
 
-  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
   gfx::Image avatar;
   gfx::Image taskbar_badge_avatar;
-  base::string16 text;
   bool is_rectangle = false;
-  if (browser_view_->IsGuestSession()) {
-    avatar = rb.
-        GetImageNamed(profiles::GetPlaceholderAvatarIconResourceID());
-  } else if (browser_view_->IsOffTheRecord()) {
-    avatar = rb.GetImageNamed(IDR_OTR_ICON);
-    // TODO(nkostylev): Allow this on ChromeOS once the ChromeOS test
-    // environment handles profile directories correctly.
-#if !defined(OS_CHROMEOS)
-    bool is_badge_rectangle = false;
-    // The taskbar badge should be the profile avatar, not the OTR avatar.
-    AvatarMenu::GetImageForMenuButton(browser_view_->browser()->profile(),
-                                      &taskbar_badge_avatar,
-                                      &is_badge_rectangle);
-#endif
-  } else if (avatar_button_ || AvatarMenu::ShouldShowAvatarMenu()) {
-    ProfileInfoCache& cache =
-        g_browser_process->profile_manager()->GetProfileInfoCache();
-    Profile* profile = browser_view_->browser()->profile();
-    size_t index = cache.GetIndexOfProfileWithPath(profile->GetPath());
-    if (index == std::string::npos)
-      return;
-    text = cache.GetNameOfProfileAtIndex(index);
-
-    AvatarMenu::GetImageForMenuButton(browser_view_->browser()->profile(),
-                                      &avatar,
-                                      &is_rectangle);
-    // Disable the menu when we should not show the menu.
-    if (avatar_button_ && !AvatarMenu::ShouldShowAvatarMenu())
-      avatar_button_->SetEnabled(false);
+
+  // Update the avatar button in the window frame and the taskbar overlay.
+  bool should_show_avatar_menu =
+      avatar_button_ || AvatarMenu::ShouldShowAvatarMenu();
+
+  if (!AvatarMenuButton::GetAvatarImages(
+          browser_view_->browser()->profile(), should_show_avatar_menu, &avatar,
+          &taskbar_badge_avatar, &is_rectangle)) {
+    return;
   }
+
+  // Disable the menu when we should not show the menu.
+  if (avatar_button_ && !AvatarMenu::ShouldShowAvatarMenu())
+    avatar_button_->SetEnabled(false);
   if (avatar_button_)
     avatar_button_->SetAvatarIcon(avatar, is_rectangle);
-
-  // For popups and panels which don't have the avatar button, we still
-  // need to draw the taskbar decoration. Even though we have an icon on the
-  // window's relaunch details, we draw over it because the user may have pinned
-  // the badge-less Chrome shortcut which will cause windows to ignore the
-  // relaunch details.
-  // TODO(calamity): ideally this should not be necessary but due to issues with
-  // the default shortcut being pinned, we add the runtime badge for safety.
-  // See crbug.com/313800.
-  chrome::DrawTaskbarDecoration(
-      frame_->GetNativeWindow(),
-      AvatarMenu::ShouldShowAvatarMenu()
-          ? (taskbar_badge_avatar.IsEmpty() ? &avatar : &taskbar_badge_avatar)
-          : NULL);
 }
 
 void BrowserNonClientFrameView::UpdateNewStyleAvatarInfo(
@@ -158,3 +158,52 @@ void BrowserNonClientFrameView::UpdateNewStyleAvatarInfo(
     frame_->GetRootView()->Layout();
   }
 }
+
+void BrowserNonClientFrameView::DrawTaskbarDecoration(
+    const gfx::Image& avatar,
+    const gfx::Image& taskbar_badge_avatar) {
+  // For popups and panels which don't have the avatar button, we still
+  // need to draw the taskbar decoration. Even though we have an icon on the
+  // window's relaunch details, we draw over it because the user may have pinned
+  // the badge-less Chrome shortcut which will cause windows to ignore the
+  // relaunch details.
+  // TODO(calamity): ideally this should not be necessary but due to issues with
+  // the default shortcut being pinned, we add the runtime badge for safety.
+  // See crbug.com/313800.
+  bool show_decoration = AvatarMenu::ShouldShowAvatarMenu() &&
+      !browser_view_->browser()->profile()->IsGuestSession();
+  // In tests, make sure that the browser process and profile manager are valid
+  // before using.
+  if (g_browser_process && g_browser_process->profile_manager()) {
+    const ProfileInfoCache& cache =
+        g_browser_process->profile_manager()->GetProfileInfoCache();
+    show_decoration = show_decoration && cache.GetNumberOfProfiles() > 1;
+  }
+  chrome::DrawTaskbarDecoration(frame_->GetNativeWindow(),
+      show_decoration
+          ? (taskbar_badge_avatar.IsEmpty() ? &avatar : &taskbar_badge_avatar)
+          : NULL);
+}
+
+void BrowserNonClientFrameView::OnProfileAdded(
+    const base::FilePath& profile_path) {
+  OnProfileAvatarChanged(profile_path);
+}
+
+void BrowserNonClientFrameView::OnProfileWasRemoved(
+    const base::FilePath& profile_path,
+    const base::string16& profile_name) {
+  OnProfileAvatarChanged(profile_path);
+}
+
+void BrowserNonClientFrameView::OnProfileAvatarChanged(
+    const base::FilePath& profile_path) {
+  gfx::Image avatar;
+  gfx::Image taskbar_badge_avatar;
+  bool is_rectangle;
+  // Only need to update the taskbar overlay here.
+  AvatarMenuButton::GetAvatarImages(browser_view_->browser()->profile(),
+                                    AvatarMenu::ShouldShowAvatarMenu(), &avatar,
+                                    &taskbar_badge_avatar, &is_rectangle);
+  DrawTaskbarDecoration(avatar, taskbar_badge_avatar);
+}