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