| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
| 6 #include "base/threading/thread.h" |
| 6 #include "mojo/loader/loader.h" | 7 #include "mojo/loader/loader.h" |
| 7 #include "mojo/loader/url_request_context_getter.h" | 8 #include "mojo/loader/url_request_context_getter.h" |
| 8 #include "net/url_request/url_fetcher.h" | 9 #include "net/url_request/url_fetcher.h" |
| 9 #include "net/url_request/url_fetcher_delegate.h" | 10 #include "net/url_request/url_fetcher_delegate.h" |
| 10 | 11 |
| 11 namespace mojo { | 12 namespace mojo { |
| 12 namespace loader { | 13 namespace loader { |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 class FetchDelegate : public net::URLFetcherDelegate { | 17 class FetchDelegate : public net::URLFetcherDelegate { |
| 17 public: | 18 public: |
| 18 explicit FetchDelegate(Loader::Delegate* delegate) | 19 explicit FetchDelegate(Loader::Delegate* delegate) |
| 19 : delegate_(delegate) { | 20 : delegate_(delegate) { |
| 20 } | 21 } |
| 21 | 22 |
| 22 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 23 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 23 | 24 |
| 24 private: | 25 private: |
| 25 Loader::Delegate* delegate_; | 26 Loader::Delegate* delegate_; |
| 26 }; | 27 }; |
| 27 | 28 |
| 28 void FetchDelegate::OnURLFetchComplete(const net::URLFetcher* source) { | 29 void FetchDelegate::OnURLFetchComplete(const net::URLFetcher* source) { |
| 29 delegate_->DidCompleteLoad(source->GetURL()); | 30 delegate_->DidCompleteLoad(source->GetURL()); |
| 30 delete this; | 31 delete this; |
| 31 } | 32 } |
| 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 |
| 33 } | 42 } |
| 34 | 43 |
| 35 class Loader::Data { | 44 class Loader::Data { |
| 36 public: | 45 public: |
| 37 Data(); | 46 Data(); |
| 38 ~Data(); | 47 ~Data(); |
| 39 | 48 |
| 49 scoped_ptr<base::Thread> network_thread_; |
| 50 scoped_ptr<base::Thread> cache_thread_; |
| 40 scoped_refptr<URLRequestContextGetter> url_request_context_getter; | 51 scoped_refptr<URLRequestContextGetter> url_request_context_getter; |
| 41 }; | 52 }; |
| 42 | 53 |
| 43 Loader::Data::Data() | 54 Loader::Data::Data() { |
| 44 : url_request_context_getter(new URLRequestContextGetter()) { | 55 network_thread_ = CreateIOThread("network_thread"); |
| 56 cache_thread_ = CreateIOThread("cache_thread"); |
| 57 |
| 58 url_request_context_getter = new URLRequestContextGetter( |
| 59 network_thread_->message_loop_proxy(), |
| 60 cache_thread_->message_loop_proxy()); |
| 45 } | 61 } |
| 46 | 62 |
| 47 Loader::Data::~Data() { | 63 Loader::Data::~Data() { |
| 48 } | 64 } |
| 49 | 65 |
| 50 Loader::Loader() | 66 Loader::Loader() |
| 51 : data_(new Data()) { | 67 : data_(new Data()) { |
| 52 } | 68 } |
| 53 | 69 |
| 54 Loader::~Loader() { | 70 Loader::~Loader() { |
| 55 } | 71 } |
| 56 | 72 |
| 57 void Loader::Load(const GURL& app_url, Delegate* delegate) { | 73 void Loader::Load(const GURL& app_url, Delegate* delegate) { |
| 58 net::URLFetcher* fetcher = net::URLFetcher::Create( | 74 net::URLFetcher* fetcher = net::URLFetcher::Create( |
| 59 app_url, net::URLFetcher::GET, new FetchDelegate(delegate)); | 75 app_url, net::URLFetcher::GET, new FetchDelegate(delegate)); |
| 60 | 76 |
| 61 fetcher->SetRequestContext(data_->url_request_context_getter.get()); | 77 fetcher->SetRequestContext(data_->url_request_context_getter.get()); |
| 62 fetcher->Start(); | 78 fetcher->Start(); |
| 63 } | 79 } |
| 64 | 80 |
| 65 } // namespace loader | 81 } // namespace loader |
| 66 } // namespace mojo | 82 } // namespace mojo |
| OLD | NEW |