Chromium Code Reviews| 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 "base/files/scoped_temp_dir.h" | |
| 9 #include "mojo/common/data_pipe_utils.h" | |
| 8 #include "mojo/public/cpp/application/connect.h" | 10 #include "mojo/public/cpp/application/connect.h" |
| 9 #include "mojo/public/cpp/bindings/strong_binding.h" | 11 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 10 #include "mojo/public/cpp/utility/run_loop.h" | 12 #include "mojo/public/cpp/utility/run_loop.h" |
| 11 #include "mojo/services/network/public/interfaces/network_service.mojom.h" | 13 #include "mojo/services/network/public/interfaces/network_service.mojom.h" |
| 12 #include "sky/viewer/document_view.h" | 14 #include "sky/viewer/document_view.h" |
| 15 #include "third_party/zlib/google/zip_reader.h" | |
| 13 | 16 |
| 14 namespace sky { | 17 namespace sky { |
| 15 | 18 |
| 16 class SkyApplication : public mojo::Application { | 19 class SkyApplication : public mojo::Application { |
| 17 public: | 20 public: |
| 18 SkyApplication(mojo::InterfaceRequest<mojo::Application> application, | 21 SkyApplication(mojo::InterfaceRequest<mojo::Application> application, |
| 19 mojo::URLResponsePtr response) | 22 mojo::URLResponsePtr response) |
| 20 : binding_(this, application.Pass()), | 23 : binding_(this, application.Pass()), |
| 21 initial_response_(response.Pass()) {} | 24 initial_response_(response.Pass()), |
| 25 is_packaged_application_(false) {} | |
| 22 | 26 |
| 23 void Initialize(mojo::ShellPtr shell, | 27 void Initialize(mojo::ShellPtr shell, |
| 24 mojo::Array<mojo::String> args, | 28 mojo::Array<mojo::String> args, |
| 25 const mojo::String& url) override { | 29 const mojo::String& url) override { |
| 26 shell_ = shell.Pass(); | 30 shell_ = shell.Pass(); |
| 27 mojo::ServiceProviderPtr service_provider; | 31 mojo::ServiceProviderPtr service_provider; |
| 28 shell_->ConnectToApplication("mojo:network_service", | 32 shell_->ConnectToApplication("mojo:network_service", |
| 29 mojo::GetProxy(&service_provider), nullptr); | 33 mojo::GetProxy(&service_provider), nullptr); |
| 30 mojo::ConnectToService(service_provider.get(), &network_service_); | 34 mojo::ConnectToService(service_provider.get(), &network_service_); |
| 31 } | 35 } |
| 32 | 36 |
| 33 void AcceptConnection(const mojo::String& requestor_url, | 37 void AcceptConnection(const mojo::String& requestor_url, |
| 34 mojo::InterfaceRequest<mojo::ServiceProvider> services, | 38 mojo::InterfaceRequest<mojo::ServiceProvider> services, |
| 35 mojo::ServiceProviderPtr exposed_services, | 39 mojo::ServiceProviderPtr exposed_services, |
| 36 const mojo::String& url) override { | 40 const mojo::String& url) override { |
| 41 if (url.get().rfind(".mojo") == url.size() - 5) { | |
|
tonyg
2015/04/14 14:36:57
Maybe just a TODO in this patch, but it seems like
| |
| 42 is_packaged_application_ = true; | |
| 43 } | |
| 37 if (initial_response_) { | 44 if (initial_response_) { |
| 38 OnResponseReceived(mojo::URLLoaderPtr(), services.Pass(), | 45 OnResponseReceived(mojo::URLLoaderPtr(), services.Pass(), |
| 39 exposed_services.Pass(), initial_response_.Pass()); | 46 exposed_services.Pass(), initial_response_.Pass()); |
| 40 } else { | 47 } else { |
| 41 mojo::URLLoaderPtr loader; | 48 mojo::URLLoaderPtr loader; |
| 42 network_service_->CreateURLLoader(mojo::GetProxy(&loader)); | 49 network_service_->CreateURLLoader(mojo::GetProxy(&loader)); |
| 43 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 50 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
| 44 request->url = url; | 51 request->url = url; |
| 45 request->auto_follow_redirects = true; | 52 request->auto_follow_redirects = true; |
| 46 | 53 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 59 void RequestQuit() override { | 66 void RequestQuit() override { |
| 60 mojo::RunLoop::current()->Quit(); | 67 mojo::RunLoop::current()->Quit(); |
| 61 } | 68 } |
| 62 | 69 |
| 63 private: | 70 private: |
| 64 void OnResponseReceived( | 71 void OnResponseReceived( |
| 65 mojo::URLLoaderPtr loader, | 72 mojo::URLLoaderPtr loader, |
| 66 mojo::InterfaceRequest<mojo::ServiceProvider> services, | 73 mojo::InterfaceRequest<mojo::ServiceProvider> services, |
| 67 mojo::ServiceProviderPtr exposed_services, | 74 mojo::ServiceProviderPtr exposed_services, |
| 68 mojo::URLResponsePtr response) { | 75 mojo::URLResponsePtr response) { |
| 69 new DocumentView(services.Pass(), exposed_services.Pass(), response.Pass(), | 76 mojo::URLResponsePtr entrypoint = mojo::URLResponse::New(); |
| 77 if (is_packaged_application_) { | |
| 78 CHECK(unpacked_app_directory_.CreateUniqueTempDir()); | |
| 79 ExtractApplication(response.Pass()); | |
| 80 base::FilePath package_root = unpacked_app_directory_.path(); | |
| 81 | |
| 82 base::FilePath entry_path = package_root.Append("main.sky"); | |
|
tonyg
2015/04/14 14:36:56
Is it a good idea to enforce the "main.sky" name?
| |
| 83 std::string url = "file://" + entry_path.value(); | |
| 84 entrypoint->url = url; | |
| 85 | |
| 86 // TODO(blundell): This should use |mojo::common::CopyFromFile()| on a | |
| 87 // background thread to do the file read asynchronously. | |
| 88 std::string source; | |
| 89 if (!base::ReadFileToString(entry_path, &source)) { | |
| 90 NOTREACHED(); | |
| 91 return; | |
| 92 } | |
| 93 mojo::ScopedDataPipeProducerHandle producer_handle; | |
| 94 mojo::CreateDataPipe(NULL, &producer_handle, &entrypoint->body); | |
| 95 DCHECK(mojo::common::BlockingCopyFromString(source, producer_handle)); | |
| 96 producer_handle.reset(); | |
| 97 } else { | |
| 98 entrypoint = response.Pass(); | |
| 99 } | |
| 100 | |
| 101 new DocumentView(services.Pass(), exposed_services.Pass(), | |
| 102 entrypoint.Pass(), | |
| 70 shell_.get()); | 103 shell_.get()); |
| 71 } | 104 } |
| 72 | 105 |
| 106 // TODO(blundell): The below two functions should be moved into a utility | |
| 107 // file somewhere where they can be shared by this file, the Dart content | |
| 108 // handler, and the Python content handler. | |
| 109 void ExtractApplication(mojo::URLResponsePtr response) { | |
| 110 zip::ZipReader reader; | |
| 111 const std::string input_data = CopyToString(response->body.Pass()); | |
|
tonyg
2015/04/14 14:36:57
This seems worthy of a comment about a background
| |
| 112 CHECK(reader.OpenFromString(input_data)); | |
|
tonyg
2015/04/14 14:36:57
Do we really want to crash upon corrupted .mojo fi
| |
| 113 base::FilePath temp_dir_path = unpacked_app_directory_.path(); | |
| 114 while (reader.HasMore()) { | |
| 115 CHECK(reader.OpenCurrentEntryInZip()); | |
| 116 CHECK(reader.ExtractCurrentEntryIntoDirectory(temp_dir_path)); | |
| 117 CHECK(reader.AdvanceToNextEntry()); | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 std::string CopyToString(mojo::ScopedDataPipeConsumerHandle body) { | |
| 122 std::string body_str; | |
| 123 bool result = mojo::common::BlockingCopyToString(body.Pass(), &body_str); | |
| 124 DCHECK(result); | |
| 125 return body_str; | |
| 126 } | |
| 127 | |
| 73 mojo::StrongBinding<mojo::Application> binding_; | 128 mojo::StrongBinding<mojo::Application> binding_; |
| 74 mojo::ShellPtr shell_; | 129 mojo::ShellPtr shell_; |
| 75 mojo::NetworkServicePtr network_service_; | 130 mojo::NetworkServicePtr network_service_; |
| 76 mojo::URLResponsePtr initial_response_; | 131 mojo::URLResponsePtr initial_response_; |
| 132 bool is_packaged_application_; | |
| 133 base::ScopedTempDir unpacked_app_directory_; | |
| 77 }; | 134 }; |
| 78 | 135 |
| 79 ContentHandlerImpl::ContentHandlerImpl( | 136 ContentHandlerImpl::ContentHandlerImpl( |
| 80 mojo::InterfaceRequest<mojo::ContentHandler> request) | 137 mojo::InterfaceRequest<mojo::ContentHandler> request) |
| 81 : binding_(this, request.Pass()) { | 138 : binding_(this, request.Pass()) { |
| 82 } | 139 } |
| 83 | 140 |
| 84 ContentHandlerImpl::~ContentHandlerImpl() { | 141 ContentHandlerImpl::~ContentHandlerImpl() { |
| 85 } | 142 } |
| 86 | 143 |
| 87 void ContentHandlerImpl::StartApplication( | 144 void ContentHandlerImpl::StartApplication( |
| 88 mojo::InterfaceRequest<mojo::Application> application, | 145 mojo::InterfaceRequest<mojo::Application> application, |
| 89 mojo::URLResponsePtr response) { | 146 mojo::URLResponsePtr response) { |
| 90 new SkyApplication(application.Pass(), response.Pass()); | 147 new SkyApplication(application.Pass(), response.Pass()); |
| 91 } | 148 } |
| 92 | 149 |
| 93 } // namespace sky | 150 } // namespace sky |
| OLD | NEW |