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

Side by Side Diff: components/safe_browsing/browser/safe_browsing_url_request_context_getter.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
OLDNEW
(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 "components/safe_browsing/browser/safe_browsing_url_request_context_get ter.h"
6
7 #include "base/single_thread_task_runner.h"
8 #include "base/task_scheduler/post_task.h"
9 #include "components/safe_browsing/common/safebrowsing_constants.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/cookie_store_factory.h"
12 #include "net/cookies/cookie_store.h"
13 #include "net/extras/sqlite/sqlite_channel_id_store.h"
14 #include "net/http/http_network_layer.h"
15 #include "net/http/http_transaction_factory.h"
16 #include "net/ssl/channel_id_service.h"
17 #include "net/ssl/default_channel_id_store.h"
18 #include "net/url_request/url_request_context.h"
19
20 using content::BrowserThread;
21
22 namespace safe_browsing {
23
24 SafeBrowsingURLRequestContextGetter::SafeBrowsingURLRequestContextGetter(
25 scoped_refptr<net::URLRequestContextGetter> system_context_getter,
26 const base::FilePath& user_data_dir)
27 : shut_down_(false),
28 user_data_dir_(user_data_dir),
29 system_context_getter_(system_context_getter),
30 network_task_runner_(
31 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)) {
32 DCHECK(!user_data_dir.empty());
33 }
34
35 net::URLRequestContext*
36 SafeBrowsingURLRequestContextGetter::GetURLRequestContext() {
37 DCHECK_CURRENTLY_ON(BrowserThread::IO);
38
39 // Check if the service has been shut down.
40 if (shut_down_)
41 return nullptr;
42
43 if (!safe_browsing_request_context_) {
44 safe_browsing_request_context_.reset(new net::URLRequestContext());
45 // May be NULL in unit tests.
46 if (system_context_getter_) {
47 safe_browsing_request_context_->CopyFrom(
48 system_context_getter_->GetURLRequestContext());
49 }
50 scoped_refptr<base::SequencedTaskRunner> background_task_runner =
51 base::CreateSequencedTaskRunnerWithTraits(
52 {base::MayBlock(), base::TaskPriority::BACKGROUND,
53 base::TaskShutdownBehavior::BLOCK_SHUTDOWN});
54 // Set up the ChannelIDService
55 scoped_refptr<net::SQLiteChannelIDStore> channel_id_db =
56 new net::SQLiteChannelIDStore(ChannelIDFilePath(),
57 background_task_runner);
58 channel_id_service_.reset(new net::ChannelIDService(
59 new net::DefaultChannelIDStore(channel_id_db.get())));
60
61 // Set up the CookieStore
62 content::CookieStoreConfig cookie_config(
63 CookieFilePath(), content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
64 nullptr, nullptr);
65 cookie_config.channel_id_service = channel_id_service_.get();
66 cookie_config.background_task_runner = background_task_runner;
67 safe_browsing_cookie_store_ = content::CreateCookieStore(cookie_config);
68 safe_browsing_request_context_->set_cookie_store(
69 safe_browsing_cookie_store_.get());
70
71 safe_browsing_request_context_->set_channel_id_service(
72 channel_id_service_.get());
73 safe_browsing_cookie_store_->SetChannelIDServiceID(
74 channel_id_service_->GetUniqueID());
75
76 // Rebuild the HttpNetworkSession and the HttpTransactionFactory to use the
77 // new ChannelIDService.
78 if (safe_browsing_request_context_->http_transaction_factory() &&
79 safe_browsing_request_context_->http_transaction_factory()
80 ->GetSession()) {
81 net::HttpNetworkSession::Params safe_browsing_params =
82 safe_browsing_request_context_->http_transaction_factory()
83 ->GetSession()
84 ->params();
85 safe_browsing_params.channel_id_service = channel_id_service_.get();
86 http_network_session_.reset(
87 new net::HttpNetworkSession(safe_browsing_params));
88 http_transaction_factory_.reset(
89 new net::HttpNetworkLayer(http_network_session_.get()));
90 safe_browsing_request_context_->set_http_transaction_factory(
91 http_transaction_factory_.get());
92 }
93 safe_browsing_request_context_->set_name("safe_browsing");
94 }
95
96 return safe_browsing_request_context_.get();
97 }
98
99 scoped_refptr<base::SingleThreadTaskRunner>
100 SafeBrowsingURLRequestContextGetter::GetNetworkTaskRunner() const {
101 return network_task_runner_;
102 }
103
104 void SafeBrowsingURLRequestContextGetter::ServiceShuttingDown() {
105 DCHECK_CURRENTLY_ON(BrowserThread::IO);
106
107 shut_down_ = true;
108 URLRequestContextGetter::NotifyContextShuttingDown();
109 safe_browsing_request_context_.reset();
110 }
111
112 void SafeBrowsingURLRequestContextGetter::DisableQuicOnIOThread() {
113 DCHECK_CURRENTLY_ON(BrowserThread::IO);
114
115 if (http_network_session_)
116 http_network_session_->DisableQuic();
117 }
118
119 base::FilePath SafeBrowsingURLRequestContextGetter::GetBaseFilename() {
120 base::FilePath path(user_data_dir_);
121 return path.Append(kSafeBrowsingBaseFilename);
122 }
123
124 base::FilePath SafeBrowsingURLRequestContextGetter::CookieFilePath() {
125 return base::FilePath(GetBaseFilename().value() + kCookiesFile);
126 }
127
128 base::FilePath SafeBrowsingURLRequestContextGetter::ChannelIDFilePath() {
129 return base::FilePath(GetBaseFilename().value() + kChannelIDFile);
130 }
131
132 SafeBrowsingURLRequestContextGetter::~SafeBrowsingURLRequestContextGetter() {}
133
134 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698