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"
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
{
15 void OnGotContentHandlerID(uint32_t content_handler_id
) {}
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
),
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 void FontLoader::Shutdown() {
47 bool FontLoader::matchFamilyName(const char family_name
[],
48 SkTypeface::Style requested
,
49 FontIdentity
* out_font_identifier
,
50 SkString
* out_family_name
,
51 SkTypeface::Style
* out_style
) {
52 return thread_
->MatchFamilyName(family_name
, requested
, out_font_identifier
,
53 out_family_name
, out_style
);
56 SkStreamAsset
* FontLoader::openStream(const FontIdentity
& identity
) {
58 base::AutoLock
lock(lock_
);
59 auto mapped_font_files_it
= mapped_font_files_
.find(identity
.fID
);
60 if (mapped_font_files_it
!= mapped_font_files_
.end())
61 return mapped_font_files_it
->second
->CreateMemoryStream();
64 scoped_refptr
<internal::MappedFontFile
> mapped_font_file
=
65 thread_
->OpenStream(identity
);
66 if (!mapped_font_file
)
69 // Get notified with |mapped_font_file| is destroyed.
70 mapped_font_file
->set_observer(this);
73 base::AutoLock
lock(lock_
);
74 auto mapped_font_files_it
=
75 mapped_font_files_
.insert(std::make_pair(mapped_font_file
->font_id(),
76 mapped_font_file
.get()))
78 return mapped_font_files_it
->second
->CreateMemoryStream();
82 void FontLoader::OnMappedFontFileDestroyed(internal::MappedFontFile
* f
) {
83 base::AutoLock
lock(lock_
);
84 mapped_font_files_
.erase(f
->font_id());
87 } // namespace font_service