Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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/search/one_google_bar/one_google_bar_service.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/callback.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "chrome/browser/search/one_google_bar/one_google_bar_fetcher.h" | |
| 13 #include "components/signin/core/browser/signin_manager_base.h" | |
| 14 | |
| 15 class OneGoogleBarService::SigninObserver : public SigninManagerBase::Observer { | |
| 16 public: | |
| 17 using SigninStatusChangedCallback = base::Closure; | |
| 18 | |
| 19 SigninObserver(SigninManagerBase* signin_manager, | |
| 20 const SigninStatusChangedCallback& callback) | |
| 21 : signin_manager_(signin_manager), callback_(callback) { | |
| 22 signin_manager_->AddObserver(this); | |
| 23 } | |
| 24 | |
| 25 ~SigninObserver() override { signin_manager_->RemoveObserver(this); } | |
| 26 | |
| 27 private: | |
| 28 // SigninManagerBase::Observer implementation. | |
| 29 void GoogleSigninSucceeded(const std::string& account_id, | |
| 30 const std::string& username, | |
| 31 const std::string& password) override { | |
| 32 callback_.Run(); | |
| 33 } | |
| 34 | |
| 35 void GoogleSignedOut(const std::string& account_id, | |
| 36 const std::string& username) override { | |
| 37 callback_.Run(); | |
| 38 } | |
| 39 | |
| 40 SigninManagerBase* const signin_manager_; | |
| 41 SigninStatusChangedCallback callback_; | |
| 42 }; | |
| 43 | |
| 44 OneGoogleBarService::OneGoogleBarService( | |
| 45 SigninManagerBase* signin_manager, | |
| 46 std::unique_ptr<OneGoogleBarFetcher> fetcher) | |
| 47 : fetcher_(std::move(fetcher)) { | |
| 48 signin_observer_ = base::MakeUnique<SigninObserver>( | |
|
sfiera
2017/04/13 15:18:31
Any reason not to do this in an initializer and ma
Marc Treib
2017/04/13 15:44:02
It can't be const because I call .reset() on it be
| |
| 49 signin_manager, base::Bind(&OneGoogleBarService::SigninStatusChanged, | |
| 50 base::Unretained(this))); | |
| 51 } | |
| 52 | |
| 53 OneGoogleBarService::~OneGoogleBarService() = default; | |
| 54 | |
| 55 void OneGoogleBarService::Shutdown() { | |
| 56 signin_observer_.reset(); | |
| 57 } | |
| 58 | |
| 59 void OneGoogleBarService::Refresh() { | |
| 60 fetcher_->Fetch(base::Bind(&OneGoogleBarService::SetOneGoogleBarData, | |
| 61 base::Unretained(this))); | |
| 62 } | |
| 63 | |
| 64 void OneGoogleBarService::AddObserver(OneGoogleBarServiceObserver* observer) { | |
| 65 observers_.AddObserver(observer); | |
| 66 } | |
| 67 | |
| 68 void OneGoogleBarService::RemoveObserver( | |
| 69 OneGoogleBarServiceObserver* observer) { | |
| 70 observers_.RemoveObserver(observer); | |
| 71 } | |
| 72 | |
| 73 void OneGoogleBarService::SigninStatusChanged() { | |
| 74 SetOneGoogleBarData(base::nullopt); | |
| 75 } | |
| 76 | |
| 77 void OneGoogleBarService::SetOneGoogleBarData( | |
| 78 const base::Optional<OneGoogleBarData>& data) { | |
| 79 if (one_google_bar_data_ == data) { | |
| 80 return; | |
| 81 } | |
| 82 | |
| 83 one_google_bar_data_ = data; | |
| 84 for (auto& observer : observers_) { | |
| 85 observer.OnOneGoogleBarDataChanged(one_google_bar_data_); | |
| 86 } | |
| 87 } | |
| OLD | NEW |