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 "content/renderer/font_access/web_font_access_impl.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "content/renderer/render_thread_impl.h" |
| 10 #include "mojo/public/cpp/bindings/lib/buffer.h" |
| 11 #include "third_party/WebKit/public/platform/WebData.h" |
| 12 #include "third_party/WebKit/public/platform/WebPassOwnPtr.h" |
| 13 #include "third_party/WebKit/public/platform/modules/font_access/WebFontAccessDe
scription.h" |
| 14 |
| 15 using blink::WebFontAccessDescription; |
| 16 using blink::WebVector; |
| 17 using blink::WebString; |
| 18 |
| 19 namespace content { |
| 20 |
| 21 WebFontAccessImpl::WebFontAccessImpl() {} |
| 22 |
| 23 WebFontAccessImpl::~WebFontAccessImpl() {} |
| 24 |
| 25 void WebFontAccessImpl::getFonts(blink::FontAccessCallbacks* raw_callbacks) { |
| 26 scoped_ptr<blink::FontAccessCallbacks> callbacks(raw_callbacks); |
| 27 // base::Unretained is safe because the Mojo chanel will be deleted and |
| 28 // will destroy its callbacks before "this" is deleted. |
| 29 GetService()->GetFontList(base::Bind(&WebFontAccessImpl::FontListHasLoaded, |
| 30 base::Unretained(this), |
| 31 base::Passed(&callbacks))); |
| 32 } |
| 33 |
| 34 void WebFontAccessImpl::getFontWebData( |
| 35 const WebString& family, |
| 36 const WebString& style, |
| 37 blink::FontDataCallbacks* raw_callbacks) { |
| 38 scoped_ptr<blink::FontDataCallbacks> callbacks(raw_callbacks); |
| 39 // base::Unretained is safe because the Mojo chanel will be deleted and |
| 40 // will destroy its callbacks before "this" is deleted. |
| 41 GetService()->GetFontData( |
| 42 family.utf8(), style.utf8(), |
| 43 base::Bind(&WebFontAccessImpl::FontDataHasLoaded, base::Unretained(this), |
| 44 base::Passed(&callbacks))); |
| 45 } |
| 46 |
| 47 void WebFontAccessImpl::FontListHasLoaded( |
| 48 scoped_ptr<blink::FontAccessCallbacks> callbacks, |
| 49 mojo::Array<FontDescriptionPtr> fonts) { |
| 50 std::vector<WebFontAccessDescription> results; |
| 51 for (size_t i = 0; i < fonts.size(); i++) { |
| 52 results.push_back(WebFontAccessDescription( |
| 53 WebString::fromLatin1(fonts[i]->family.get()), |
| 54 WebString::fromLatin1(fonts[i]->style.get()), |
| 55 WebString::fromLatin1(fonts[i]->full_name.get()))); |
| 56 } |
| 57 callbacks->onSuccess( |
| 58 blink::adoptWebPtr(new WebVector<WebFontAccessDescription>(results))); |
| 59 } |
| 60 |
| 61 void WebFontAccessImpl::FontDataHasLoaded( |
| 62 scoped_ptr<blink::FontDataCallbacks> callbacks, |
| 63 mojo::ScopedSharedBufferHandle handle, |
| 64 uint64_t size) { |
| 65 if (size == 0) { |
| 66 callbacks->onError(); |
| 67 } |
| 68 |
| 69 void* data; |
| 70 MojoResult success = mojo::MapBuffer(handle.get(), 0, size, &data, |
| 71 MOJO_READ_DATA_FLAG_ALL_OR_NONE); |
| 72 if (success != MOJO_RESULT_OK) |
| 73 callbacks->onError(); |
| 74 blink::WebData* web_data = new blink::WebData(static_cast<char*>(data), size); |
| 75 callbacks->onSuccess(blink::adoptWebPtr(web_data)); |
| 76 } |
| 77 |
| 78 FontAccessServicePtr& WebFontAccessImpl::GetService() { |
| 79 if (!provider_) { |
| 80 RenderThread::Get()->GetServiceRegistry()->ConnectToRemoteService( |
| 81 mojo::GetProxy(&provider_)); |
| 82 } |
| 83 return provider_; |
| 84 } |
| 85 |
| 86 } // namespace content |
OLD | NEW |