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

Side by Side Diff: net/url_request/url_request_prepackaged_interceptor.cc

Issue 508473002: Componentize component_updater: Move URLRequestPrepackagedInterceptor from content/ to net/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: self review Created 6 years, 3 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 (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 "content/test/net/url_request_prepackaged_interceptor.h" 5 #include "net/url_request/url_request_prepackaged_interceptor.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/threading/sequenced_worker_pool.h" 8 #include "base/threading/sequenced_worker_pool.h"
9 #include "base/threading/thread_restrictions.h" 9 #include "base/threading/thread_restrictions.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "net/url_request/url_request.h" 10 #include "net/url_request/url_request.h"
12 #include "net/url_request/url_request_file_job.h" 11 #include "net/url_request/url_request_file_job.h"
13 #include "net/url_request/url_request_filter.h" 12 #include "net/url_request/url_request_filter.h"
14 #include "net/url_request/url_request_interceptor.h" 13 #include "net/url_request/url_request_interceptor.h"
15 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
16 15
17 using content::BrowserThread; 16 namespace net {
18
19 namespace content {
20 17
21 namespace { 18 namespace {
22 19
23 class URLRequestPrepackagedJob : public net::URLRequestFileJob { 20 class URLRequestPrepackagedJob : public net::URLRequestFileJob {
24 public: 21 public:
25 URLRequestPrepackagedJob(net::URLRequest* request, 22 URLRequestPrepackagedJob(
26 net::NetworkDelegate* network_delegate, 23 net::URLRequest* request,
27 const base::FilePath& file_path) 24 net::NetworkDelegate* network_delegate,
28 : net::URLRequestFileJob( 25 const base::FilePath& file_path,
29 request, network_delegate, file_path, 26 const scoped_refptr<base::TaskRunner>& worker_task_runner)
30 content::BrowserThread::GetBlockingPool()-> 27 : net::URLRequestFileJob(request,
31 GetTaskRunnerWithShutdownBehavior( 28 network_delegate,
32 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)) {} 29 file_path,
30 worker_task_runner) {}
33 31
34 virtual int GetResponseCode() const OVERRIDE { return 200; } 32 virtual int GetResponseCode() const OVERRIDE { return 200; }
35 33
36 private: 34 private:
37 virtual ~URLRequestPrepackagedJob() {} 35 virtual ~URLRequestPrepackagedJob() {}
38 36
39 DISALLOW_COPY_AND_ASSIGN(URLRequestPrepackagedJob); 37 DISALLOW_COPY_AND_ASSIGN(URLRequestPrepackagedJob);
40 }; 38 };
41 39
42 } // namespace 40 } // namespace
43 41
44 class URLRequestPrepackagedInterceptor::Delegate 42 class URLRequestPrepackagedInterceptor::Delegate
45 : public net::URLRequestInterceptor { 43 : public net::URLRequestInterceptor {
46 public: 44 public:
47 Delegate(const std::string& scheme, const std::string& hostname) 45 Delegate(const std::string& scheme,
48 : scheme_(scheme), hostname_(hostname), hit_count_(0) {} 46 const std::string& hostname,
47 const scoped_refptr<base::TaskRunner>& io_task_runner,
48 const scoped_refptr<base::TaskRunner>& worker_task_runner)
49 : scheme_(scheme),
50 hostname_(hostname),
51 io_task_runner_(io_task_runner),
52 worker_task_runner_(worker_task_runner),
53 hit_count_(0) {}
49 virtual ~Delegate() {} 54 virtual ~Delegate() {}
50 55
51 void Register() { 56 void Register() {
52 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( 57 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor(
53 scheme_, hostname_, 58 scheme_, hostname_, scoped_ptr<net::URLRequestInterceptor>(this));
54 scoped_ptr<net::URLRequestInterceptor>(this));
55 } 59 }
56 60
57 static void Unregister( 61 static void Unregister(const std::string& scheme,
58 const std::string& scheme, 62 const std::string& hostname) {
59 const std::string& hostname) {
60 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme, 63 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme,
61 hostname); 64 hostname);
62 } 65 }
63 66
64 // When requests for |url| arrive, respond with the contents of |path|. The 67 // When requests for |url| arrive, respond with the contents of |path|. The
65 // hostname and scheme of |url| must match the corresponding parameters 68 // hostname and scheme of |url| must match the corresponding parameters
66 // passed as constructor arguments. 69 // passed as constructor arguments.
67 void SetResponse(const GURL& url, 70 void SetResponse(const GURL& url,
68 const base::FilePath& path, 71 const base::FilePath& path,
69 bool ignore_query) { 72 bool ignore_query) {
70 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 73 CHECK(io_task_runner_->RunsTasksOnCurrentThread());
Sorin Jianu 2014/08/26 20:32:52 I wonder if these CHECKS were correct to begin wit
tommycli 2014/08/26 21:06:58 I personally think they should be DCHECKs. I chang
71 // It's ok to do a blocking disk access on this thread; this class 74 // It's ok to do a blocking disk access on this thread; this class
72 // is just used for tests. 75 // is just used for tests.
73 base::ThreadRestrictions::ScopedAllowIO allow_io; 76 base::ThreadRestrictions::ScopedAllowIO allow_io;
74 EXPECT_TRUE(base::PathExists(path)); 77 EXPECT_TRUE(base::PathExists(path));
75 if (ignore_query) { 78 if (ignore_query) {
76 ignore_query_responses_[url] = path; 79 ignore_query_responses_[url] = path;
77 } else { 80 } else {
78 responses_[url] = path; 81 responses_[url] = path;
79 } 82 }
80 } 83 }
81 84
82 // Returns how many requests have been issued that have a stored reply. 85 // Returns how many requests have been issued that have a stored reply.
83 int GetHitCount() const { 86 int GetHitCount() const {
84 base::AutoLock auto_lock(hit_count_lock_); 87 base::AutoLock auto_lock(hit_count_lock_);
85 return hit_count_; 88 return hit_count_;
86 } 89 }
87 90
88 private: 91 private:
89 typedef std::map<GURL, base::FilePath> ResponseMap; 92 typedef std::map<GURL, base::FilePath> ResponseMap;
90 93
91 // When computing matches, this ignores the query parameters of the url. 94 // When computing matches, this ignores the query parameters of the url.
92 virtual net::URLRequestJob* MaybeInterceptRequest( 95 virtual net::URLRequestJob* MaybeInterceptRequest(
93 net::URLRequest* request, 96 net::URLRequest* request,
94 net::NetworkDelegate* network_delegate) const OVERRIDE { 97 net::NetworkDelegate* network_delegate) const OVERRIDE {
95 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 98 CHECK(io_task_runner_->RunsTasksOnCurrentThread());
96 if (request->url().scheme() != scheme_ || 99 if (request->url().scheme() != scheme_ ||
97 request->url().host() != hostname_) { 100 request->url().host() != hostname_) {
98 return NULL; 101 return NULL;
99 } 102 }
100 103
101 ResponseMap::const_iterator it = responses_.find(request->url()); 104 ResponseMap::const_iterator it = responses_.find(request->url());
102 if (it == responses_.end()) { 105 if (it == responses_.end()) {
103 // Search for this request's url, ignoring any query parameters. 106 // Search for this request's url, ignoring any query parameters.
104 GURL url = request->url(); 107 GURL url = request->url();
105 if (url.has_query()) { 108 if (url.has_query()) {
106 GURL::Replacements replacements; 109 GURL::Replacements replacements;
107 replacements.ClearQuery(); 110 replacements.ClearQuery();
108 url = url.ReplaceComponents(replacements); 111 url = url.ReplaceComponents(replacements);
109 } 112 }
110 it = ignore_query_responses_.find(url); 113 it = ignore_query_responses_.find(url);
111 if (it == ignore_query_responses_.end()) 114 if (it == ignore_query_responses_.end())
112 return NULL; 115 return NULL;
113 } 116 }
114 { 117 {
115 base::AutoLock auto_lock(hit_count_lock_); 118 base::AutoLock auto_lock(hit_count_lock_);
116 ++hit_count_; 119 ++hit_count_;
117 } 120 }
118 121
119 return new URLRequestPrepackagedJob(request, 122 return new URLRequestPrepackagedJob(
120 network_delegate, 123 request, network_delegate, it->second, worker_task_runner_);
121 it->second);
122 } 124 }
123 125
124 const std::string scheme_; 126 const std::string scheme_;
125 const std::string hostname_; 127 const std::string hostname_;
126 128
129 const scoped_refptr<base::TaskRunner> io_task_runner_;
130 const scoped_refptr<base::TaskRunner> worker_task_runner_;
131
127 ResponseMap responses_; 132 ResponseMap responses_;
128 ResponseMap ignore_query_responses_; 133 ResponseMap ignore_query_responses_;
129 134
130 mutable base::Lock hit_count_lock_; 135 mutable base::Lock hit_count_lock_;
131 mutable int hit_count_; 136 mutable int hit_count_;
132 137
133 DISALLOW_COPY_AND_ASSIGN(Delegate); 138 DISALLOW_COPY_AND_ASSIGN(Delegate);
134 }; 139 };
135 140
136
137 URLRequestPrepackagedInterceptor::URLRequestPrepackagedInterceptor( 141 URLRequestPrepackagedInterceptor::URLRequestPrepackagedInterceptor(
138 const std::string& scheme, 142 const std::string& scheme,
139 const std::string& hostname) 143 const std::string& hostname,
144 const scoped_refptr<base::TaskRunner>& io_task_runner,
145 const scoped_refptr<base::TaskRunner>& worker_task_runner)
140 : scheme_(scheme), 146 : scheme_(scheme),
141 hostname_(hostname), 147 hostname_(hostname),
142 delegate_(new Delegate(scheme, hostname)) { 148 io_task_runner_(io_task_runner),
143 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 149 delegate_(
144 base::Bind(&Delegate::Register, 150 new Delegate(scheme, hostname, io_task_runner_, worker_task_runner)) {
145 base::Unretained(delegate_))); 151 io_task_runner_->PostTask(
152 FROM_HERE, base::Bind(&Delegate::Register, base::Unretained(delegate_)));
146 } 153 }
147 154
148 URLRequestPrepackagedInterceptor::~URLRequestPrepackagedInterceptor() { 155 URLRequestPrepackagedInterceptor::~URLRequestPrepackagedInterceptor() {
149 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 156 io_task_runner_->PostTask(
150 base::Bind(&Delegate::Unregister, 157 FROM_HERE, base::Bind(&Delegate::Unregister, scheme_, hostname_));
151 scheme_,
152 hostname_));
153 } 158 }
154 159
155 void URLRequestPrepackagedInterceptor::SetResponse( 160 void URLRequestPrepackagedInterceptor::SetResponse(const GURL& url,
156 const GURL& url, 161 const base::FilePath& path) {
157 const base::FilePath& path) {
158 CHECK_EQ(scheme_, url.scheme()); 162 CHECK_EQ(scheme_, url.scheme());
159 CHECK_EQ(hostname_, url.host()); 163 CHECK_EQ(hostname_, url.host());
160 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 164 io_task_runner_->PostTask(FROM_HERE,
161 base::Bind(&Delegate::SetResponse, 165 base::Bind(&Delegate::SetResponse,
162 base::Unretained(delegate_), url, path, 166 base::Unretained(delegate_),
163 false)); 167 url,
168 path,
169 false));
164 } 170 }
165 171
166 void URLRequestPrepackagedInterceptor::SetResponseIgnoreQuery( 172 void URLRequestPrepackagedInterceptor::SetResponseIgnoreQuery(
167 const GURL& url, 173 const GURL& url,
168 const base::FilePath& path) { 174 const base::FilePath& path) {
169 CHECK_EQ(scheme_, url.scheme()); 175 CHECK_EQ(scheme_, url.scheme());
170 CHECK_EQ(hostname_, url.host()); 176 CHECK_EQ(hostname_, url.host());
171 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 177 io_task_runner_->PostTask(FROM_HERE,
172 base::Bind(&Delegate::SetResponse, 178 base::Bind(&Delegate::SetResponse,
173 base::Unretained(delegate_), url, path, 179 base::Unretained(delegate_),
174 true)); 180 url,
181 path,
182 true));
175 } 183 }
176 184
177 int URLRequestPrepackagedInterceptor::GetHitCount() { 185 int URLRequestPrepackagedInterceptor::GetHitCount() {
178 return delegate_->GetHitCount(); 186 return delegate_->GetHitCount();
179 } 187 }
180 188
181 189 URLLocalHostRequestPrepackagedInterceptor::
182 URLLocalHostRequestPrepackagedInterceptor 190 URLLocalHostRequestPrepackagedInterceptor(
183 ::URLLocalHostRequestPrepackagedInterceptor() 191 const scoped_refptr<base::TaskRunner>& io_task_runner,
184 : URLRequestPrepackagedInterceptor("http", "localhost") { 192 const scoped_refptr<base::TaskRunner>& worker_task_runner)
193 : URLRequestPrepackagedInterceptor("http",
194 "localhost",
195 io_task_runner,
196 worker_task_runner) {
185 } 197 }
186 198
187 } // namespace content 199 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698