Index: content/renderer/font_access/web_font_access_impl.cc |
diff --git a/content/renderer/font_access/web_font_access_impl.cc b/content/renderer/font_access/web_font_access_impl.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..390709a58d9515cb3db879e0882186a1001b640f |
--- /dev/null |
+++ b/content/renderer/font_access/web_font_access_impl.cc |
@@ -0,0 +1,86 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/renderer/font_access/web_font_access_impl.h" |
+ |
+#include "base/bind.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "content/renderer/render_thread_impl.h" |
+#include "mojo/public/cpp/bindings/lib/buffer.h" |
+#include "third_party/WebKit/public/platform/WebData.h" |
+#include "third_party/WebKit/public/platform/WebPassOwnPtr.h" |
+#include "third_party/WebKit/public/platform/modules/font_access/WebFontAccessDescription.h" |
+ |
+using blink::WebFontAccessDescription; |
+using blink::WebVector; |
+using blink::WebString; |
+ |
+namespace content { |
+ |
+WebFontAccessImpl::WebFontAccessImpl() {} |
+ |
+WebFontAccessImpl::~WebFontAccessImpl() {} |
+ |
+void WebFontAccessImpl::getFonts(blink::FontAccessCallbacks* raw_callbacks) { |
+ scoped_ptr<blink::FontAccessCallbacks> callbacks(raw_callbacks); |
+ // base::Unretained is safe because the Mojo chanel will be deleted and |
+ // will destroy its callbacks before "this" is deleted. |
+ GetService()->GetFontList(base::Bind(&WebFontAccessImpl::FontListHasLoaded, |
+ base::Unretained(this), |
+ base::Passed(&callbacks))); |
+} |
+ |
+void WebFontAccessImpl::getFontWebData( |
+ const WebString& family, |
+ const WebString& style, |
+ blink::FontDataCallbacks* raw_callbacks) { |
+ scoped_ptr<blink::FontDataCallbacks> callbacks(raw_callbacks); |
+ // base::Unretained is safe because the Mojo chanel will be deleted and |
+ // will destroy its callbacks before "this" is deleted. |
+ GetService()->GetFontData( |
+ family.utf8(), style.utf8(), |
+ base::Bind(&WebFontAccessImpl::FontDataHasLoaded, base::Unretained(this), |
+ base::Passed(&callbacks))); |
+} |
+ |
+void WebFontAccessImpl::FontListHasLoaded( |
+ scoped_ptr<blink::FontAccessCallbacks> callbacks, |
+ mojo::Array<FontDescriptionPtr> fonts) { |
+ std::vector<WebFontAccessDescription> results; |
+ for (size_t i = 0; i < fonts.size(); i++) { |
+ results.push_back(WebFontAccessDescription( |
+ WebString::fromLatin1(fonts[i]->family.get()), |
+ WebString::fromLatin1(fonts[i]->style.get()), |
+ WebString::fromLatin1(fonts[i]->full_name.get()))); |
+ } |
+ callbacks->onSuccess( |
+ blink::adoptWebPtr(new WebVector<WebFontAccessDescription>(results))); |
+} |
+ |
+void WebFontAccessImpl::FontDataHasLoaded( |
+ scoped_ptr<blink::FontDataCallbacks> callbacks, |
+ mojo::ScopedSharedBufferHandle handle, |
+ uint64_t size) { |
+ if (size == 0) { |
+ callbacks->onError(); |
+ } |
+ |
+ void* data; |
+ MojoResult success = mojo::MapBuffer(handle.get(), 0, size, &data, |
+ MOJO_READ_DATA_FLAG_ALL_OR_NONE); |
+ if (success != MOJO_RESULT_OK) |
+ callbacks->onError(); |
+ blink::WebData* web_data = new blink::WebData(static_cast<char*>(data), size); |
+ callbacks->onSuccess(blink::adoptWebPtr(web_data)); |
+} |
+ |
+FontAccessServicePtr& WebFontAccessImpl::GetService() { |
+ if (!provider_) { |
+ RenderThread::Get()->GetServiceRegistry()->ConnectToRemoteService( |
+ mojo::GetProxy(&provider_)); |
+ } |
+ return provider_; |
+} |
+ |
+} // namespace content |