Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(305)

Side by Side Diff: chrome/browser/android/search_geolocation/search_geolocation_service.h

Issue 2637953003: Refactor SearchGeolocationService to make it testable (Closed)
Patch Set: Refactor for test Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 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 #ifndef CHROME_BROWSER_ANDROID_SEARCH_GEOLOCATION_SEARCH_GEOLOCATION_SERVICE_H_ 5 #ifndef CHROME_BROWSER_ANDROID_SEARCH_GEOLOCATION_SEARCH_GEOLOCATION_SERVICE_H_
6 #define CHROME_BROWSER_ANDROID_SEARCH_GEOLOCATION_SEARCH_GEOLOCATION_SERVICE_H_ 6 #define CHROME_BROWSER_ANDROID_SEARCH_GEOLOCATION_SEARCH_GEOLOCATION_SERVICE_H_
7 7
8 #include "base/callback.h"
dominickn 2017/01/18 00:11:18 Nit: you can just use callback_forward.h here I th
raymes 2017/01/18 04:07:24 Done.
8 #include "base/memory/singleton.h" 9 #include "base/memory/singleton.h"
9 #include "components/content_settings/core/common/content_settings.h" 10 #include "components/content_settings/core/common/content_settings.h"
10 #include "components/keyed_service/content/browser_context_keyed_service_factory .h" 11 #include "components/keyed_service/content/browser_context_keyed_service_factory .h"
11 #include "components/keyed_service/core/keyed_service.h" 12 #include "components/keyed_service/core/keyed_service.h"
12 #include "components/search_engines/template_url_service_observer.h"
13 #include "url/origin.h" 13 #include "url/origin.h"
14 14
15 namespace content{ 15 namespace content{
16 class BrowserContext; 16 class BrowserContext;
17 } 17 }
18 18
19 namespace user_prefs { 19 namespace user_prefs {
20 class PrefRegistrySyncable; 20 class PrefRegistrySyncable;
21 } 21 }
22 22
23 class HostContentSettingsMap; 23 class HostContentSettingsMap;
24 class PrefService; 24 class PrefService;
25 class Profile; 25 class Profile;
26 class TemplateURLService;
27 26
28 // Helper class to manage the DSE Geolocation setting. It keeps the setting 27 // Helper class to manage the DSE Geolocation setting. It keeps the setting
29 // valid by watching change to the CCTLD and DSE, and also provides logic for 28 // valid by watching change to the CCTLD and DSE, and also provides logic for
30 // whether the setting should be used and it's current value. 29 // whether the setting should be used and it's current value.
31 // Glossary: 30 // Glossary:
32 // DSE: Default Search Engine 31 // DSE: Default Search Engine
33 // CCTLD: Country Code Top Level Domain (e.g. google.com.au) 32 // CCTLD: Country Code Top Level Domain (e.g. google.com.au)
34 class SearchGeolocationService 33 class SearchGeolocationService : public KeyedService {
35 : public TemplateURLServiceObserver,
36 public KeyedService {
37 public: 34 public:
35 // Delegate for search engine related functionality. Can be overridden for
36 // testing.
37 class SearchEngineDelegate {
38 public:
39 // Returns true if the current DSE is Google (Google is the only search
40 // engine to currently support DSE geolocation).
41 virtual bool IsDSEGoogle() = 0;
42
43 // Returns the origin of the current CCTLD of the default search engine. If
dominickn 2017/01/18 00:11:18 "Returns the origin of the current Google CCTLD if
raymes 2017/01/18 04:07:24 Done.
44 // the default search engine is not Google, will return a unique Origin.
45 virtual url::Origin GetGoogleDSECCTLD() = 0;
46
47 // Set a callback that will be called if the DSE or CCTLD changes for any
48 // reason.
49 virtual void SetDSEChangedCallback(const base::Closure& callback) = 0;
50 };
51
38 // Factory implementation will not create a service in incognito. 52 // Factory implementation will not create a service in incognito.
39 class Factory : public BrowserContextKeyedServiceFactory { 53 class Factory : public BrowserContextKeyedServiceFactory {
40 public: 54 public:
41 static SearchGeolocationService* GetForBrowserContext( 55 static SearchGeolocationService* GetForBrowserContext(
42 content::BrowserContext* context); 56 content::BrowserContext* context);
43 57
44 static Factory* GetInstance(); 58 static Factory* GetInstance();
45 private: 59 private:
46 friend struct base::DefaultSingletonTraits<Factory>; 60 friend struct base::DefaultSingletonTraits<Factory>;
47 61
(...skipping 22 matching lines...) Expand all
70 void SetDSEGeolocationSetting(bool setting); 84 void SetDSEGeolocationSetting(bool setting);
71 85
72 // KeyedService: 86 // KeyedService:
73 void Shutdown() override; 87 void Shutdown() override;
74 88
75 private: 89 private:
76 struct PrefValue; 90 struct PrefValue;
77 91
78 ~SearchGeolocationService() override; 92 ~SearchGeolocationService() override;
79 93
80 // TemplateURLServiceObserver
81 // When the DSE CCTLD changes (either by changing their DSE or by changing 94 // When the DSE CCTLD changes (either by changing their DSE or by changing
82 // their CCTLD, and their DSE supports geolocation: 95 // their CCTLD, and their DSE supports geolocation:
83 // * If the DSE CCTLD origin permission is BLOCK, but the DSE geolocation 96 // * If the DSE CCTLD origin permission is BLOCK, but the DSE geolocation
84 // setting is on, change the DSE geolocation setting to off 97 // setting is on, change the DSE geolocation setting to off
85 // * If the DSE CCTLD origin permission is ALLOW, but the DSE geolocation 98 // * If the DSE CCTLD origin permission is ALLOW, but the DSE geolocation
86 // setting is off, reset the DSE CCTLD origin permission to ASK. 99 // setting is off, reset the DSE CCTLD origin permission to ASK.
87 // Also, if the previous DSE did not support geolocation, and the new one 100 // Also, if the previous DSE did not support geolocation, and the new one
88 // does, and the geolocation setting is on, reset whether the DSE geolocation 101 // does, and the geolocation setting is on, reset whether the DSE geolocation
89 // disclosure has been shown. 102 // disclosure has been shown.
90 void OnTemplateURLServiceChanged() override; 103 void OnDSEChanged();
91 104
92 // Initialize the DSE geolocation setting if it hasn't already been 105 // Initialize the DSE geolocation setting if it hasn't already been
93 // initialized. Also, if it hasn't been initialized, reset whether the DSE 106 // initialized. Also, if it hasn't been initialized, reset whether the DSE
94 // geolocation disclosure has been shown to ensure user who may have seen it 107 // geolocation disclosure has been shown to ensure user who may have seen it
95 // on earlier versions (due to Finch experiments) see it again. 108 // on earlier versions (due to Finch experiments) see it again.
96 void InitializeDSEGeolocationSettingIfNeeded(); 109 void InitializeDSEGeolocationSettingIfNeeded();
97 110
98 // Check that the DSE geolocation setting is valid with respect to the content 111 // Check that the DSE geolocation setting is valid with respect to the content
99 // setting. The rules of vaidity are: 112 // setting. The rules of vaidity are:
100 // * If the content setting is BLOCK, the DSE geolocation setting must 113 // * If the content setting is BLOCK, the DSE geolocation setting must
101 // be false. 114 // be false.
102 // * If the content setting is ALLOW, the DSE geolocation setting must be 115 // * If the content setting is ALLOW, the DSE geolocation setting must be
103 // true. 116 // true.
104 // * If the content setting is ASK, the DSE geolocation setting can be true or 117 // * If the content setting is ASK, the DSE geolocation setting can be true or
105 // false. 118 // false.
106 // One way the setting could become invalid is if the feature enabling the 119 // One way the setting could become invalid is if the feature enabling the
107 // setting was disabled (via Finch or flags), content settings were changed, 120 // setting was disabled (via Finch or flags), content settings were changed,
108 // and the feature enabled again. Or the enterprise policy settings could have 121 // and the feature enabled again. Or the enterprise policy settings could have
109 // been updated in a way that makes the setting invalid. 122 // been updated in a way that makes the setting invalid.
110 void EnsureDSEGeolocationSettingIsValid(); 123 void EnsureDSEGeolocationSettingIsValid();
111 124
112 // Returns true if the current DSE is Google (Google is the only search engine
113 // to currently support DSE geolocation).
114 bool IsGoogleSearchEngine();
115
116 // Returns the origin of the current CCTLD of the default search engine. If
117 // the default search engine is not Google, will return a unique Origin.
118 url::Origin GetDSECCTLD();
119
120 PrefValue GetDSEGeolocationPref(); 125 PrefValue GetDSEGeolocationPref();
121 void SetDSEGeolocationPref(const PrefValue& pref); 126 void SetDSEGeolocationPref(const PrefValue& pref);
122 127
123 // Retrieve the geolocation content setting for the current DSE CCTLD. 128 // Retrieve the geolocation content setting for the current DSE CCTLD.
124 ContentSetting GetCurrentContentSetting(); 129 ContentSetting GetCurrentContentSetting();
125 130
126 // Reset the geolocation content setting for the current DSE CCTLD back to the 131 // Reset the geolocation content setting for the current DSE CCTLD back to the
127 // default. 132 // default.
128 void ResetContentSetting(); 133 void ResetContentSetting();
129 134
130 // Returns whether the user can change the geolocation content setting for the 135 // Returns whether the user can change the geolocation content setting for the
131 // current DSE CCTLD. 136 // current DSE CCTLD.
132 bool IsContentSettingUserSettable(); 137 bool IsContentSettingUserSettable();
133 138
134 // Whether the feature/experiment setup is enabling the consistent search 139 // Whether the feature/experiment setup is enabling the consistent search
135 // geolocation system. 140 // geolocation system.
136 bool UseConsistentSearchGeolocation(); 141 bool UseConsistentSearchGeolocation();
137 142
143 void SetSearchEngineDelegateForTest(
144 std::unique_ptr<SearchEngineDelegate> delegate);
145
138 Profile* profile_; 146 Profile* profile_;
139 PrefService* pref_service_; 147 PrefService* pref_service_;
140 HostContentSettingsMap* host_content_settings_map_; 148 HostContentSettingsMap* host_content_settings_map_;
141 TemplateURLService* template_url_service_; 149 std::unique_ptr<SearchEngineDelegate> delegate_;
142 }; 150 };
143 151
144 #endif // CHROME_BROWSER_ANDROID_SEARCH_GEOLOCATION_SEARCH_GEOLOCATION_SERVICE_ H_ 152 #endif // CHROME_BROWSER_ANDROID_SEARCH_GEOLOCATION_SEARCH_GEOLOCATION_SERVICE_ H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698