Home | History | Annotate | Download | only in contacts
      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_CHROMEOS_CONTACTS_CONTACT_MANAGER_H_
      6 #define CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_MANAGER_H_
      7 
      8 #include <map>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/compiler_specific.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/memory/weak_ptr.h"
     15 #include "base/observer_list.h"
     16 #include "base/stl_util.h"
     17 #include "chrome/browser/chromeos/contacts/contact_store_observer.h"
     18 #include "content/public/browser/notification_observer.h"
     19 #include "content/public/browser/notification_registrar.h"
     20 
     21 class Profile;
     22 
     23 namespace contacts {
     24 
     25 class Contact;
     26 typedef std::vector<const Contact*> ContactPointers;
     27 
     28 class ContactManagerObserver;
     29 class ContactStore;
     30 class ContactStoreFactory;
     31 
     32 // Class that exposes contacts to rest of the browser.
     33 class ContactManagerInterface {
     34  public:
     35   ContactManagerInterface() {}
     36   virtual ~ContactManagerInterface() {}
     37 
     38   // Returns a weak pointer tied to the lifetime of this object.
     39   virtual base::WeakPtr<ContactManagerInterface> GetWeakPtr() = 0;
     40 
     41   // Adds or removes an observer for changes to |profile|'s contacts.
     42   virtual void AddObserver(ContactManagerObserver* observer,
     43                            Profile* profile) = 0;
     44   virtual void RemoveObserver(ContactManagerObserver* observer,
     45                               Profile* profile) = 0;
     46 
     47   // Returns pointers to all currently-loaded contacts for |profile|.  The
     48   // returned Contact objects may not persist indefinitely; the caller must not
     49   // refer to them again after unblocking the UI thread.
     50   virtual scoped_ptr<ContactPointers> GetAllContacts(Profile* profile) = 0;
     51 
     52   // Returns the contact identified by |contact_id|.
     53   // NULL is returned if the contact doesn't exist.
     54   virtual const Contact* GetContactById(Profile* profile,
     55                                         const std::string& contact_id) = 0;
     56 
     57  private:
     58   DISALLOW_COPY_AND_ASSIGN(ContactManagerInterface);
     59 };
     60 
     61 // Real, singleton implementation of ContactManagerInterface.
     62 class ContactManager : public ContactManagerInterface,
     63                        public ContactStoreObserver,
     64                        public content::NotificationObserver {
     65  public:
     66   static ContactManager* GetInstance();
     67 
     68   ContactManager();
     69   virtual ~ContactManager();
     70 
     71   // Swaps in a new factory to use for creating ContactStores.
     72   // Must be called before any stores have been created.
     73   void SetContactStoreForTesting(scoped_ptr<ContactStoreFactory> factory);
     74 
     75   void Init();
     76 
     77   // ContactManagerInterface overrides:
     78   virtual base::WeakPtr<ContactManagerInterface> GetWeakPtr() OVERRIDE;
     79   virtual void AddObserver(ContactManagerObserver* observer,
     80                            Profile* profile) OVERRIDE;
     81   virtual void RemoveObserver(ContactManagerObserver* observer,
     82                               Profile* profile) OVERRIDE;
     83   virtual scoped_ptr<ContactPointers> GetAllContacts(Profile* profile) OVERRIDE;
     84   virtual const Contact* GetContactById(Profile* profile,
     85                                         const std::string& contact_id) OVERRIDE;
     86 
     87   // ContactStoreObserver overrides:
     88   virtual void OnContactsUpdated(ContactStore* store) OVERRIDE;
     89 
     90   // content::NotificationObserver overrides:
     91   virtual void Observe(int type,
     92                        const content::NotificationSource& source,
     93                        const content::NotificationDetails& details) OVERRIDE;
     94 
     95  private:
     96   typedef ObserverList<ContactManagerObserver> Observers;
     97   typedef std::map<Profile*, ContactStore*> ContactStoreMap;
     98   typedef std::map<Profile*, Observers*> ProfileObserversMap;
     99 
    100   // Returns the list of observers interested in |profile|.  If not present,
    101   // creates a new list if |create| is true and returns NULL otherwise.
    102   Observers* GetObserversForProfile(Profile* profile, bool create);
    103 
    104   // Handles profile creation and destruction.
    105   void HandleProfileCreated(Profile* profile);
    106   void HandleProfileDestroyed(Profile* profile);
    107 
    108   content::NotificationRegistrar registrar_;
    109 
    110   // Maps from a profile to observers that are interested in changes to that
    111   // profile's contacts.
    112   ProfileObserversMap profile_observers_;
    113 
    114   // Deletes values in |profile_observers_|.
    115   STLValueDeleter<ProfileObserversMap> profile_observers_deleter_;
    116 
    117   // Creates objects for |contact_stores_|.
    118   scoped_ptr<ContactStoreFactory> contact_store_factory_;
    119 
    120   // Maps from a profile to a store for getting the profile's contacts.
    121   ContactStoreMap contact_stores_;
    122 
    123   // Deletes values in |contact_stores_|.
    124   STLValueDeleter<ContactStoreMap> contact_stores_deleter_;
    125 
    126   // Note: This should remain the last member so it'll be destroyed and
    127   // invalidate its weak pointers before any other members are destroyed.
    128   base::WeakPtrFactory<ContactManagerInterface> weak_ptr_factory_;
    129 
    130   DISALLOW_COPY_AND_ASSIGN(ContactManager);
    131 };
    132 
    133 }  // namespace contacts
    134 
    135 #endif  // CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_MANAGER_H_
    136