| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/test_url_fetcher_factory.h" | 5 #include "content/test/test_url_fetcher_factory.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" |
| 9 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 10 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 11 #include "content/common/net/url_fetcher_impl.h" | 13 #include "content/common/net/url_fetcher_impl.h" |
| 12 #include "content/public/common/url_fetcher_delegate.h" | 14 #include "content/public/common/url_fetcher_delegate.h" |
| 13 #include "net/base/host_port_pair.h" | 15 #include "net/base/host_port_pair.h" |
| 14 #include "net/http/http_response_headers.h" | 16 #include "net/http/http_response_headers.h" |
| 15 #include "net/url_request/url_request_status.h" | 17 #include "net/url_request/url_request_status.h" |
| 16 | 18 |
| 17 ScopedURLFetcherFactory::ScopedURLFetcherFactory( | 19 ScopedURLFetcherFactory::ScopedURLFetcherFactory( |
| 18 content::URLFetcherFactory* factory) { | 20 content::URLFetcherFactory* factory) { |
| 19 DCHECK(!URLFetcherImpl::factory()); | 21 DCHECK(!URLFetcherImpl::factory()); |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 } | 227 } |
| 226 | 228 |
| 227 // This class is used by the FakeURLFetcherFactory below. | 229 // This class is used by the FakeURLFetcherFactory below. |
| 228 class FakeURLFetcher : public TestURLFetcher { | 230 class FakeURLFetcher : public TestURLFetcher { |
| 229 public: | 231 public: |
| 230 // Normal URL fetcher constructor but also takes in a pre-baked response. | 232 // Normal URL fetcher constructor but also takes in a pre-baked response. |
| 231 FakeURLFetcher(const GURL& url, | 233 FakeURLFetcher(const GURL& url, |
| 232 content::URLFetcherDelegate* d, | 234 content::URLFetcherDelegate* d, |
| 233 const std::string& response_data, bool success) | 235 const std::string& response_data, bool success) |
| 234 : TestURLFetcher(0, url, d), | 236 : TestURLFetcher(0, url, d), |
| 235 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 237 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 236 set_status(net::URLRequestStatus( | 238 set_status(net::URLRequestStatus( |
| 237 success ? net::URLRequestStatus::SUCCESS : | 239 success ? net::URLRequestStatus::SUCCESS : |
| 238 net::URLRequestStatus::FAILED, | 240 net::URLRequestStatus::FAILED, |
| 239 0)); | 241 0)); |
| 240 set_response_code(success ? 200 : 500); | 242 set_response_code(success ? 200 : 500); |
| 241 SetResponseString(response_data); | 243 SetResponseString(response_data); |
| 242 } | 244 } |
| 243 | 245 |
| 244 // Start the request. This will call the given delegate asynchronously | 246 // Start the request. This will call the given delegate asynchronously |
| 245 // with the pre-baked response as parameter. | 247 // with the pre-baked response as parameter. |
| 246 virtual void Start() OVERRIDE { | 248 virtual void Start() OVERRIDE { |
| 247 MessageLoop::current()->PostTask( | 249 MessageLoop::current()->PostTask( |
| 248 FROM_HERE, | 250 FROM_HERE, |
| 249 method_factory_.NewRunnableMethod(&FakeURLFetcher::RunDelegate)); | 251 base::Bind(&FakeURLFetcher::RunDelegate, weak_factory_.GetWeakPtr())); |
| 250 } | 252 } |
| 251 | 253 |
| 252 virtual const GURL& GetURL() const OVERRIDE { | 254 virtual const GURL& GetURL() const OVERRIDE { |
| 253 return TestURLFetcher::GetOriginalURL(); | 255 return TestURLFetcher::GetOriginalURL(); |
| 254 } | 256 } |
| 255 | 257 |
| 256 private: | 258 private: |
| 257 virtual ~FakeURLFetcher() { | 259 virtual ~FakeURLFetcher() { |
| 258 } | 260 } |
| 259 | 261 |
| 260 // This is the method which actually calls the delegate that is passed in the | 262 // This is the method which actually calls the delegate that is passed in the |
| 261 // constructor. | 263 // constructor. |
| 262 void RunDelegate() { | 264 void RunDelegate() { |
| 263 delegate()->OnURLFetchComplete(this); | 265 delegate()->OnURLFetchComplete(this); |
| 264 } | 266 } |
| 265 | 267 |
| 266 // Method factory used to run the delegate. | 268 base::WeakPtrFactory<FakeURLFetcher> weak_factory_; |
| 267 ScopedRunnableMethodFactory<FakeURLFetcher> method_factory_; | |
| 268 | 269 |
| 269 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher); | 270 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher); |
| 270 }; | 271 }; |
| 271 | 272 |
| 272 FakeURLFetcherFactory::FakeURLFetcherFactory() | 273 FakeURLFetcherFactory::FakeURLFetcherFactory() |
| 273 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 274 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 274 } | 275 } |
| 275 | 276 |
| 276 FakeURLFetcherFactory::FakeURLFetcherFactory( | 277 FakeURLFetcherFactory::FakeURLFetcherFactory( |
| 277 content::URLFetcherFactory* default_factory) | 278 content::URLFetcherFactory* default_factory) |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 | 315 |
| 315 URLFetcherImplFactory::~URLFetcherImplFactory() {} | 316 URLFetcherImplFactory::~URLFetcherImplFactory() {} |
| 316 | 317 |
| 317 content::URLFetcher* URLFetcherImplFactory::CreateURLFetcher( | 318 content::URLFetcher* URLFetcherImplFactory::CreateURLFetcher( |
| 318 int id, | 319 int id, |
| 319 const GURL& url, | 320 const GURL& url, |
| 320 content::URLFetcher::RequestType request_type, | 321 content::URLFetcher::RequestType request_type, |
| 321 content::URLFetcherDelegate* d) { | 322 content::URLFetcherDelegate* d) { |
| 322 return new URLFetcherImpl(url, request_type, d); | 323 return new URLFetcherImpl(url, request_type, d); |
| 323 } | 324 } |
| OLD | NEW |