Home | History | Annotate | Download | only in profiles
      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 #ifndef CHROME_BROWSER_PROFILES_AVATAR_MENU_MODEL_H_
      6 #define CHROME_BROWSER_PROFILES_AVATAR_MENU_MODEL_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/compiler_specific.h"
     12 #include "base/strings/string16.h"
     13 #include "chrome/browser/profiles/profile_metrics.h"
     14 #include "content/public/browser/notification_observer.h"
     15 #include "content/public/browser/notification_registrar.h"
     16 #include "content/public/browser/web_contents.h"
     17 #include "content/public/browser/web_contents_observer.h"
     18 #include "ui/gfx/image/image.h"
     19 
     20 class AvatarMenuModelObserver;
     21 class Browser;
     22 class Profile;
     23 class ProfileInfoInterface;
     24 
     25 // This class is the model for the menu-like interface that appears when the
     26 // avatar icon is clicked in the browser window frame. This class will notify
     27 // its observer when the backend data changes, and the controller/view for this
     28 // model should forward actions back to it in response to user events.
     29 class AvatarMenuModel : public content::NotificationObserver {
     30  public:
     31   // Represents an item in the menu.
     32   struct Item {
     33     Item(size_t model_index, const gfx::Image& icon);
     34     ~Item();
     35 
     36     // The icon to be displayed next to the item.
     37     gfx::Image icon;
     38 
     39     // Whether or not the current browser is using this profile.
     40     bool active;
     41 
     42     // The name of this profile.
     43     string16 name;
     44 
     45     // A string representing the sync state of the profile.
     46     string16 sync_state;
     47 
     48     // Whether or not the current profile is signed in. If true, |sync_state| is
     49     // expected to be the email of the signed in user.
     50     bool signed_in;
     51 
     52     // Whether or not the current profile requires sign-in before use.
     53     bool signin_required;
     54 
     55     // The index in the |profile_cache| that this Item represents.
     56     size_t model_index;
     57   };
     58 
     59   // Constructor. |observer| can be NULL. |browser| can be NULL and a new one
     60   // will be created if an action requires it.
     61   AvatarMenuModel(ProfileInfoInterface* profile_cache,
     62                   AvatarMenuModelObserver* observer,
     63                   Browser* browser);
     64   virtual ~AvatarMenuModel();
     65 
     66   // Actions performed by the view that the controller forwards back to the
     67   // model:
     68   // Opens a Browser with the specified profile in response to the user
     69   // selecting an item. If |always_create| is true then a new window is created
     70   // even if a window for that profile already exists.
     71   void SwitchToProfile(size_t index, bool always_create);
     72   // Opens the profile settings in response to clicking the edit button next to
     73   // an item.
     74   void EditProfile(size_t index);
     75   // Creates a new profile.
     76   void AddNewProfile(ProfileMetrics::ProfileAdd type);
     77   // Creates a new guest user window.
     78   static void SwitchToGuestProfileWindow(Browser* browser);
     79 
     80   // Gets the path associated with the profile at |index|.
     81   base::FilePath GetProfilePath(size_t index);
     82 
     83   // Gets the number of profiles.
     84   size_t GetNumberOfItems();
     85 
     86   // Returns the index of the active profile.
     87   size_t GetActiveProfileIndex();
     88 
     89   // Gets the an Item at a specified index.
     90   const Item& GetItemAt(size_t index);
     91 
     92   // Returns true if the add profile link should be shown.
     93   bool ShouldShowAddNewProfileLink() const;
     94 
     95   // Returns information about a managed user which will be displayed in the
     96   // avatar menu. If the profile does not belong to a managed user, an empty
     97   // string will be returned.
     98   base::string16 GetManagedUserInformation() const;
     99 
    100   // Returns the icon for the managed user which will be displayed in the
    101   // avatar menu.
    102   const gfx::Image& GetManagedUserIcon() const;
    103 
    104   // This model is also used for the always-present Mac system menubar. As the
    105   // last active browser changes, the model needs to update accordingly.
    106   void set_browser(Browser* browser) { browser_ = browser; }
    107 
    108   // content::NotificationObserver:
    109   virtual void Observe(int type,
    110                        const content::NotificationSource& source,
    111                        const content::NotificationDetails& details) OVERRIDE;
    112 
    113   // True if avatar menu should be displayed.
    114   static bool ShouldShowAvatarMenu();
    115 
    116   // Start the sign-out process for this profile.
    117   // Parameter |logout_override| alows changing the destination URL for the
    118   // sign-out process and return value (the WebContents executing the sign-out)
    119   // are for testing; pass NULL for normal use.
    120   content::WebContents* BeginSignOut();
    121 
    122   // Use a different URL for logout (for testing only).
    123   void SetLogoutURL(const std::string& logout_url) {
    124     logout_override_ = logout_url;
    125   }
    126 
    127  private:
    128   // Rebuilds the menu from the cache and notifies the |observer_|.
    129   void RebuildMenu();
    130 
    131   // Clears the list of items, deleting each.
    132   void ClearMenu();
    133 
    134   // The cache that provides the profile information. Weak.
    135   ProfileInfoInterface* profile_info_;
    136 
    137   // The observer of this model, which is notified of changes. Weak.
    138   AvatarMenuModelObserver* observer_;
    139 
    140   // Browser in which this avatar menu resides. Weak.
    141   Browser* browser_;
    142 
    143   // List of built "menu items."
    144   std::vector<Item*> items_;
    145 
    146   // Listens for notifications from the ProfileInfoCache.
    147   content::NotificationRegistrar registrar_;
    148 
    149   // Special "override" logout URL used to let tests work.
    150   std::string logout_override_;
    151 
    152   DISALLOW_COPY_AND_ASSIGN(AvatarMenuModel);
    153 };
    154 
    155 #endif  // CHROME_BROWSER_PROFILES_AVATAR_MENU_MODEL_H_
    156