Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 COMPONENTS_SAFE_BROWSING_PASSWORD_PROTECTION_PASSWORD_PROTECTION_SERVICE _H_ | 5 #ifndef COMPONENTS_SAFE_BROWSING_PASSWORD_PROTECTION_PASSWORD_PROTECTION_SERVICE _H_ |
| 6 #define COMPONENTS_SAFE_BROWSING_PASSWORD_PROTECTION_PASSWORD_PROTECTION_SERVICE _H_ | 6 #define COMPONENTS_SAFE_BROWSING_PASSWORD_PROTECTION_PASSWORD_PROTECTION_SERVICE _H_ |
| 7 | 7 |
| 8 #include <unordered_set> | 8 #include <unordered_set> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/scoped_observer.h" | |
| 15 #include "base/values.h" | 16 #include "base/values.h" |
| 16 #include "components/content_settings/core/browser/host_content_settings_map.h" | |
| 17 #include "components/history/core/browser/history_service_observer.h" | 17 #include "components/history/core/browser/history_service_observer.h" |
| 18 #include "components/safe_browsing/csd.pb.h" | 18 #include "components/safe_browsing/csd.pb.h" |
| 19 #include "net/url_request/url_request_context_getter.h" | 19 #include "net/url_request/url_request_context_getter.h" |
| 20 | 20 |
| 21 namespace history { | 21 namespace history { |
| 22 class HistoryService; | 22 class HistoryService; |
| 23 } | 23 } |
| 24 | 24 |
| 25 class GURL; | 25 class GURL; |
| 26 class HostContentSettingsMap; | |
| 26 | 27 |
| 27 namespace safe_browsing { | 28 namespace safe_browsing { |
| 28 | 29 |
| 29 class SafeBrowsingDatabaseManager; | 30 class SafeBrowsingDatabaseManager; |
| 30 class PasswordProtectionRequest; | 31 class PasswordProtectionRequest; |
| 31 | 32 |
| 33 // Manage password protection pings and verdicts. There is one instance of this | |
| 34 // class per profile. Therefore, one every PasswordProtectionService instance | |
|
Nathan Parker
2017/03/30 21:38:12
nit: confusing wording around "one every"
Jialiu Lin
2017/03/30 23:23:10
Done.
| |
| 35 // associates with unique HistoryService instance and HostContentSettingsMap | |
| 36 // instance. | |
| 32 class PasswordProtectionService : history::HistoryServiceObserver { | 37 class PasswordProtectionService : history::HistoryServiceObserver { |
| 33 public: | 38 public: |
| 34 using CheckCsdWhitelistCallback = base::Callback<void(bool)>; | 39 using CheckCsdWhitelistCallback = base::Callback<void(bool)>; |
| 35 | 40 |
| 36 PasswordProtectionService( | 41 PasswordProtectionService( |
| 37 const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager, | 42 const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager, |
| 38 scoped_refptr<net::URLRequestContextGetter> request_context_getter); | 43 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 44 history::HistoryService* history_service, | |
| 45 HostContentSettingsMap* host_content_settings_map); | |
| 39 | 46 |
| 40 ~PasswordProtectionService() override; | 47 ~PasswordProtectionService() override; |
| 41 | 48 |
| 42 // Checks if |url| matches CSD whitelist and record UMA metric accordingly. | 49 // Checks if |url| matches CSD whitelist and record UMA metric accordingly. |
| 43 // Currently called by PasswordReuseDetectionManager on UI thread. | 50 // Currently called by PasswordReuseDetectionManager on UI thread. |
| 44 void RecordPasswordReuse(const GURL& url); | 51 void RecordPasswordReuse(const GURL& url); |
| 45 | 52 |
| 46 void CheckCsdWhitelistOnIOThread(const GURL& url, | 53 void CheckCsdWhitelistOnIOThread(const GURL& url, |
| 47 const CheckCsdWhitelistCallback& callback); | 54 const CheckCsdWhitelistCallback& callback); |
| 48 | 55 |
| 49 base::WeakPtr<PasswordProtectionService> GetWeakPtr() { | 56 base::WeakPtr<PasswordProtectionService> GetWeakPtr() { |
| 50 return weak_factory_.GetWeakPtr(); | 57 return weak_factory_.GetWeakPtr(); |
| 51 } | 58 } |
| 52 | 59 |
| 53 // Looks up |settings| to find the cached verdict response. If verdict is not | 60 // Looks up |settings| to find the cached verdict response. If verdict is not |
| 54 // available or is expired, return VERDICT_TYPE_UNSPECIFIED. Can be called on | 61 // available or is expired, return VERDICT_TYPE_UNSPECIFIED. Can be called on |
| 55 // any thread. | 62 // any thread. |
| 56 LoginReputationClientResponse::VerdictType GetCachedVerdict( | 63 LoginReputationClientResponse::VerdictType GetCachedVerdict( |
| 57 const HostContentSettingsMap* settings, | |
| 58 const GURL& url, | 64 const GURL& url, |
| 59 LoginReputationClientResponse* out_response); | 65 LoginReputationClientResponse* out_response); |
| 60 | 66 |
| 61 // Stores |verdict| in |settings| based on |url|, |verdict| and | 67 // Stores |verdict| in |settings| based on |url|, |verdict| and |
| 62 // |receive_time|. | 68 // |receive_time|. |
| 63 void CacheVerdict(const GURL& url, | 69 void CacheVerdict(const GURL& url, |
| 64 LoginReputationClientResponse* verdict, | 70 LoginReputationClientResponse* verdict, |
| 65 const base::Time& receive_time, | 71 const base::Time& receive_time); |
| 66 HostContentSettingsMap* settings); | |
| 67 | 72 |
| 68 // Creates an instance of PasswordProtectionRequest and call Start() on that | 73 // Creates an instance of PasswordProtectionRequest and call Start() on that |
| 69 // instance. This function also insert this request object in |requests_| for | 74 // instance. This function also insert this request object in |requests_| for |
| 70 // record keeping. | 75 // record keeping. |
| 71 void StartRequest(const GURL& main_frame_url, | 76 void StartRequest(const GURL& main_frame_url, |
| 72 LoginReputationClientRequest::TriggerType type, | 77 LoginReputationClientRequest::TriggerType type); |
| 73 bool is_extended_reporting, | |
| 74 bool is_incognito); | |
| 75 | 78 |
| 76 // Called by a PasswordProtectionRequest instance when it finishes to remove | 79 // Called by a PasswordProtectionRequest instance when it finishes to remove |
| 77 // itself from |requests_|. | 80 // itself from |requests_|. |
| 78 virtual void RequestFinished( | 81 virtual void RequestFinished( |
| 79 PasswordProtectionRequest* request, | 82 PasswordProtectionRequest* request, |
| 80 std::unique_ptr<LoginReputationClientResponse> response); | 83 std::unique_ptr<LoginReputationClientResponse> response); |
| 81 | 84 |
| 82 // Cancels all requests in |requests_|, empties it, and releases references to | 85 // Cancels all requests in |requests_|, empties it, and releases references to |
| 83 // the requests. | 86 // the requests. |
| 84 void CancelPendingRequests(); | 87 void CancelPendingRequests(); |
| 85 | 88 |
| 86 // Gets the total number of verdict (no matter expired or not) we cached for | 89 // Gets the total number of verdict (no matter expired or not) we cached for |
| 87 // current active profile. | 90 // current active profile. |
| 88 virtual size_t GetStoredVerdictCount(); | 91 virtual int GetStoredVerdictCount(); |
| 89 | 92 |
| 90 scoped_refptr<net::URLRequestContextGetter> request_context_getter() { | 93 scoped_refptr<net::URLRequestContextGetter> request_context_getter() { |
| 91 return request_context_getter_; | 94 return request_context_getter_; |
| 92 } | 95 } |
| 93 | 96 |
| 94 // Returns the URL where PasswordProtectionRequest instances send requests. | 97 // Returns the URL where PasswordProtectionRequest instances send requests. |
| 95 static GURL GetPasswordProtectionRequestUrl(); | 98 static GURL GetPasswordProtectionRequestUrl(); |
| 96 | 99 |
| 97 // Gets the request timeout in milliseconds. | 100 // Gets the request timeout in milliseconds. |
| 98 static int GetRequestTimeoutInMS(); | 101 static int GetRequestTimeoutInMS(); |
| 99 | 102 |
| 103 // Obtains referrer chain of |event_url| and |event_tab_id| and add this | |
|
Nathan Parker
2017/03/30 21:38:12
nit: s/add/adds
Jialiu Lin
2017/03/30 23:23:09
Done.
| |
| 104 // info into |frame|. | |
| 105 virtual void FillReferrerChain( | |
| 106 const GURL& event_url, | |
| 107 int event_tab_id, // -1 if tab id is not available. | |
| 108 LoginReputationClientRequest::Frame* frame) = 0; | |
| 109 | |
| 110 virtual bool IsExtendedReporting() = 0; | |
| 111 virtual bool IsIncognito() = 0; | |
| 112 | |
| 113 // If we can send ping to Safe Browsing backend. | |
| 114 virtual bool IsPingingEnabled() = 0; | |
| 115 | |
| 100 protected: | 116 protected: |
| 101 friend class PasswordProtectionRequest; | 117 friend class PasswordProtectionRequest; |
| 102 | 118 |
| 103 // Increases "PasswordManager.PasswordReuse.MainFrameMatchCsdWhitelist" UMA | 119 // Increases "PasswordManager.PasswordReuse.MainFrameMatchCsdWhitelist" UMA |
| 104 // metric based on input. | 120 // metric based on input. |
| 105 void OnMatchCsdWhiteListResult(bool match_whitelist); | 121 void OnMatchCsdWhiteListResult(bool match_whitelist); |
| 106 | 122 |
| 107 // Gets HostContentSettingMap for current active profile; | |
| 108 // TODO(jialiul): make this a pure virtual function when we have a derived | |
| 109 // class ready in chrome/browser/safe_browsing directory. | |
| 110 virtual HostContentSettingsMap* GetSettingMapForActiveProfile(); | |
| 111 | |
| 112 private: | 123 private: |
| 113 friend class PasswordProtectionServiceTest; | 124 friend class PasswordProtectionServiceTest; |
| 114 FRIEND_TEST_ALL_PREFIXES(PasswordProtectionServiceTest, | 125 FRIEND_TEST_ALL_PREFIXES(PasswordProtectionServiceTest, |
| 115 TestParseInvalidVerdictEntry); | 126 TestParseInvalidVerdictEntry); |
| 116 FRIEND_TEST_ALL_PREFIXES(PasswordProtectionServiceTest, | 127 FRIEND_TEST_ALL_PREFIXES(PasswordProtectionServiceTest, |
| 117 TestParseValidVerdictEntry); | 128 TestParseValidVerdictEntry); |
| 118 FRIEND_TEST_ALL_PREFIXES(PasswordProtectionServiceTest, | 129 FRIEND_TEST_ALL_PREFIXES(PasswordProtectionServiceTest, |
| 119 TestPathVariantsMatchCacheExpression); | 130 TestPathVariantsMatchCacheExpression); |
| 120 FRIEND_TEST_ALL_PREFIXES(PasswordProtectionServiceTest, | 131 FRIEND_TEST_ALL_PREFIXES(PasswordProtectionServiceTest, |
| 121 TestPathMatchCacheExpressionExactly); | 132 TestPathMatchCacheExpressionExactly); |
| 122 FRIEND_TEST_ALL_PREFIXES(PasswordProtectionServiceTest, | 133 FRIEND_TEST_ALL_PREFIXES(PasswordProtectionServiceTest, |
| 123 TestCleanUpCachedVerdicts); | 134 TestCleanUpCachedVerdicts); |
| 124 | 135 |
| 125 // Overridden from history::HistoryServiceObserver. | 136 // Overridden from history::HistoryServiceObserver. |
| 126 void OnURLsDeleted(history::HistoryService* history_service, | 137 void OnURLsDeleted(history::HistoryService* history_service, |
| 127 bool all_history, | 138 bool all_history, |
| 128 bool expired, | 139 bool expired, |
| 129 const history::URLRows& deleted_rows, | 140 const history::URLRows& deleted_rows, |
| 130 const std::set<GURL>& favicon_urls) override; | 141 const std::set<GURL>& favicon_urls) override; |
| 131 | 142 |
| 143 void HistoryServiceBeingDeleted( | |
| 144 history::HistoryService* history_service) override; | |
| 145 | |
| 132 // Posted to UI thread by OnURLsDeleted(..). This function cleans up password | 146 // Posted to UI thread by OnURLsDeleted(..). This function cleans up password |
| 133 // protection content settings related to deleted URLs. | 147 // protection content settings related to deleted URLs. |
| 134 void RemoveContentSettingsOnURLsDeleted(bool all_history, | 148 void RemoveContentSettingsOnURLsDeleted(bool all_history, |
| 135 const history::URLRows& deleted_rows, | 149 const history::URLRows& deleted_rows); |
| 136 HostContentSettingsMap* setting_map); | |
| 137 | 150 |
| 138 static bool ParseVerdictEntry(base::DictionaryValue* verdict_entry, | 151 static bool ParseVerdictEntry(base::DictionaryValue* verdict_entry, |
| 139 int* out_verdict_received_time, | 152 int* out_verdict_received_time, |
| 140 LoginReputationClientResponse* out_verdict); | 153 LoginReputationClientResponse* out_verdict); |
| 141 | 154 |
| 142 static bool PathMatchCacheExpressionExactly( | 155 static bool PathMatchCacheExpressionExactly( |
| 143 const std::vector<std::string>& generated_paths, | 156 const std::vector<std::string>& generated_paths, |
| 144 const std::string& cache_expression_path); | 157 const std::string& cache_expression_path); |
| 145 | 158 |
| 146 static bool PathVariantsMatchCacheExpression( | 159 static bool PathVariantsMatchCacheExpression( |
| 147 const std::vector<std::string>& generated_paths, | 160 const std::vector<std::string>& generated_paths, |
| 148 const std::string& cache_expression_path); | 161 const std::string& cache_expression_path); |
| 149 | 162 |
| 150 static bool IsCacheExpired(int cache_creation_time, int cache_duration); | 163 static bool IsCacheExpired(int cache_creation_time, int cache_duration); |
| 151 | 164 |
| 152 static void GeneratePathVariantsWithoutQuery(const GURL& url, | 165 static void GeneratePathVariantsWithoutQuery(const GURL& url, |
| 153 std::vector<std::string>* paths); | 166 std::vector<std::string>* paths); |
| 154 | 167 |
| 155 static std::string GetCacheExpressionPath( | 168 static std::string GetCacheExpressionPath( |
| 156 const std::string& cache_expression); | 169 const std::string& cache_expression); |
| 157 | 170 |
| 158 static std::unique_ptr<base::DictionaryValue> CreateDictionaryFromVerdict( | 171 static std::unique_ptr<base::DictionaryValue> CreateDictionaryFromVerdict( |
| 159 const LoginReputationClientResponse* verdict, | 172 const LoginReputationClientResponse* verdict, |
| 160 const base::Time& receive_time); | 173 const base::Time& receive_time); |
| 161 | 174 |
| 162 // Stored verdict count for each HostContentSettingsMap. | 175 // Stored verdict count for each HostContentSettingsMap. |
|
Nathan Parker
2017/03/30 21:38:12
... for this profile (?)
Jialiu Lin
2017/03/30 23:23:11
Oops, forget to update comment.
Fixed.
| |
| 163 std::unordered_map<HostContentSettingsMap*, size_t> stored_verdict_counts_; | 176 int stored_verdict_count_; |
| 177 | |
| 178 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_; | |
| 164 | 179 |
| 165 // The context we use to issue network requests. This request_context_getter | 180 // The context we use to issue network requests. This request_context_getter |
| 166 // is obtained from SafeBrowsingService so that we can use the Safe Browsing | 181 // is obtained from SafeBrowsingService so that we can use the Safe Browsing |
| 167 // cookie store. | 182 // cookie store. |
| 168 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 183 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 169 | 184 |
| 170 // Set of pending PasswordProtectionRequests. | 185 // Set of pending PasswordProtectionRequests. |
| 171 std::unordered_set<std::unique_ptr<PasswordProtectionRequest>> requests_; | 186 std::unordered_set<std::unique_ptr<PasswordProtectionRequest>> requests_; |
| 172 | 187 |
| 173 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_; | 188 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| 189 history_service_observer_; | |
| 190 | |
| 191 // Content settings map associated with this instance. | |
| 192 HostContentSettingsMap* content_settings_; | |
| 193 | |
| 174 base::WeakPtrFactory<PasswordProtectionService> weak_factory_; | 194 base::WeakPtrFactory<PasswordProtectionService> weak_factory_; |
| 175 DISALLOW_COPY_AND_ASSIGN(PasswordProtectionService); | 195 DISALLOW_COPY_AND_ASSIGN(PasswordProtectionService); |
| 176 }; | 196 }; |
| 177 | 197 |
| 178 } // namespace safe_browsing | 198 } // namespace safe_browsing |
| 179 | 199 |
| 180 #endif // COMPONENTS_SAFE_BROWSING_PASSWORD_PROTECTION_PASSWORD_PROTECTION_SERV ICE_H_ | 200 #endif // COMPONENTS_SAFE_BROWSING_PASSWORD_PROTECTION_PASSWORD_PROTECTION_SERV ICE_H_ |
| OLD | NEW |