OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/identity/account_tracker.h" | 5 #include "chrome/browser/extensions/api/identity/account_tracker.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 } | 46 } |
47 | 47 |
48 void AccountTracker::AddObserver(Observer* observer) { | 48 void AccountTracker::AddObserver(Observer* observer) { |
49 observer_list_.AddObserver(observer); | 49 observer_list_.AddObserver(observer); |
50 } | 50 } |
51 | 51 |
52 void AccountTracker::RemoveObserver(Observer* observer) { | 52 void AccountTracker::RemoveObserver(Observer* observer) { |
53 observer_list_.RemoveObserver(observer); | 53 observer_list_.RemoveObserver(observer); |
54 } | 54 } |
55 | 55 |
| 56 std::vector<AccountIds> AccountTracker::GetAccounts() const { |
| 57 const std::string primary_account_id = signin_manager_account_id(); |
| 58 std::vector<AccountIds> accounts; |
| 59 |
| 60 for (std::map<std::string, AccountState>::const_iterator it = |
| 61 accounts_.begin(); |
| 62 it != accounts_.end(); |
| 63 ++it) { |
| 64 const AccountState& state = it->second; |
| 65 bool is_visible = state.is_signed_in && !state.ids.gaia.empty(); |
| 66 |
| 67 if (it->first == primary_account_id) { |
| 68 if (is_visible) |
| 69 accounts.insert(accounts.begin(), state.ids); |
| 70 else |
| 71 return std::vector<AccountIds>(); |
| 72 |
| 73 } else if (is_visible) { |
| 74 accounts.push_back(state.ids); |
| 75 } |
| 76 } |
| 77 return accounts; |
| 78 } |
| 79 |
56 void AccountTracker::OnRefreshTokenAvailable(const std::string& account_id) { | 80 void AccountTracker::OnRefreshTokenAvailable(const std::string& account_id) { |
57 // Ignore refresh tokens if there is no primary account ID at all. | 81 // Ignore refresh tokens if there is no primary account ID at all. |
58 if (signin_manager_account_id().empty()) | 82 if (signin_manager_account_id().empty()) |
59 return; | 83 return; |
60 | 84 |
61 DVLOG(1) << "AVAILABLE " << account_id; | 85 DVLOG(1) << "AVAILABLE " << account_id; |
62 ClearAuthError(account_id); | 86 ClearAuthError(account_id); |
63 UpdateSignInState(account_id, true); | 87 UpdateSignInState(account_id, true); |
64 } | 88 } |
65 | 89 |
(...skipping 16 matching lines...) Expand all Loading... |
82 | 106 |
83 void AccountTracker::GoogleSignedOut(const std::string& username) { | 107 void AccountTracker::GoogleSignedOut(const std::string& username) { |
84 if (username == signin_manager_account_id() || | 108 if (username == signin_manager_account_id() || |
85 signin_manager_account_id().empty()) { | 109 signin_manager_account_id().empty()) { |
86 StopTrackingAllAccounts(); | 110 StopTrackingAllAccounts(); |
87 } else { | 111 } else { |
88 StopTrackingAccount(username); | 112 StopTrackingAccount(username); |
89 } | 113 } |
90 } | 114 } |
91 | 115 |
92 const std::string AccountTracker::signin_manager_account_id() { | 116 void AccountTracker::SetAccountStateForTest(AccountIds ids, bool is_signed_in) { |
| 117 accounts_[ids.account_key].ids = ids; |
| 118 accounts_[ids.account_key].is_signed_in = is_signed_in; |
| 119 |
| 120 DVLOG(1) << "SetAccountStateForTest " << ids.account_key << ":" |
| 121 << is_signed_in; |
| 122 |
| 123 if (VLOG_IS_ON(1)) { |
| 124 for (std::map<std::string, AccountState>::const_iterator it = |
| 125 accounts_.begin(); |
| 126 it != accounts_.end(); |
| 127 ++it) { |
| 128 DVLOG(1) << it->first << ":" << it->second.is_signed_in; |
| 129 } |
| 130 } |
| 131 } |
| 132 |
| 133 const std::string AccountTracker::signin_manager_account_id() const { |
93 return SigninManagerFactory::GetForProfile(profile_) | 134 return SigninManagerFactory::GetForProfile(profile_) |
94 ->GetAuthenticatedAccountId(); | 135 ->GetAuthenticatedAccountId(); |
95 } | 136 } |
96 | 137 |
97 void AccountTracker::NotifyAccountAdded(const AccountState& account) { | 138 void AccountTracker::NotifyAccountAdded(const AccountState& account) { |
98 DCHECK(!account.ids.gaia.empty()); | 139 DCHECK(!account.ids.gaia.empty()); |
99 FOR_EACH_OBSERVER( | 140 FOR_EACH_OBSERVER( |
100 Observer, observer_list_, OnAccountAdded(account.ids)); | 141 Observer, observer_list_, OnAccountAdded(account.ids)); |
101 } | 142 } |
102 | 143 |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 LOG(ERROR) << "OnOAuthError"; | 308 LOG(ERROR) << "OnOAuthError"; |
268 tracker_->OnUserInfoFetchFailure(this); | 309 tracker_->OnUserInfoFetchFailure(this); |
269 } | 310 } |
270 | 311 |
271 void AccountIdFetcher::OnNetworkError(int response_code) { | 312 void AccountIdFetcher::OnNetworkError(int response_code) { |
272 LOG(ERROR) << "OnNetworkError " << response_code; | 313 LOG(ERROR) << "OnNetworkError " << response_code; |
273 tracker_->OnUserInfoFetchFailure(this); | 314 tracker_->OnUserInfoFetchFailure(this); |
274 } | 315 } |
275 | 316 |
276 } // namespace extensions | 317 } // namespace extensions |
OLD | NEW |