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 #ifndef CHROME_BROWSER_SEARCH_ONE_GOOGLE_BAR_ONE_GOOGLE_BAR_SERVICE_H_
6 #define CHROME_BROWSER_SEARCH_ONE_GOOGLE_BAR_ONE_GOOGLE_BAR_SERVICE_H_
7 
8 #include <memory>
9 
10 #include "base/observer_list.h"
11 #include "base/optional.h"
12 #include "chrome/browser/search/one_google_bar/one_google_bar_data.h"
13 #include "chrome/browser/search/one_google_bar/one_google_bar_loader.h"
14 #include "chrome/browser/search/one_google_bar/one_google_bar_service_observer.h"
15 #include "components/keyed_service/core/keyed_service.h"
16 
17 namespace signin {
18 class IdentityManager;
19 }  // namespace signin
20 
21 // A service that downloads, caches, and hands out OneGoogleBarData. It never
22 // initiates a download automatically, only when Refresh is called. When the
23 // user signs in or out, the cached value is cleared.
24 class OneGoogleBarService : public KeyedService {
25  public:
26   OneGoogleBarService(signin::IdentityManager* identity_manager,
27                       std::unique_ptr<OneGoogleBarLoader> loader);
28   ~OneGoogleBarService() override;
29 
30   // KeyedService implementation.
31   void Shutdown() override;
32 
33   // Returns the currently cached OneGoogleBarData, if any.
one_google_bar_data()34   const base::Optional<OneGoogleBarData>& one_google_bar_data() const {
35     return one_google_bar_data_;
36   }
37 
38   // Requests an asynchronous refresh from the network. After the update
39   // completes, OnOneGoogleBarDataUpdated will be called on the observers.
40   void Refresh();
41 
42   // Add/remove observers. All observers must unregister themselves before the
43   // OneGoogleBarService is destroyed.
44   void AddObserver(OneGoogleBarServiceObserver* observer);
45   void RemoveObserver(OneGoogleBarServiceObserver* observer);
46 
loader_for_testing()47   OneGoogleBarLoader* loader_for_testing() { return loader_.get(); }
48 
language_code()49   std::string language_code() { return language_code_; }
50 
51   // Used for testing.
52   void SetLanguageCodeForTesting(const std::string& language_code);
53 
54   // Sets ogdeb query parameter in loader.
55   bool SetAdditionalQueryParams(const std::string& value);
56 
57  private:
58   class SigninObserver;
59 
60   void SigninStatusChanged();
61 
62   void OneGoogleBarDataLoaded(OneGoogleBarLoader::Status status,
63                               const base::Optional<OneGoogleBarData>& data);
64 
65   void NotifyObservers();
66 
67   std::unique_ptr<OneGoogleBarLoader> loader_;
68 
69   std::unique_ptr<SigninObserver> signin_observer_;
70 
71   base::ObserverList<OneGoogleBarServiceObserver, true>::Unchecked observers_;
72 
73   base::Optional<OneGoogleBarData> one_google_bar_data_;
74 
75   std::string language_code_;
76 };
77 
78 #endif  // CHROME_BROWSER_SEARCH_ONE_GOOGLE_BAR_ONE_GOOGLE_BAR_SERVICE_H_
79