OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/profiles/avatar_menu_model.h" |
| 6 |
| 7 #include "base/stl_util.h" |
| 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/profiles/avatar_menu_model_observer.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/profiles/profile_info_cache.h" |
| 12 #include "chrome/browser/profiles/profile_manager.h" |
| 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_list.h" |
| 15 #include "chrome/browser/ui/browser_window.h" |
| 16 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "chrome/common/url_constants.h" |
| 18 #include "content/common/notification_service.h" |
| 19 #include "ui/gfx/image/image.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 class ProfileSwitchObserver : public ProfileManagerObserver { |
| 24 public: |
| 25 virtual void OnProfileCreated(Profile* profile, Status status) OVERRIDE { |
| 26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 27 |
| 28 if (status == STATUS_INITIALIZED) { |
| 29 DCHECK(profile); |
| 30 Browser* browser = BrowserList::FindTabbedBrowser(profile, false); |
| 31 if (browser) |
| 32 browser->window()->Activate(); |
| 33 else |
| 34 Browser::NewWindowWithProfile(profile); |
| 35 } |
| 36 } |
| 37 |
| 38 virtual bool DeleteAfter() OVERRIDE { return true; } |
| 39 }; |
| 40 |
| 41 } // namespace |
| 42 |
| 43 AvatarMenuModel::AvatarMenuModel(ProfileInfoInterface* profile_cache, |
| 44 AvatarMenuModelObserver* observer, |
| 45 Browser* browser) |
| 46 : profile_info_(profile_cache), |
| 47 observer_(observer), |
| 48 browser_(browser) { |
| 49 DCHECK(profile_info_); |
| 50 DCHECK(observer_); |
| 51 // Don't DCHECK(browser_) so that unit tests can reuse this ctor. |
| 52 |
| 53 // Register this as an observer of the info cache. |
| 54 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 55 NotificationService::AllSources()); |
| 56 |
| 57 // Build the initial menu. |
| 58 RebuildMenu(); |
| 59 } |
| 60 |
| 61 AvatarMenuModel::~AvatarMenuModel() { |
| 62 ClearMenu(); |
| 63 } |
| 64 |
| 65 AvatarMenuModel::Item::Item(size_t model_index, const gfx::Image& icon) |
| 66 : icon(icon), |
| 67 model_index(model_index) { |
| 68 } |
| 69 |
| 70 AvatarMenuModel::Item::~Item() { |
| 71 } |
| 72 |
| 73 void AvatarMenuModel::SwichToProfile(size_t index) { |
| 74 const Item& item = GetItemAt(index); |
| 75 FilePath path = profile_info_->GetPathOfProfileAtIndex(item.model_index); |
| 76 |
| 77 // This will be deleted by the manager after the profile is ready. |
| 78 ProfileSwitchObserver* observer = new ProfileSwitchObserver(); |
| 79 g_browser_process->profile_manager()->CreateProfileAsync( |
| 80 path, observer); |
| 81 } |
| 82 |
| 83 void AvatarMenuModel::EditProfile(size_t index) { |
| 84 DCHECK(browser_); |
| 85 browser_->ShowOptionsTab(chrome::kPersonalOptionsSubPage); |
| 86 } |
| 87 |
| 88 void AvatarMenuModel::AddNewProfile() { |
| 89 ProfileManager::CreateMultiProfileAsync(); |
| 90 } |
| 91 |
| 92 size_t AvatarMenuModel::GetNumberOfItems() { |
| 93 return items_.size(); |
| 94 } |
| 95 |
| 96 const AvatarMenuModel::Item& AvatarMenuModel::GetItemAt(size_t index) { |
| 97 DCHECK_LT(index, items_.size()); |
| 98 return *items_[index]; |
| 99 } |
| 100 |
| 101 void AvatarMenuModel::Observe(int type, |
| 102 const NotificationSource& source, |
| 103 const NotificationDetails& details) { |
| 104 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, type); |
| 105 RebuildMenu(); |
| 106 } |
| 107 |
| 108 void AvatarMenuModel::RebuildMenu() { |
| 109 ClearMenu(); |
| 110 |
| 111 const size_t count = profile_info_->GetNumberOfProfiles(); |
| 112 for (size_t i = 0; i < count; ++i) { |
| 113 Item* item = new Item(i, profile_info_->GetAvatarIconOfProfileAtIndex(i)); |
| 114 item->name = profile_info_->GetNameOfProfileAtIndex(i); |
| 115 if (browser_) { |
| 116 FilePath path = profile_info_->GetPathOfProfileAtIndex(i); |
| 117 item->active = browser_->profile()->GetPath() == path; |
| 118 } |
| 119 items_.push_back(item); |
| 120 } |
| 121 |
| 122 observer_->OnAvatarMenuModelChanged(); |
| 123 } |
| 124 |
| 125 void AvatarMenuModel::ClearMenu() { |
| 126 STLDeleteContainerPointers(items_.begin(), items_.end()); |
| 127 items_.clear(); |
| 128 } |
OLD | NEW |