| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/loader/loader.h" | |
| 6 | |
| 7 #include "base/message_loop/message_loop.h" | |
| 8 #include "base/threading/thread.h" | |
| 9 #include "mojo/loader/url_request_context_getter.h" | |
| 10 #include "net/url_request/url_fetcher.h" | |
| 11 #include "net/url_request/url_fetcher_delegate.h" | |
| 12 | |
| 13 namespace mojo { | |
| 14 namespace loader { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 class JobImpl : public net::URLFetcherDelegate, public Job { | |
| 19 public: | |
| 20 JobImpl(const GURL& app_url, Job::Delegate* delegate); | |
| 21 virtual ~JobImpl(); | |
| 22 | |
| 23 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
| 24 | |
| 25 net::URLFetcher* fetcher() const { return fetcher_.get(); } | |
| 26 | |
| 27 private: | |
| 28 scoped_ptr<net::URLFetcher> fetcher_; | |
| 29 Job::Delegate* delegate_; | |
| 30 | |
| 31 DISALLOW_COPY_AND_ASSIGN(JobImpl); | |
| 32 }; | |
| 33 | |
| 34 JobImpl::JobImpl(const GURL& app_url, Job::Delegate* delegate) | |
| 35 : delegate_(delegate) { | |
| 36 fetcher_.reset(net::URLFetcher::Create(app_url, net::URLFetcher::GET, this)); | |
| 37 } | |
| 38 | |
| 39 JobImpl::~JobImpl() { | |
| 40 } | |
| 41 | |
| 42 void JobImpl::OnURLFetchComplete(const net::URLFetcher* source) { | |
| 43 base::FilePath app_path; | |
| 44 source->GetResponseAsFilePath(true, &app_path); | |
| 45 delegate_->DidCompleteLoad(source->GetURL(), app_path); | |
| 46 } | |
| 47 | |
| 48 scoped_ptr<base::Thread> CreateIOThread(const char* name) { | |
| 49 scoped_ptr<base::Thread> thread(new base::Thread(name)); | |
| 50 base::Thread::Options options; | |
| 51 options.message_loop_type = base::MessageLoop::TYPE_IO; | |
| 52 thread->StartWithOptions(options); | |
| 53 return thread.Pass(); | |
| 54 } | |
| 55 | |
| 56 } // namespace | |
| 57 | |
| 58 class Loader::Data { | |
| 59 public: | |
| 60 scoped_refptr<base::SingleThreadTaskRunner> file_runner; | |
| 61 scoped_ptr<base::Thread> cache_thread; | |
| 62 scoped_refptr<URLRequestContextGetter> url_request_context_getter; | |
| 63 }; | |
| 64 | |
| 65 Loader::Loader(base::SingleThreadTaskRunner* network_runner, | |
| 66 base::SingleThreadTaskRunner* file_runner, | |
| 67 base::FilePath base_path) | |
| 68 : data_(new Data()) { | |
| 69 data_->file_runner = file_runner; | |
| 70 data_->cache_thread = CreateIOThread("cache_thread"); | |
| 71 data_->url_request_context_getter = new URLRequestContextGetter( | |
| 72 base_path, network_runner, data_->cache_thread->message_loop_proxy()); | |
| 73 } | |
| 74 | |
| 75 Loader::~Loader() { | |
| 76 } | |
| 77 | |
| 78 scoped_ptr<Job> Loader::Load(const GURL& app_url, Job::Delegate* delegate) { | |
| 79 JobImpl* job = new JobImpl(app_url, delegate); | |
| 80 job->fetcher()->SetRequestContext(data_->url_request_context_getter.get()); | |
| 81 job->fetcher()->SaveResponseToTemporaryFile(data_->file_runner.get()); | |
| 82 job->fetcher()->Start(); | |
| 83 return make_scoped_ptr(static_cast<Job*>(job)); | |
| 84 } | |
| 85 | |
| 86 } // namespace loader | |
| 87 } // namespace mojo | |
| OLD | NEW |