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

Side by Side Diff: chrome/browser/safe_browsing/safe_browsing_service.cc

Issue 2901213004: Componentize safe_browsing: factor out SafeBrowsingURLRequestContextGetter. (Closed)
Patch Set: cleanup includes Created 3 years, 7 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
« no previous file with comments | « no previous file | components/safe_browsing/browser/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/safe_browsing/safe_browsing_service.h" 5 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/bind_helpers.h" 12 #include "base/bind_helpers.h"
13 #include "base/callback.h" 13 #include "base/callback.h"
14 #include "base/command_line.h" 14 #include "base/command_line.h"
15 #include "base/lazy_instance.h" 15 #include "base/lazy_instance.h"
16 #include "base/macros.h" 16 #include "base/macros.h"
17 #include "base/memory/ptr_util.h" 17 #include "base/memory/ptr_util.h"
18 #include "base/metrics/histogram_macros.h" 18 #include "base/metrics/histogram_macros.h"
19 #include "base/path_service.h" 19 #include "base/path_service.h"
20 #include "base/single_thread_task_runner.h"
21 #include "base/strings/string_util.h" 20 #include "base/strings/string_util.h"
22 #include "base/task_scheduler/post_task.h"
23 #include "base/threading/thread.h" 21 #include "base/threading/thread.h"
24 #include "base/threading/thread_restrictions.h" 22 #include "base/threading/thread_restrictions.h"
25 #include "base/trace_event/trace_event.h" 23 #include "base/trace_event/trace_event.h"
26 #include "build/build_config.h" 24 #include "build/build_config.h"
27 #include "chrome/browser/browser_process.h" 25 #include "chrome/browser/browser_process.h"
28 #include "chrome/browser/chrome_notification_types.h" 26 #include "chrome/browser/chrome_notification_types.h"
29 #include "chrome/browser/profiles/profile.h" 27 #include "chrome/browser/profiles/profile.h"
30 #include "chrome/browser/profiles/profile_manager.h" 28 #include "chrome/browser/profiles/profile_manager.h"
31 #include "chrome/browser/safe_browsing/chrome_password_protection_service.h" 29 #include "chrome/browser/safe_browsing/chrome_password_protection_service.h"
32 #include "chrome/browser/safe_browsing/ping_manager.h" 30 #include "chrome/browser/safe_browsing/ping_manager.h"
33 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager .h" 31 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager .h"
34 #include "chrome/browser/safe_browsing/ui_manager.h" 32 #include "chrome/browser/safe_browsing/ui_manager.h"
35 #include "chrome/common/chrome_paths.h" 33 #include "chrome/common/chrome_paths.h"
36 #include "chrome/common/chrome_switches.h" 34 #include "chrome/common/chrome_switches.h"
37 #include "chrome/common/pref_names.h" 35 #include "chrome/common/pref_names.h"
38 #include "chrome/common/safe_browsing/file_type_policies.h" 36 #include "chrome/common/safe_browsing/file_type_policies.h"
39 #include "components/prefs/pref_change_registrar.h" 37 #include "components/prefs/pref_change_registrar.h"
40 #include "components/prefs/pref_service.h" 38 #include "components/prefs/pref_service.h"
39 #include "components/safe_browsing/browser/safe_browsing_url_request_context_get ter.h"
41 #include "components/safe_browsing/common/safebrowsing_constants.h" 40 #include "components/safe_browsing/common/safebrowsing_constants.h"
42 #include "components/safe_browsing/common/safebrowsing_switches.h" 41 #include "components/safe_browsing/common/safebrowsing_switches.h"
43 #include "components/safe_browsing/triggers/trigger_manager.h" 42 #include "components/safe_browsing/triggers/trigger_manager.h"
44 #include "components/safe_browsing_db/database_manager.h" 43 #include "components/safe_browsing_db/database_manager.h"
45 #include "components/safe_browsing_db/v4_feature_list.h" 44 #include "components/safe_browsing_db/v4_feature_list.h"
46 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" 45 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h"
47 #include "components/safe_browsing_db/v4_local_database_manager.h" 46 #include "components/safe_browsing_db/v4_local_database_manager.h"
48 #include "content/public/browser/browser_thread.h" 47 #include "content/public/browser/browser_thread.h"
49 #include "content/public/browser/cookie_store_factory.h"
50 #include "content/public/browser/notification_service.h" 48 #include "content/public/browser/notification_service.h"
51 #include "content/public/browser/resource_request_info.h" 49 #include "content/public/browser/resource_request_info.h"
52 #include "google_apis/google_api_keys.h" 50 #include "google_apis/google_api_keys.h"
53 #include "net/cookies/cookie_store.h"
54 #include "net/extras/sqlite/cookie_crypto_delegate.h"
55 #include "net/extras/sqlite/sqlite_channel_id_store.h"
56 #include "net/http/http_network_layer.h"
57 #include "net/http/http_transaction_factory.h"
58 #include "net/ssl/channel_id_service.h"
59 #include "net/ssl/default_channel_id_store.h"
60 #include "net/url_request/url_request_context.h"
61 #include "net/url_request/url_request_context_getter.h" 51 #include "net/url_request/url_request_context_getter.h"
Jialiu Lin 2017/05/24 21:42:31 nit: Do you still need this include?
timvolodine 2017/05/25 13:01:04 The URLRequestContextGetter is still used in 2 pla
62 #include "services/preferences/public/interfaces/tracked_preference_validation_d elegate.mojom.h" 52 #include "services/preferences/public/interfaces/tracked_preference_validation_d elegate.mojom.h"
63 53
64 #if defined(OS_WIN) 54 #if defined(OS_WIN)
65 #include "chrome/installer/util/browser_distribution.h" 55 #include "chrome/installer/util/browser_distribution.h"
66 #endif 56 #endif
67 57
68 #if defined(SAFE_BROWSING_DB_LOCAL) 58 #if defined(SAFE_BROWSING_DB_LOCAL)
69 #include "chrome/browser/safe_browsing/local_database_manager.h" 59 #include "chrome/browser/safe_browsing/local_database_manager.h"
70 #elif defined(SAFE_BROWSING_DB_REMOTE) 60 #elif defined(SAFE_BROWSING_DB_REMOTE)
71 #include "components/safe_browsing_db/remote_database_manager.h" 61 #include "components/safe_browsing_db/remote_database_manager.h"
(...skipping 10 matching lines...) Expand all
82 #include "chrome/browser/safe_browsing/incident_reporting/variations_seed_signat ure_analyzer.h" 72 #include "chrome/browser/safe_browsing/incident_reporting/variations_seed_signat ure_analyzer.h"
83 #include "chrome/browser/safe_browsing/protocol_manager.h" 73 #include "chrome/browser/safe_browsing/protocol_manager.h"
84 #endif 74 #endif
85 75
86 using content::BrowserThread; 76 using content::BrowserThread;
87 77
88 namespace safe_browsing { 78 namespace safe_browsing {
89 79
90 namespace { 80 namespace {
91 81
92 // Filename suffix for the cookie database.
93 const base::FilePath::CharType kCookiesFile[] = FILE_PATH_LITERAL(" Cookies");
94 const base::FilePath::CharType kChannelIDFile[] =
95 FILE_PATH_LITERAL(" Channel IDs");
96
97 // The default URL prefix where browser fetches chunk updates, hashes, 82 // The default URL prefix where browser fetches chunk updates, hashes,
98 // and reports safe browsing hits and malware details. 83 // and reports safe browsing hits and malware details.
99 const char kSbDefaultURLPrefix[] = 84 const char kSbDefaultURLPrefix[] =
100 "https://safebrowsing.google.com/safebrowsing"; 85 "https://safebrowsing.google.com/safebrowsing";
101 86
102 // The backup URL prefix used when there are issues establishing a connection 87 // The backup URL prefix used when there are issues establishing a connection
103 // with the server at the primary URL. 88 // with the server at the primary URL.
104 const char kSbBackupConnectErrorURLPrefix[] = 89 const char kSbBackupConnectErrorURLPrefix[] =
105 "https://alt1-safebrowsing.google.com/safebrowsing"; 90 "https://alt1-safebrowsing.google.com/safebrowsing";
106 91
107 // The backup URL prefix used when there are HTTP-specific issues with the 92 // The backup URL prefix used when there are HTTP-specific issues with the
108 // server at the primary URL. 93 // server at the primary URL.
109 const char kSbBackupHttpErrorURLPrefix[] = 94 const char kSbBackupHttpErrorURLPrefix[] =
110 "https://alt2-safebrowsing.google.com/safebrowsing"; 95 "https://alt2-safebrowsing.google.com/safebrowsing";
111 96
112 // The backup URL prefix used when there are local network specific issues. 97 // The backup URL prefix used when there are local network specific issues.
113 const char kSbBackupNetworkErrorURLPrefix[] = 98 const char kSbBackupNetworkErrorURLPrefix[] =
114 "https://alt3-safebrowsing.google.com/safebrowsing"; 99 "https://alt3-safebrowsing.google.com/safebrowsing";
115 100
116 base::FilePath CookieFilePath() {
117 return base::FilePath(
118 SafeBrowsingService::GetBaseFilename().value() + kCookiesFile);
119 }
120
121 base::FilePath ChannelIDFilePath() {
122 return base::FilePath(SafeBrowsingService::GetBaseFilename().value() +
123 kChannelIDFile);
124 }
125
126 } // namespace 101 } // namespace
127 102
128 class SafeBrowsingURLRequestContextGetter
129 : public net::URLRequestContextGetter {
130 public:
131 explicit SafeBrowsingURLRequestContextGetter(
132 scoped_refptr<net::URLRequestContextGetter> system_context_getter);
133
134 // Implementation for net::UrlRequestContextGetter.
135 net::URLRequestContext* GetURLRequestContext() override;
136 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
137 const override;
138
139 // Shuts down any pending requests using the getter, and sets |shut_down_| to
140 // true.
141 void ServiceShuttingDown();
142
143 // Disables QUIC. This should not be necessary anymore when
144 // http://crbug.com/678653 is implemented.
145 void DisableQuicOnIOThread();
146
147 protected:
148 ~SafeBrowsingURLRequestContextGetter() override;
149
150 private:
151 bool shut_down_;
152
153 scoped_refptr<net::URLRequestContextGetter> system_context_getter_;
154
155 std::unique_ptr<net::CookieStore> safe_browsing_cookie_store_;
156
157 std::unique_ptr<net::URLRequestContext> safe_browsing_request_context_;
158
159 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
160
161 std::unique_ptr<net::ChannelIDService> channel_id_service_;
162 std::unique_ptr<net::HttpNetworkSession> http_network_session_;
163 std::unique_ptr<net::HttpTransactionFactory> http_transaction_factory_;
164 };
165
166 SafeBrowsingURLRequestContextGetter::SafeBrowsingURLRequestContextGetter(
167 scoped_refptr<net::URLRequestContextGetter> system_context_getter)
168 : shut_down_(false),
169 system_context_getter_(system_context_getter),
170 network_task_runner_(
171 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)) {}
172
173 net::URLRequestContext*
174 SafeBrowsingURLRequestContextGetter::GetURLRequestContext() {
175 DCHECK_CURRENTLY_ON(BrowserThread::IO);
176
177 // Check if the service has been shut down.
178 if (shut_down_)
179 return nullptr;
180
181 if (!safe_browsing_request_context_) {
182 safe_browsing_request_context_.reset(new net::URLRequestContext());
183 // May be NULL in unit tests.
184 if (system_context_getter_) {
185 safe_browsing_request_context_->CopyFrom(
186 system_context_getter_->GetURLRequestContext());
187 }
188 scoped_refptr<base::SequencedTaskRunner> background_task_runner =
189 base::CreateSequencedTaskRunnerWithTraits(
190 {base::MayBlock(), base::TaskPriority::BACKGROUND,
191 base::TaskShutdownBehavior::BLOCK_SHUTDOWN});
192 // Set up the ChannelIDService
193 scoped_refptr<net::SQLiteChannelIDStore> channel_id_db =
194 new net::SQLiteChannelIDStore(ChannelIDFilePath(),
195 background_task_runner);
196 channel_id_service_.reset(new net::ChannelIDService(
197 new net::DefaultChannelIDStore(channel_id_db.get())));
198
199 // Set up the CookieStore
200 content::CookieStoreConfig cookie_config(
201 CookieFilePath(), content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
202 nullptr, nullptr);
203 cookie_config.channel_id_service = channel_id_service_.get();
204 cookie_config.background_task_runner = background_task_runner;
205 safe_browsing_cookie_store_ = content::CreateCookieStore(cookie_config);
206 safe_browsing_request_context_->set_cookie_store(
207 safe_browsing_cookie_store_.get());
208
209 safe_browsing_request_context_->set_channel_id_service(
210 channel_id_service_.get());
211 safe_browsing_cookie_store_->SetChannelIDServiceID(
212 channel_id_service_->GetUniqueID());
213
214 // Rebuild the HttpNetworkSession and the HttpTransactionFactory to use the
215 // new ChannelIDService.
216 if (safe_browsing_request_context_->http_transaction_factory() &&
217 safe_browsing_request_context_->http_transaction_factory()
218 ->GetSession()) {
219 net::HttpNetworkSession::Params safe_browsing_params =
220 safe_browsing_request_context_->http_transaction_factory()
221 ->GetSession()
222 ->params();
223 safe_browsing_params.channel_id_service = channel_id_service_.get();
224 http_network_session_.reset(
225 new net::HttpNetworkSession(safe_browsing_params));
226 http_transaction_factory_.reset(
227 new net::HttpNetworkLayer(http_network_session_.get()));
228 safe_browsing_request_context_->set_http_transaction_factory(
229 http_transaction_factory_.get());
230 }
231 safe_browsing_request_context_->set_name("safe_browsing");
232 }
233
234 return safe_browsing_request_context_.get();
235 }
236
237 scoped_refptr<base::SingleThreadTaskRunner>
238 SafeBrowsingURLRequestContextGetter::GetNetworkTaskRunner() const {
239 return network_task_runner_;
240 }
241
242 void SafeBrowsingURLRequestContextGetter::ServiceShuttingDown() {
243 DCHECK_CURRENTLY_ON(BrowserThread::IO);
244
245 shut_down_ = true;
246 URLRequestContextGetter::NotifyContextShuttingDown();
247 safe_browsing_request_context_.reset();
248 }
249
250 void SafeBrowsingURLRequestContextGetter::DisableQuicOnIOThread() {
251 DCHECK_CURRENTLY_ON(BrowserThread::IO);
252
253 if (http_network_session_)
254 http_network_session_->DisableQuic();
255 }
256
257 SafeBrowsingURLRequestContextGetter::~SafeBrowsingURLRequestContextGetter() {}
258
259 // static 103 // static
260 SafeBrowsingServiceFactory* SafeBrowsingService::factory_ = NULL; 104 SafeBrowsingServiceFactory* SafeBrowsingService::factory_ = NULL;
261 105
262 // The default SafeBrowsingServiceFactory. Global, made a singleton so we 106 // The default SafeBrowsingServiceFactory. Global, made a singleton so we
263 // don't leak it. 107 // don't leak it.
264 class SafeBrowsingServiceFactoryImpl : public SafeBrowsingServiceFactory { 108 class SafeBrowsingServiceFactoryImpl : public SafeBrowsingServiceFactory {
265 public: 109 public:
266 SafeBrowsingService* CreateSafeBrowsingService() override { 110 SafeBrowsingService* CreateSafeBrowsingService() override {
267 return new SafeBrowsingService(V4FeatureList::GetV4UsageStatus()); 111 return new SafeBrowsingService(V4FeatureList::GetV4UsageStatus());
268 } 112 }
269 113
270 private: 114 private:
271 friend struct base::LazyInstanceTraitsBase<SafeBrowsingServiceFactoryImpl>; 115 friend struct base::LazyInstanceTraitsBase<SafeBrowsingServiceFactoryImpl>;
272 116
273 SafeBrowsingServiceFactoryImpl() { } 117 SafeBrowsingServiceFactoryImpl() { }
274 118
275 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactoryImpl); 119 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactoryImpl);
276 }; 120 };
277 121
278 static base::LazyInstance<SafeBrowsingServiceFactoryImpl>::Leaky 122 static base::LazyInstance<SafeBrowsingServiceFactoryImpl>::Leaky
279 g_safe_browsing_service_factory_impl = LAZY_INSTANCE_INITIALIZER; 123 g_safe_browsing_service_factory_impl = LAZY_INSTANCE_INITIALIZER;
280 124
281 // static 125 // static
282 base::FilePath SafeBrowsingService::GetCookieFilePathForTesting() { 126 base::FilePath SafeBrowsingService::GetCookieFilePathForTesting() {
283 return CookieFilePath(); 127 return base::FilePath(SafeBrowsingService::GetBaseFilename().value() +
128 safe_browsing::kCookiesFile);
284 } 129 }
285 130
286 // static 131 // static
287 base::FilePath SafeBrowsingService::GetBaseFilename() { 132 base::FilePath SafeBrowsingService::GetBaseFilename() {
288 base::FilePath path; 133 base::FilePath path;
289 bool result = PathService::Get(chrome::DIR_USER_DATA, &path); 134 bool result = PathService::Get(chrome::DIR_USER_DATA, &path);
290 DCHECK(result); 135 DCHECK(result);
291 return path.Append(safe_browsing::kSafeBrowsingBaseFilename); 136 return path.Append(safe_browsing::kSafeBrowsingBaseFilename);
292 } 137 }
293 138
(...skipping 20 matching lines...) Expand all
314 // We should have already been shut down. If we're still enabled, then the 159 // We should have already been shut down. If we're still enabled, then the
315 // database isn't going to be closed properly, which could lead to corruption. 160 // database isn't going to be closed properly, which could lead to corruption.
316 DCHECK(!enabled_); 161 DCHECK(!enabled_);
317 } 162 }
318 163
319 void SafeBrowsingService::Initialize() { 164 void SafeBrowsingService::Initialize() {
320 // Ensure FileTypePolicies's Singleton is instantiated during startup. 165 // Ensure FileTypePolicies's Singleton is instantiated during startup.
321 // This guarantees we'll log UMA metrics about its state. 166 // This guarantees we'll log UMA metrics about its state.
322 FileTypePolicies::GetInstance(); 167 FileTypePolicies::GetInstance();
323 168
169 base::FilePath user_data_dir;
170 bool result = PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
171 DCHECK(result);
172
324 url_request_context_getter_ = new SafeBrowsingURLRequestContextGetter( 173 url_request_context_getter_ = new SafeBrowsingURLRequestContextGetter(
325 g_browser_process->system_request_context()); 174 g_browser_process->system_request_context(), user_data_dir);
326 175
327 ui_manager_ = CreateUIManager(); 176 ui_manager_ = CreateUIManager();
328 177
329 if (!use_v4_only_) { 178 if (!use_v4_only_) {
330 database_manager_ = CreateDatabaseManager(); 179 database_manager_ = CreateDatabaseManager();
331 } 180 }
332 181
333 navigation_observer_manager_ = new SafeBrowsingNavigationObserverManager(); 182 navigation_observer_manager_ = new SafeBrowsingNavigationObserverManager();
334 183
335 services_delegate_->Initialize(v4_enabled_); 184 services_delegate_->Initialize(v4_enabled_);
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after
788 auto it = password_protection_service_map_.find(profile); 637 auto it = password_protection_service_map_.find(profile);
789 if (it != password_protection_service_map_.end()) 638 if (it != password_protection_service_map_.end())
790 password_protection_service_map_.erase(it); 639 password_protection_service_map_.erase(it);
791 } 640 }
792 641
793 void SafeBrowsingService::CreateTriggerManager() { 642 void SafeBrowsingService::CreateTriggerManager() {
794 DCHECK_CURRENTLY_ON(BrowserThread::UI); 643 DCHECK_CURRENTLY_ON(BrowserThread::UI);
795 trigger_manager_ = base::MakeUnique<TriggerManager>(ui_manager_.get()); 644 trigger_manager_ = base::MakeUnique<TriggerManager>(ui_manager_.get());
796 } 645 }
797 } // namespace safe_browsing 646 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « no previous file | components/safe_browsing/browser/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698