Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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 "base/message_loop/message_loop.h" | |
| 6 #include "base/threading/thread.h" | |
| 7 #include "mojo/loader/loader.h" | |
| 8 #include "mojo/loader/url_request_context_getter.h" | |
| 9 #include "net/url_request/url_fetcher.h" | |
| 10 #include "net/url_request/url_fetcher_delegate.h" | |
| 11 | |
| 12 namespace mojo { | |
| 13 namespace loader { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 class FetchDelegate : public net::URLFetcherDelegate { | |
| 18 public: | |
| 19 explicit FetchDelegate(Loader::Delegate* delegate) | |
| 20 : delegate_(delegate) { | |
| 21 } | |
| 22 | |
| 23 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
| 24 | |
| 25 private: | |
| 26 Loader::Delegate* delegate_; | |
| 27 }; | |
| 28 | |
| 29 void FetchDelegate::OnURLFetchComplete(const net::URLFetcher* source) { | |
| 30 delegate_->DidCompleteLoad(source->GetURL()); | |
| 31 delete this; | |
| 32 } | |
| 33 | |
| 34 scoped_ptr<base::Thread> CreateIOThread(const char* name) { | |
| 35 scoped_ptr<base::Thread> thread(new base::Thread(name)); | |
| 36 base::Thread::Options options; | |
| 37 options.message_loop_type = base::MessageLoop::TYPE_IO; | |
| 38 thread->StartWithOptions(options); | |
| 39 return thread.Pass(); | |
| 40 } | |
| 41 | |
| 42 } | |
|
darin (slow to review)
2013/10/18 07:11:29
nit: "} // namespace"
abarth-chromium
2013/10/18 07:27:17
Thanks. My Chromium style is a bit rusty. :)
| |
| 43 | |
| 44 class Loader::Data { | |
| 45 public: | |
| 46 scoped_ptr<base::Thread> cache_thread; | |
| 47 scoped_refptr<URLRequestContextGetter> url_request_context_getter; | |
| 48 }; | |
| 49 | |
| 50 Loader::Loader(base::SingleThreadTaskRunner* network_runner, | |
| 51 base::FilePath base_path) | |
| 52 : data_(new Data()) { | |
| 53 data_->cache_thread = CreateIOThread("cache_thread"); | |
| 54 data_->url_request_context_getter = new URLRequestContextGetter( | |
| 55 base_path, network_runner, data_->cache_thread->message_loop_proxy()); | |
| 56 } | |
| 57 | |
| 58 Loader::~Loader() { | |
|
darin (slow to review)
2013/10/18 07:11:29
what if Loader is destroyed before OnURLFetchCompl
abarth-chromium
2013/10/18 07:27:17
That's a good point.
| |
| 59 } | |
| 60 | |
| 61 void Loader::Load(const GURL& app_url, Delegate* delegate) { | |
| 62 net::URLFetcher* fetcher = net::URLFetcher::Create( | |
| 63 app_url, net::URLFetcher::GET, new FetchDelegate(delegate)); | |
| 64 | |
| 65 fetcher->SetRequestContext(data_->url_request_context_getter.get()); | |
| 66 fetcher->Start(); | |
| 67 } | |
| 68 | |
| 69 } // namespace loader | |
| 70 } // namespace mojo | |
| OLD | NEW |