Chromium Code Reviews| 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 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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, method_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 // Method factory used to run the delegate. |
| 267 ScopedRunnableMethodFactory<FakeURLFetcher> method_factory_; | 269 base::WeakPtrFactory<FakeURLFetcher> method_factory_; |
|
James Hawkins
2011/11/14 17:57:06
weak_factory_
dcheng
2011/11/14 20:47:49
Done.
| |
| 268 | 270 |
| 269 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher); | 271 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher); |
| 270 }; | 272 }; |
| 271 | 273 |
| 272 FakeURLFetcherFactory::FakeURLFetcherFactory() | 274 FakeURLFetcherFactory::FakeURLFetcherFactory() |
| 273 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 275 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 274 } | 276 } |
| 275 | 277 |
| 276 FakeURLFetcherFactory::FakeURLFetcherFactory( | 278 FakeURLFetcherFactory::FakeURLFetcherFactory( |
| 277 content::URLFetcherFactory* default_factory) | 279 content::URLFetcherFactory* default_factory) |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 314 | 316 |
| 315 URLFetcherImplFactory::~URLFetcherImplFactory() {} | 317 URLFetcherImplFactory::~URLFetcherImplFactory() {} |
| 316 | 318 |
| 317 content::URLFetcher* URLFetcherImplFactory::CreateURLFetcher( | 319 content::URLFetcher* URLFetcherImplFactory::CreateURLFetcher( |
| 318 int id, | 320 int id, |
| 319 const GURL& url, | 321 const GURL& url, |
| 320 content::URLFetcher::RequestType request_type, | 322 content::URLFetcher::RequestType request_type, |
| 321 content::URLFetcherDelegate* d) { | 323 content::URLFetcherDelegate* d) { |
| 322 return new URLFetcherImpl(url, request_type, d); | 324 return new URLFetcherImpl(url, request_type, d); |
| 323 } | 325 } |
| OLD | NEW |