| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/update_client/url_request_post_interceptor.h" | 5 #include "components/update_client/url_request_post_interceptor.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 DISALLOW_COPY_AND_ASSIGN(URLRequestMockJob); | 60 DISALLOW_COPY_AND_ASSIGN(URLRequestMockJob); |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 URLRequestPostInterceptor::URLRequestPostInterceptor( | 63 URLRequestPostInterceptor::URLRequestPostInterceptor( |
| 64 const GURL& url, | 64 const GURL& url, |
| 65 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) | 65 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) |
| 66 : url_(url), io_task_runner_(io_task_runner), hit_count_(0) { | 66 : url_(url), io_task_runner_(io_task_runner), hit_count_(0) { |
| 67 } | 67 } |
| 68 | 68 |
| 69 URLRequestPostInterceptor::~URLRequestPostInterceptor() { | 69 URLRequestPostInterceptor::~URLRequestPostInterceptor() { |
| 70 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 70 DCHECK(io_task_runner_->RunsTasksInCurrentSequence()); |
| 71 ClearExpectations(); | 71 ClearExpectations(); |
| 72 } | 72 } |
| 73 | 73 |
| 74 void URLRequestPostInterceptor::ClearExpectations() { | 74 void URLRequestPostInterceptor::ClearExpectations() { |
| 75 while (!expectations_.empty()) { | 75 while (!expectations_.empty()) { |
| 76 Expectation expectation(expectations_.front()); | 76 Expectation expectation(expectations_.front()); |
| 77 delete expectation.first; | 77 delete expectation.first; |
| 78 expectations_.pop(); | 78 expectations_.pop(); |
| 79 } | 79 } |
| 80 } | 80 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 } | 147 } |
| 148 | 148 |
| 149 class URLRequestPostInterceptor::Delegate : public net::URLRequestInterceptor { | 149 class URLRequestPostInterceptor::Delegate : public net::URLRequestInterceptor { |
| 150 public: | 150 public: |
| 151 Delegate(const std::string& scheme, | 151 Delegate(const std::string& scheme, |
| 152 const std::string& hostname, | 152 const std::string& hostname, |
| 153 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) | 153 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) |
| 154 : scheme_(scheme), hostname_(hostname), io_task_runner_(io_task_runner) {} | 154 : scheme_(scheme), hostname_(hostname), io_task_runner_(io_task_runner) {} |
| 155 | 155 |
| 156 void Register() { | 156 void Register() { |
| 157 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 157 DCHECK(io_task_runner_->RunsTasksInCurrentSequence()); |
| 158 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( | 158 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( |
| 159 scheme_, hostname_, std::unique_ptr<net::URLRequestInterceptor>(this)); | 159 scheme_, hostname_, std::unique_ptr<net::URLRequestInterceptor>(this)); |
| 160 } | 160 } |
| 161 | 161 |
| 162 void Unregister() { | 162 void Unregister() { |
| 163 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 163 DCHECK(io_task_runner_->RunsTasksInCurrentSequence()); |
| 164 for (InterceptorMap::iterator it = interceptors_.begin(); | 164 for (InterceptorMap::iterator it = interceptors_.begin(); |
| 165 it != interceptors_.end(); ++it) | 165 it != interceptors_.end(); ++it) |
| 166 delete (*it).second; | 166 delete (*it).second; |
| 167 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme_, | 167 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme_, |
| 168 hostname_); | 168 hostname_); |
| 169 } | 169 } |
| 170 | 170 |
| 171 void OnCreateInterceptor(URLRequestPostInterceptor* interceptor) { | 171 void OnCreateInterceptor(URLRequestPostInterceptor* interceptor) { |
| 172 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 172 DCHECK(io_task_runner_->RunsTasksInCurrentSequence()); |
| 173 DCHECK(interceptors_.find(interceptor->GetUrl()) == interceptors_.end()); | 173 DCHECK(interceptors_.find(interceptor->GetUrl()) == interceptors_.end()); |
| 174 | 174 |
| 175 interceptors_.insert(std::make_pair(interceptor->GetUrl(), interceptor)); | 175 interceptors_.insert(std::make_pair(interceptor->GetUrl(), interceptor)); |
| 176 } | 176 } |
| 177 | 177 |
| 178 private: | 178 private: |
| 179 ~Delegate() override {} | 179 ~Delegate() override {} |
| 180 | 180 |
| 181 net::URLRequestJob* MaybeInterceptRequest( | 181 net::URLRequestJob* MaybeInterceptRequest( |
| 182 net::URLRequest* request, | 182 net::URLRequest* request, |
| 183 net::NetworkDelegate* network_delegate) const override { | 183 net::NetworkDelegate* network_delegate) const override { |
| 184 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 184 DCHECK(io_task_runner_->RunsTasksInCurrentSequence()); |
| 185 | 185 |
| 186 // Only intercepts POST. | 186 // Only intercepts POST. |
| 187 if (!request->has_upload()) | 187 if (!request->has_upload()) |
| 188 return NULL; | 188 return NULL; |
| 189 | 189 |
| 190 GURL url = request->url(); | 190 GURL url = request->url(); |
| 191 if (url.has_query()) { | 191 if (url.has_query()) { |
| 192 GURL::Replacements replacements; | 192 GURL::Replacements replacements; |
| 193 replacements.ClearQuery(); | 193 replacements.ClearQuery(); |
| 194 url = url.ReplaceComponents(replacements); | 194 url = url.ReplaceComponents(replacements); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 return CreateInterceptorForPath(POST_INTERCEPT_PATH); | 301 return CreateInterceptorForPath(POST_INTERCEPT_PATH); |
| 302 } | 302 } |
| 303 | 303 |
| 304 URLRequestPostInterceptor* InterceptorFactory::CreateInterceptorForPath( | 304 URLRequestPostInterceptor* InterceptorFactory::CreateInterceptorForPath( |
| 305 const char* url_path) { | 305 const char* url_path) { |
| 306 return URLRequestPostInterceptorFactory::CreateInterceptor( | 306 return URLRequestPostInterceptorFactory::CreateInterceptor( |
| 307 base::FilePath::FromUTF8Unsafe(url_path)); | 307 base::FilePath::FromUTF8Unsafe(url_path)); |
| 308 } | 308 } |
| 309 | 309 |
| 310 } // namespace update_client | 310 } // namespace update_client |
| OLD | NEW |