Roll src/third_party/WebKit 3aea697:d9c6159 (svn 201973:201974)
[chromium-blink-merge.git] / components / font_service / public / cpp / font_loader.cc
blobc7886a77727c7cf65f531f263462113b1888c1a3
1 // Copyright 2015 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.
5 #include "components/font_service/public/cpp/font_loader.h"
7 #include "base/bind.h"
8 #include "components/font_service/public/cpp/font_service_thread.h"
9 #include "mojo/application/public/cpp/application_impl.h"
10 #include "mojo/application/public/cpp/connect.h"
11 #include "mojo/application/public/interfaces/shell.mojom.h"
13 namespace font_service {
14 namespace {
15 void OnGotContentHandlerID(uint32_t content_handler_id) {}
16 } // namespace
18 FontLoader::FontLoader(mojo::Shell* shell) {
19 mojo::ServiceProviderPtr font_service_provider;
20 mojo::URLRequestPtr request(mojo::URLRequest::New());
21 request->url = mojo::String::From("mojo:font_service");
22 FontServicePtr font_service;
23 shell->ConnectToApplication(request.Pass(), GetProxy(&font_service_provider),
24 nullptr, nullptr,
25 base::Bind(&OnGotContentHandlerID));
26 mojo::ConnectToService(font_service_provider.get(), &font_service);
28 thread_ = new internal::FontServiceThread(font_service.Pass());
31 FontLoader::FontLoader(mojo::ApplicationImpl* application_impl) {
32 mojo::URLRequestPtr request(mojo::URLRequest::New());
33 request->url = mojo::String::From("mojo:font_service");
34 FontServicePtr font_service;
35 application_impl->ConnectToService(request.Pass(), &font_service);
37 thread_ = new internal::FontServiceThread(font_service.Pass());
40 FontLoader::~FontLoader() {}
42 bool FontLoader::matchFamilyName(const char family_name[],
43 SkTypeface::Style requested,
44 FontIdentity* out_font_identifier,
45 SkString* out_family_name,
46 SkTypeface::Style* out_style) {
47 return thread_->MatchFamilyName(family_name, requested, out_font_identifier,
48 out_family_name, out_style);
51 SkStreamAsset* FontLoader::openStream(const FontIdentity& identity) {
53 base::AutoLock lock(lock_);
54 auto mapped_font_files_it = mapped_font_files_.find(identity.fID);
55 if (mapped_font_files_it != mapped_font_files_.end())
56 return mapped_font_files_it->second->CreateMemoryStream();
59 scoped_refptr<internal::MappedFontFile> mapped_font_file =
60 thread_->OpenStream(identity);
61 if (!mapped_font_file)
62 return nullptr;
64 // Get notified with |mapped_font_file| is destroyed.
65 mapped_font_file->set_observer(this);
68 base::AutoLock lock(lock_);
69 auto mapped_font_files_it =
70 mapped_font_files_.insert(std::make_pair(mapped_font_file->font_id(),
71 mapped_font_file.get()))
72 .first;
73 return mapped_font_files_it->second->CreateMemoryStream();
77 void FontLoader::OnMappedFontFileDestroyed(internal::MappedFontFile* f) {
78 base::AutoLock lock(lock_);
79 mapped_font_files_.erase(f->font_id());
82 } // namespace font_service