Chromium Code Reviews| Index: mojo/loader/loader.cc |
| diff --git a/mojo/loader/loader.cc b/mojo/loader/loader.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9105a881269555bbcfb7b76c19d5acaf837aa240 |
| --- /dev/null |
| +++ b/mojo/loader/loader.cc |
| @@ -0,0 +1,70 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/threading/thread.h" |
| +#include "mojo/loader/loader.h" |
| +#include "mojo/loader/url_request_context_getter.h" |
| +#include "net/url_request/url_fetcher.h" |
| +#include "net/url_request/url_fetcher_delegate.h" |
| + |
| +namespace mojo { |
| +namespace loader { |
| + |
| +namespace { |
| + |
| +class FetchDelegate : public net::URLFetcherDelegate { |
| + public: |
| + explicit FetchDelegate(Loader::Delegate* delegate) |
| + : delegate_(delegate) { |
| + } |
| + |
| + virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| + |
| + private: |
| + Loader::Delegate* delegate_; |
| +}; |
| + |
| +void FetchDelegate::OnURLFetchComplete(const net::URLFetcher* source) { |
| + delegate_->DidCompleteLoad(source->GetURL()); |
| + delete this; |
| +} |
| + |
| +scoped_ptr<base::Thread> CreateIOThread(const char* name) { |
| + scoped_ptr<base::Thread> thread(new base::Thread(name)); |
| + base::Thread::Options options; |
| + options.message_loop_type = base::MessageLoop::TYPE_IO; |
| + thread->StartWithOptions(options); |
| + return thread.Pass(); |
| +} |
| + |
| +} |
|
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. :)
|
| + |
| +class Loader::Data { |
| + public: |
| + scoped_ptr<base::Thread> cache_thread; |
| + scoped_refptr<URLRequestContextGetter> url_request_context_getter; |
| +}; |
| + |
| +Loader::Loader(base::SingleThreadTaskRunner* network_runner, |
| + base::FilePath base_path) |
| + : data_(new Data()) { |
| + data_->cache_thread = CreateIOThread("cache_thread"); |
| + data_->url_request_context_getter = new URLRequestContextGetter( |
| + base_path, network_runner, data_->cache_thread->message_loop_proxy()); |
| +} |
| + |
| +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.
|
| +} |
| + |
| +void Loader::Load(const GURL& app_url, Delegate* delegate) { |
| + net::URLFetcher* fetcher = net::URLFetcher::Create( |
| + app_url, net::URLFetcher::GET, new FetchDelegate(delegate)); |
| + |
| + fetcher->SetRequestContext(data_->url_request_context_getter.get()); |
| + fetcher->Start(); |
| +} |
| + |
| +} // namespace loader |
| +} // namespace mojo |