OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "sky/viewer/content_handler_impl.h" | 5 #include "sky/viewer/content_handler_impl.h" |
6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "mojo/public/cpp/application/connect.h" |
| 9 #include "mojo/services/public/interfaces/network/network_service.mojom.h" |
7 #include "sky/viewer/document_view.h" | 10 #include "sky/viewer/document_view.h" |
8 | 11 |
9 namespace sky { | 12 namespace sky { |
10 | 13 |
| 14 class SkyApplication : public mojo::Application { |
| 15 public: |
| 16 SkyApplication(scoped_refptr<base::MessageLoopProxy> compositor_thread, |
| 17 mojo::ShellPtr shell, |
| 18 mojo::URLResponsePtr response) |
| 19 : compositor_thread_(compositor_thread), |
| 20 url_(response->url), |
| 21 shell_(shell.Pass()), |
| 22 initial_response_(response.Pass()), |
| 23 view_count_(0) { |
| 24 shell_.set_client(this); |
| 25 mojo::ServiceProviderPtr service_provider; |
| 26 shell_->ConnectToApplication("mojo:network_service", |
| 27 mojo::GetProxy(&service_provider)); |
| 28 mojo::ConnectToService(service_provider.get(), &network_service_); |
| 29 } |
| 30 |
| 31 void Initialize(mojo::Array<mojo::String> args) override {} |
| 32 |
| 33 void AcceptConnection(const mojo::String& requestor_url, |
| 34 mojo::ServiceProviderPtr provider) override { |
| 35 ++view_count_; |
| 36 if (initial_response_) { |
| 37 OnResponseReceived(mojo::URLLoaderPtr(), provider.Pass(), |
| 38 initial_response_.Pass()); |
| 39 } else { |
| 40 mojo::URLLoaderPtr loader; |
| 41 network_service_->CreateURLLoader(mojo::GetProxy(&loader)); |
| 42 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
| 43 request->url = url_; |
| 44 request->auto_follow_redirects = true; |
| 45 |
| 46 // |loader| will be pass to the OnResponseReceived method through a |
| 47 // callback. Because order of evaluation is undefined, a reference to the |
| 48 // raw pointer is needed. |
| 49 mojo::URLLoader* raw_loader = loader.get(); |
| 50 raw_loader->Start( |
| 51 request.Pass(), |
| 52 base::Bind(&SkyApplication::OnResponseReceived, |
| 53 base::Unretained(this), base::Passed(&loader), |
| 54 base::Passed(&provider))); |
| 55 } |
| 56 } |
| 57 |
| 58 private: |
| 59 void OnViewDestroyed() { |
| 60 --view_count_; |
| 61 if (view_count_ == 0) { |
| 62 delete this; |
| 63 } |
| 64 } |
| 65 |
| 66 void OnResponseReceived(mojo::URLLoaderPtr loader, |
| 67 mojo::ServiceProviderPtr provider, |
| 68 mojo::URLResponsePtr response) { |
| 69 new DocumentView( |
| 70 base::Bind(&SkyApplication::OnViewDestroyed, base::Unretained(this)), |
| 71 provider.Pass(), response.Pass(), shell_.get(), compositor_thread_); |
| 72 } |
| 73 |
| 74 scoped_refptr<base::MessageLoopProxy> compositor_thread_; |
| 75 mojo::String url_; |
| 76 mojo::ShellPtr shell_; |
| 77 mojo::NetworkServicePtr network_service_; |
| 78 mojo::URLResponsePtr initial_response_; |
| 79 uint32_t view_count_; |
| 80 }; |
| 81 |
11 ContentHandlerImpl::ContentHandlerImpl( | 82 ContentHandlerImpl::ContentHandlerImpl( |
12 scoped_refptr<base::MessageLoopProxy> compositor_thread) | 83 scoped_refptr<base::MessageLoopProxy> compositor_thread) |
13 : compositor_thread_(compositor_thread) { | 84 : compositor_thread_(compositor_thread) { |
14 } | 85 } |
15 | 86 |
16 ContentHandlerImpl::~ContentHandlerImpl() { | 87 ContentHandlerImpl::~ContentHandlerImpl() { |
17 } | 88 } |
18 | 89 |
19 void ContentHandlerImpl::StartApplication(mojo::ShellPtr shell, | 90 void ContentHandlerImpl::StartApplication(mojo::ShellPtr shell, |
20 mojo::URLResponsePtr response) { | 91 mojo::URLResponsePtr response) { |
21 new DocumentView(response.Pass(), shell.Pass(), compositor_thread_); | 92 new SkyApplication(compositor_thread_, shell.Pass(), response.Pass()); |
22 } | 93 } |
23 | 94 |
24 } // namespace sky | 95 } // namespace sky |
OLD | NEW |