| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "blimp/engine/mojo/font_fetcher_mojo_impl.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 #include <memory> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "blimp/engine/browser/font_data_fetcher.h" | |
| 14 #include "mojo/public/cpp/system/buffer.h" | |
| 15 #include "third_party/skia/include/core/SkStream.h" | |
| 16 | |
| 17 namespace blimp { | |
| 18 namespace engine { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 void ReturnFontRequestResponse( | |
| 23 const FontFetcherMojoImpl::GetFontStreamCallback& callback, | |
| 24 std::unique_ptr<SkStream> font_stream) { | |
| 25 uint32_t font_stream_size = font_stream->getLength(); | |
| 26 if (font_stream_size == 0) { | |
| 27 LOG(WARNING) << "Font data not available."; | |
| 28 callback.Run(mojo::ScopedSharedBufferHandle(), 0); | |
| 29 return; | |
| 30 } | |
| 31 | |
| 32 mojo::ScopedSharedBufferHandle handle = | |
| 33 mojo::SharedBufferHandle::Create(font_stream_size); | |
| 34 if (!handle.is_valid()) { | |
| 35 LOG(ERROR) << "Failed to create SharedBufferHandle of " << font_stream_size | |
| 36 << " bytes."; | |
| 37 callback.Run(mojo::ScopedSharedBufferHandle(), 0); | |
| 38 return; | |
| 39 } | |
| 40 | |
| 41 mojo::ScopedSharedBufferMapping mapping = handle->Map(font_stream_size); | |
| 42 if (!mapping) { | |
| 43 LOG(ERROR) << "Failed to mmap region of " << font_stream_size << " bytes."; | |
| 44 callback.Run(mojo::ScopedSharedBufferHandle(), 0); | |
| 45 return; | |
| 46 } | |
| 47 | |
| 48 uint32_t bytes_read = font_stream->read(mapping.get(), font_stream_size); | |
| 49 DCHECK_EQ(bytes_read, font_stream_size); | |
| 50 if (bytes_read < font_stream_size) { | |
| 51 LOG(ERROR) << "SKStream read failed. Read " << bytes_read | |
| 52 << " bytes, should read " << font_stream_size << "bytes"; | |
| 53 callback.Run(mojo::ScopedSharedBufferHandle(), 0); | |
| 54 return; | |
| 55 } | |
| 56 | |
| 57 callback.Run(handle->Clone(mojo::SharedBufferHandle::AccessMode::READ_ONLY), | |
| 58 font_stream_size); | |
| 59 } | |
| 60 | |
| 61 } // namespace | |
| 62 | |
| 63 FontFetcherMojoImpl::FontFetcherMojoImpl(FontDataFetcher* font_data_fetcher) | |
| 64 : font_data_fetcher_(font_data_fetcher) { | |
| 65 DCHECK(font_data_fetcher_); | |
| 66 } | |
| 67 | |
| 68 FontFetcherMojoImpl::~FontFetcherMojoImpl() {} | |
| 69 | |
| 70 void FontFetcherMojoImpl::GetFontStream(const std::string& font_hash, | |
| 71 const GetFontStreamCallback& callback) { | |
| 72 VLOG(1) << "FontFetcherIPC::GetFontStream called for font_hash " | |
| 73 << font_hash; | |
| 74 | |
| 75 font_data_fetcher_->FetchFontStream( | |
| 76 font_hash, base::Bind(&ReturnFontRequestResponse, callback)); | |
| 77 } | |
| 78 | |
| 79 void FontFetcherMojoImpl::BindRequest( | |
| 80 mojo::InterfaceRequest<mojom::FontFetcher> request) { | |
| 81 bindings_.AddBinding(this, std::move(request)); | |
| 82 } | |
| 83 | |
| 84 } // namespace engine | |
| 85 } // namespace blimp | |
| OLD | NEW |