Commit c66f88be authored by derat@chromium.org's avatar derat@chromium.org

contacts: Add ContactManager.

This adds a singleton class that holds ContactStore objects
and exposes Contact objects to the rest of the browser.

BUG=128805
TEST=none
TBR=ben@chromium.org

Review URL: https://chromiumcodereview.appspot.com/10831162

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@149980 0039d316-1c4b-4281-b951-d872f2087c98
parent bba45f23
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/contacts/contact_manager.h"
#include "base/logging.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/contacts/contact.pb.h"
#include "chrome/browser/chromeos/contacts/contact_manager_observer.h"
#include "chrome/browser/chromeos/contacts/contact_store.h"
#include "chrome/browser/chromeos/contacts/google_contact_store.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
using content::BrowserThread;
namespace contacts {
namespace {
// Singleton instance.
ContactManager* g_instance = NULL;
} // namespace
// static
ContactManager* ContactManager::GetInstance() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(g_instance);
return g_instance;
}
ContactManager::ContactManager()
: profile_observers_deleter_(&profile_observers_),
contact_store_factory_(new GoogleContactStoreFactory),
contact_stores_deleter_(&contact_stores_) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!g_instance);
g_instance = this;
}
ContactManager::~ContactManager() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK_EQ(g_instance, this);
g_instance = NULL;
for (ContactStoreMap::const_iterator it = contact_stores_.begin();
it != contact_stores_.end(); ++it) {
it->second->RemoveObserver(this);
}
}
void ContactManager::SetContactStoreForTesting(
scoped_ptr<ContactStoreFactory> factory) {
DCHECK(contact_stores_.empty());
contact_store_factory_.swap(factory);
}
void ContactManager::Init() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
registrar_.Add(
this,
chrome::NOTIFICATION_PROFILE_CREATED,
content::NotificationService::AllSources());
registrar_.Add(
this,
chrome::NOTIFICATION_PROFILE_DESTROYED,
content::NotificationService::AllSources());
// Notify about any already-existing profiles.
std::vector<Profile*> profiles(
g_browser_process->profile_manager()->GetLoadedProfiles());
for (size_t i = 0; i < profiles.size(); ++i)
HandleProfileCreated(profiles[i]);
}
void ContactManager::AddObserver(ContactManagerObserver* observer,
Profile* profile) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(observer);
DCHECK(profile);
Observers* observers = GetObserversForProfile(profile, true);
observers->AddObserver(observer);
}
void ContactManager::RemoveObserver(ContactManagerObserver* observer,
Profile* profile) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(observer);
DCHECK(profile);
Observers* observers = GetObserversForProfile(profile, false);
if (observers)
observers->RemoveObserver(observer);
}
scoped_ptr<ContactPointers> ContactManager::GetAllContacts(Profile* profile) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(profile);
scoped_ptr<ContactPointers> contacts(new ContactPointers);
ContactStoreMap::const_iterator it = contact_stores_.find(profile);
if (it != contact_stores_.end())
it->second->AppendContacts(contacts.get());
return contacts.Pass();
}
const Contact* ContactManager::GetContactByProviderId(
Profile* profile,
const std::string& provider_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(profile);
ContactStoreMap::const_iterator it = contact_stores_.find(profile);
return it != contact_stores_.end() ?
it->second->GetContactByProviderId(provider_id) :
NULL;
}
void ContactManager::OnContactsUpdated(ContactStore* store) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
for (ContactStoreMap::const_iterator it = contact_stores_.begin();
it != contact_stores_.end(); ++it) {
if (it->second == store) {
Profile* profile = it->first;
Observers* observers = GetObserversForProfile(profile, false);
if (observers) {
FOR_EACH_OBSERVER(ContactManagerObserver,
*observers,
OnContactsUpdated(profile));
}
return;
}
}
NOTREACHED() << "Got update from unknown contact store " << store;
}
void ContactManager::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
switch (type) {
case chrome::NOTIFICATION_PROFILE_CREATED:
HandleProfileCreated(content::Source<Profile>(source).ptr());
break;
case chrome::NOTIFICATION_PROFILE_DESTROYED: {
Profile* profile = content::Details<Profile>(details).ptr();
if (profile)
HandleProfileDestroyed(profile);
break;
}
default:
NOTREACHED() << "Unexpected notification " << type;
}
}
ContactManager::Observers* ContactManager::GetObserversForProfile(
Profile* profile,
bool create) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
ProfileObserversMap::const_iterator it = profile_observers_.find(profile);
if (it != profile_observers_.end())
return it->second;
if (!create)
return NULL;
Observers* observers = new Observers;
profile_observers_[profile] = observers;
return observers;
}
void ContactManager::HandleProfileCreated(Profile* profile) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(profile);
ContactStoreMap::iterator it = contact_stores_.find(profile);
if (it != contact_stores_.end())
return;
if (!contact_store_factory_->CanCreateContactStoreForProfile(profile))
return;
VLOG(1) << "Adding profile " << profile->GetProfileName();
ContactStore* store = contact_store_factory_->CreateContactStore(profile);
DCHECK(store);
store->AddObserver(this);
store->Init();
DCHECK_EQ(contact_stores_.count(profile), static_cast<size_t>(0));
contact_stores_[profile] = store;
}
void ContactManager::HandleProfileDestroyed(Profile* profile) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(profile);
ContactStoreMap::iterator store_it = contact_stores_.find(profile);
if (store_it != contact_stores_.end()) {
store_it->second->RemoveObserver(this);
delete store_it->second;
contact_stores_.erase(store_it);
}
ProfileObserversMap::iterator observer_it = profile_observers_.find(profile);
if (observer_it != profile_observers_.end()) {
delete observer_it->second;
profile_observers_.erase(observer_it);
}
}
} // namespace contacts
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_MANAGER_H_
#define CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_MANAGER_H_
#include <map>
#include <vector>
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/observer_list.h"
#include "base/stl_util.h"
#include "chrome/browser/chromeos/contacts/contact_store_observer.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
class Profile;
namespace contacts {
class Contact;
typedef std::vector<const Contact*> ContactPointers;
class ContactManagerObserver;
class ContactStore;
class ContactStoreFactory;
// Singleton class that exposes contacts to rest of the browser.
class ContactManager : public ContactStoreObserver,
public content::NotificationObserver {
public:
static ContactManager* GetInstance();
ContactManager();
virtual ~ContactManager();
// Swaps in a new factory to use for creating ContactStores.
// Must be called before any stores have been created.
void SetContactStoreForTesting(scoped_ptr<ContactStoreFactory> factory);
void Init();
// Adds or removes an observer for changes to |profile|'s contacts.
void AddObserver(ContactManagerObserver* observer, Profile* profile);
void RemoveObserver(ContactManagerObserver* observer, Profile* profile);
// Returns pointers to all currently-loaded contacts for |profile|. The
// returned Contact objects may not persist indefinitely; the caller must not
// refer to them again after unblocking the UI thread.
scoped_ptr<ContactPointers> GetAllContacts(Profile* profile);
// Returns the contact identified by |provider_id|.
// NULL is returned if the contact doesn't exist.
const Contact* GetContactByProviderId(Profile* profile,
const std::string& provider_id);
// ContactStoreObserver overrides:
virtual void OnContactsUpdated(ContactStore* store) OVERRIDE;
// content::NotificationObserver overrides:
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
private:
typedef ObserverList<ContactManagerObserver> Observers;
typedef std::map<Profile*, ContactStore*> ContactStoreMap;
typedef std::map<Profile*, Observers*> ProfileObserversMap;
// Returns the list of observers interested in |profile|. If not present,
// creates a new list if |create| is true and returns NULL otherwise.
Observers* GetObserversForProfile(Profile* profile, bool create);
// Handles profile creation and destruction.
void HandleProfileCreated(Profile* profile);
void HandleProfileDestroyed(Profile* profile);
content::NotificationRegistrar registrar_;
// Maps from a profile to observers that are interested in changes to that
// profile's contacts.
ProfileObserversMap profile_observers_;
// Deletes values in |profile_observers_|.
STLValueDeleter<ProfileObserversMap> profile_observers_deleter_;
// Creates objects for |contact_stores_|.
scoped_ptr<ContactStoreFactory> contact_store_factory_;
// Maps from a profile to a store for getting the profile's contacts.
ContactStoreMap contact_stores_;
// Deletes values in |contact_stores_|.
STLValueDeleter<ContactStoreMap> contact_stores_deleter_;
DISALLOW_COPY_AND_ASSIGN(ContactManager);
};
} // namespace contacts
#endif // CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_MANAGER_H_
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_MANAGER_OBSERVER_H_
#define CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_MANAGER_OBSERVER_H_
class Profile;
namespace contacts {
class ContactManager;
// Interface for classes that need to observe changes to ContactManager.
class ContactManagerObserver {
public:
ContactManagerObserver() {}
virtual ~ContactManagerObserver() {}
// Called when |profile|'s contacts have been updated.
virtual void OnContactsUpdated(Profile* profile) = 0;
private:
DISALLOW_COPY_AND_ASSIGN(ContactManagerObserver);
};
} // namespace contacts
#endif // CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_MANAGER_OBSERVER_H_
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/contacts/contact_manager.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "chrome/browser/chromeos/contacts/contact.pb.h"
#include "chrome/browser/chromeos/contacts/contact_manager_observer.h"
#include "chrome/browser/chromeos/contacts/contact_test_util.h"
#include "chrome/browser/chromeos/contacts/fake_contact_store.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
namespace contacts {
namespace test {
// ContactManagerObserver implementation that registers itself with a
// ContactManager and counts the number of times that it's been told that
// contacts have been updated.
class TestContactManagerObserver : public ContactManagerObserver {
public:
TestContactManagerObserver(ContactManager* contact_manager,
Profile* profile)
: contact_manager_(contact_manager),
profile_(profile),
num_updates_(0) {
contact_manager_->AddObserver(this, profile_);
}
~TestContactManagerObserver() {
contact_manager_->RemoveObserver(this, profile_);
}
int num_updates() const { return num_updates_; }
void reset_stats() { num_updates_ = 0; }
// ContactManagerObserver overrides:
void OnContactsUpdated(Profile* profile) OVERRIDE {
CHECK(profile == profile_);
num_updates_++;
}
private:
ContactManager* contact_manager_; // not owned
Profile* profile_; // not owned
// Number of times that OnContactsUpdated() has been called.
int num_updates_;
DISALLOW_COPY_AND_ASSIGN(TestContactManagerObserver);
};
class ContactManagerTest : public testing::Test {
public:
ContactManagerTest() : ui_thread_(BrowserThread::UI, &message_loop_) {}
virtual ~ContactManagerTest() {}
protected:
// testing::Test implementation.
virtual void SetUp() OVERRIDE {
profile_manager_.reset(
new TestingProfileManager(
static_cast<TestingBrowserProcess*>(g_browser_process)));
ASSERT_TRUE(profile_manager_->SetUp());
contact_manager_.reset(new ContactManager);
store_factory_ = new FakeContactStoreFactory;
contact_manager_->SetContactStoreForTesting(
scoped_ptr<ContactStoreFactory>(store_factory_).Pass());
contact_manager_->Init();
}
MessageLoopForUI message_loop_;
content::TestBrowserThread ui_thread_;
scoped_ptr<TestingProfileManager> profile_manager_;
scoped_ptr<ContactManager> contact_manager_;
FakeContactStoreFactory* store_factory_; // not owned
private:
DISALLOW_COPY_AND_ASSIGN(ContactManagerTest);
};
TEST_F(ContactManagerTest, NotifyOnUpdate) {
const std::string kProfileName = "test_profile";
TestingProfile* profile =
profile_manager_->CreateTestingProfile(kProfileName);
TestContactManagerObserver observer(contact_manager_.get(), profile);
EXPECT_EQ(0, observer.num_updates());
// ContactManager should notify its observers when it receives notification
// that a ContactStore has been updated.
FakeContactStore* store = store_factory_->GetContactStoreForProfile(profile);
ASSERT_TRUE(store);
store->NotifyObserversAboutContactsUpdate();
EXPECT_EQ(1, observer.num_updates());
store->NotifyObserversAboutContactsUpdate();
EXPECT_EQ(2, observer.num_updates());
profile_manager_->DeleteTestingProfile(kProfileName);
EXPECT_EQ(2, observer.num_updates());
}
TEST_F(ContactManagerTest, GetContacts) {
// Create two contacts and tell the store to return them.
const std::string kProviderId1 = "1";
scoped_ptr<Contact> contact1(new Contact);
InitContact(kProviderId1, "1", false, contact1.get());
const std::string kProviderId2 = "2";
scoped_ptr<Contact> contact2(new Contact);
InitContact(kProviderId2, "2", false, contact2.get());
const std::string kProfileName = "test_profile";
TestingProfile* profile =
profile_manager_->CreateTestingProfile(kProfileName);
FakeContactStore* store = store_factory_->GetContactStoreForProfile(profile);
ASSERT_TRUE(store);
ContactPointers store_contacts;
store_contacts.push_back(contact1.get());
store_contacts.push_back(contact2.get());
store->SetContacts(store_contacts);
store->NotifyObserversAboutContactsUpdate();
// Check that GetAllContacts() returns both contacts.
scoped_ptr<ContactPointers> loaded_contacts =
contact_manager_->GetAllContacts(profile);
EXPECT_EQ(ContactsToString(store_contacts),
ContactsToString(*loaded_contacts));
// Check that we can get individual contacts using GetContactByProviderId().
const Contact* loaded_contact =
contact_manager_->GetContactByProviderId(profile, kProviderId1);
ASSERT_TRUE(loaded_contact);
EXPECT_EQ(ContactToString(*contact1), ContactToString(*loaded_contact));
loaded_contact =
contact_manager_->GetContactByProviderId(profile, kProviderId2);
ASSERT_TRUE(loaded_contact);
EXPECT_EQ(ContactToString(*contact2), ContactToString(*loaded_contact));
// We should get NULL if we pass a nonexistent provider ID.
EXPECT_FALSE(contact_manager_->GetContactByProviderId(profile, "foo"));
profile_manager_->DeleteTestingProfile(kProfileName);
}
TEST_F(ContactManagerTest, ProfileUnsupportedByContactStore) {
// ContactManager shouldn't try to create a ContactStore for an unsuppported
// Profile.
store_factory_->set_permit_store_creation(false);
const std::string kProfileName = "test_profile";
TestingProfile* profile =
profile_manager_->CreateTestingProfile(kProfileName);
EXPECT_FALSE(store_factory_->GetContactStoreForProfile(profile));
profile_manager_->DeleteTestingProfile(kProfileName);
}
} // namespace test
} // namespace contacts
......@@ -78,7 +78,8 @@ FakeContactStoreFactory::~FakeContactStoreFactory() {
FakeContactStore* FakeContactStoreFactory::GetContactStoreForProfile(
Profile* profile) {
CHECK(profile);
return stores_[profile];
ProfileStoreMap::const_iterator it = stores_.find(profile);
return it != stores_.end() ? it->second : NULL;
}
void FakeContactStoreFactory::RemoveStore(FakeContactStore* store) {
......
......@@ -73,7 +73,8 @@ class FakeContactStoreFactory : public ContactStoreFactory {
permit_store_creation_ = permit;
}
// Returns the FakeContactStore previously created for |profile|.
// Returns the FakeContactStore previously created for |profile|, or NULL if
// no store has been created for it.
FakeContactStore* GetContactStoreForProfile(Profile* profile);
// Removes |store| from |stores_| after being called by a FakeContactStore's
......
......@@ -85,8 +85,7 @@ GoogleContactStore::GoogleContactStore(Profile* profile)
}
GoogleContactStore::~GoogleContactStore() {
// This should also be running on the UI thread but we can't check it; the
// message loop is typically already getting torn down at this point.
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
weak_ptr_factory_.InvalidateWeakPtrs();
DestroyDatabase();
}
......
......@@ -461,6 +461,9 @@
'browser/chromeos/chrome_browser_main_chromeos.h',
'browser/chromeos/contacts/contact_database.cc',
'browser/chromeos/contacts/contact_database.h',
'browser/chromeos/contacts/contact_manager.cc',
'browser/chromeos/contacts/contact_manager.h',
'browser/chromeos/contacts/contact_manager_observer.h',
'browser/chromeos/contacts/contact_store.h',
'browser/chromeos/contacts/contact_store_observer.h',
'browser/chromeos/contacts/google_contact_store.cc',
......
......@@ -1082,6 +1082,7 @@
'browser/chromeos/bluetooth/bluetooth_service_record_unittest.cc',
'browser/chromeos/bluetooth/bluetooth_utils_unittest.cc',
'browser/chromeos/contacts/contact_database_unittest.cc',
'browser/chromeos/contacts/contact_manager_unittest.cc',
'browser/chromeos/contacts/contact_test_util.cc',
'browser/chromeos/contacts/contact_test_util.h',
'browser/chromeos/contacts/fake_contact_database.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment