| 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <string> |
| 6 | 7 |
| 7 #include "base/macros.h" | 8 #include "base/macros.h" |
| 8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 10 #include "base/strings/string_tokenizer.h" | |
| 11 #include "examples/bitmap_uploader/bitmap_uploader.h" | 11 #include "examples/bitmap_uploader/bitmap_uploader.h" |
| 12 #include "mojo/application/application_runner_chromium.h" | 12 #include "mojo/application/application_runner_chromium.h" |
| 13 #include "mojo/application/content_handler_factory.h" | 13 #include "mojo/application/content_handler_factory.h" |
| 14 #include "mojo/common/data_pipe_utils.h" |
| 14 #include "mojo/public/c/system/main.h" | 15 #include "mojo/public/c/system/main.h" |
| 15 #include "mojo/public/cpp/application/application_connection.h" | 16 #include "mojo/public/cpp/application/application_connection.h" |
| 16 #include "mojo/public/cpp/application/application_delegate.h" | 17 #include "mojo/public/cpp/application/application_delegate.h" |
| 17 #include "mojo/public/cpp/application/application_impl.h" | 18 #include "mojo/public/cpp/application/application_impl.h" |
| 18 #include "mojo/public/cpp/application/interface_factory_impl.h" | 19 #include "mojo/public/cpp/application/interface_factory_impl.h" |
| 19 #include "mojo/public/cpp/application/service_provider_impl.h" | 20 #include "mojo/public/cpp/application/service_provider_impl.h" |
| 20 #include "mojo/services/content_handler/public/interfaces/content_handler.mojom.
h" | 21 #include "mojo/services/content_handler/public/interfaces/content_handler.mojom.
h" |
| 21 #include "mojo/services/view_manager/public/cpp/types.h" | 22 #include "mojo/services/view_manager/public/cpp/types.h" |
| 22 #include "mojo/services/view_manager/public/cpp/view.h" | 23 #include "mojo/services/view_manager/public/cpp/view.h" |
| 23 #include "mojo/services/view_manager/public/cpp/view_manager.h" | 24 #include "mojo/services/view_manager/public/cpp/view_manager.h" |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 zoom_percentage_ -= kZoomStep; | 138 zoom_percentage_ -= kZoomStep; |
| 138 } | 139 } |
| 139 | 140 |
| 140 void ZoomToActualSize() { | 141 void ZoomToActualSize() { |
| 141 if (zoom_percentage_ == kDefaultZoomPercentage) | 142 if (zoom_percentage_ == kDefaultZoomPercentage) |
| 142 return; | 143 return; |
| 143 zoom_percentage_ = kDefaultZoomPercentage; | 144 zoom_percentage_ = kDefaultZoomPercentage; |
| 144 } | 145 } |
| 145 | 146 |
| 146 void DecodePNG(URLResponsePtr response) { | 147 void DecodePNG(URLResponsePtr response) { |
| 147 int content_length = GetContentLength(response->headers); | 148 std::string data; |
| 148 scoped_ptr<unsigned char[]> data(new unsigned char[content_length]); | 149 mojo::common::BlockingCopyToString(response->body.Pass(), &data); |
| 149 unsigned char* buf = data.get(); | |
| 150 uint32_t bytes_remaining = content_length; | |
| 151 uint32_t num_bytes = bytes_remaining; | |
| 152 while (bytes_remaining > 0) { | |
| 153 MojoResult result = ReadDataRaw(response->body.get(), buf, &num_bytes, | |
| 154 MOJO_READ_DATA_FLAG_NONE); | |
| 155 if (result == MOJO_RESULT_SHOULD_WAIT) { | |
| 156 Wait(response->body.get(), MOJO_HANDLE_SIGNAL_READABLE, | |
| 157 MOJO_DEADLINE_INDEFINITE, nullptr); | |
| 158 } else if (result == MOJO_RESULT_OK) { | |
| 159 buf += num_bytes; | |
| 160 num_bytes = bytes_remaining -= num_bytes; | |
| 161 } else { | |
| 162 break; | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 bitmap_.reset(new std::vector<unsigned char>); | 150 bitmap_.reset(new std::vector<unsigned char>); |
| 167 gfx::PNGCodec::Decode(static_cast<const unsigned char*>(data.get()), | 151 gfx::PNGCodec::Decode(reinterpret_cast<const unsigned char*>(data.data()), |
| 168 content_length, gfx::PNGCodec::FORMAT_BGRA, | 152 data.length(), gfx::PNGCodec::FORMAT_BGRA, |
| 169 bitmap_.get(), &width_, &height_); | 153 bitmap_.get(), &width_, &height_); |
| 170 } | 154 } |
| 171 | 155 |
| 172 int GetContentLength(const Array<String>& headers) { | |
| 173 for (size_t i = 0; i < headers.size(); ++i) { | |
| 174 base::StringTokenizer t(headers[i], ": ;="); | |
| 175 while (t.GetNext()) { | |
| 176 if (!t.token_is_delim() && t.token() == "Content-Length") { | |
| 177 while (t.GetNext()) { | |
| 178 if (!t.token_is_delim()) | |
| 179 return atoi(t.token().c_str()); | |
| 180 } | |
| 181 } | |
| 182 } | |
| 183 } | |
| 184 return 0; | |
| 185 } | |
| 186 | |
| 187 int width_; | 156 int width_; |
| 188 int height_; | 157 int height_; |
| 189 scoped_ptr<std::vector<unsigned char>> bitmap_; | 158 scoped_ptr<std::vector<unsigned char>> bitmap_; |
| 190 ApplicationImpl* app_; | 159 ApplicationImpl* app_; |
| 191 std::map<View*, EmbedderData*> embedder_for_roots_; | 160 std::map<View*, EmbedderData*> embedder_for_roots_; |
| 192 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_; | 161 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_; |
| 193 uint16_t zoom_percentage_; | 162 uint16_t zoom_percentage_; |
| 194 | 163 |
| 195 DISALLOW_COPY_AND_ASSIGN(PNGView); | 164 DISALLOW_COPY_AND_ASSIGN(PNGView); |
| 196 }; | 165 }; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 220 DISALLOW_COPY_AND_ASSIGN(PNGViewer); | 189 DISALLOW_COPY_AND_ASSIGN(PNGViewer); |
| 221 }; | 190 }; |
| 222 | 191 |
| 223 } // namespace examples | 192 } // namespace examples |
| 224 } // namespace mojo | 193 } // namespace mojo |
| 225 | 194 |
| 226 MojoResult MojoMain(MojoHandle application_request) { | 195 MojoResult MojoMain(MojoHandle application_request) { |
| 227 mojo::ApplicationRunnerChromium runner(new mojo::examples::PNGViewer()); | 196 mojo::ApplicationRunnerChromium runner(new mojo::examples::PNGViewer()); |
| 228 return runner.Run(application_request); | 197 return runner.Run(application_request); |
| 229 } | 198 } |
| OLD | NEW |