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