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 #ifndef COMPONENTS_FONT_SERVICE_PUBLIC_CPP_FONT_LOADER_H_
6 #define COMPONENTS_FONT_SERVICE_PUBLIC_CPP_FONT_LOADER_H_
8 #include "base/containers/hash_tables.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/synchronization/lock.h"
11 #include "components/font_service/public/cpp/mapped_font_file.h"
12 #include "components/font_service/public/interfaces/font_service.mojom.h"
13 #include "third_party/skia/include/core/SkStream.h"
14 #include "third_party/skia/include/core/SkTypeface.h"
15 #include "third_party/skia/include/ports/SkFontConfigInterface.h"
18 class ApplicationImpl
;
22 namespace font_service
{
24 class FontServiceThread
;
27 // FontConfig implementation for Skia which proxies to the font service to get
28 // out of the sandbox. This methods of this class (as imposed by blink
29 // requirements) may be called on any thread. (Because of this restriction,
30 // also see the FontServiceThread class.)
32 // This is the mojo equivalent to content/common/font_config_ipc_linux.h
33 class FontLoader
: public SkFontConfigInterface
,
34 public internal::MappedFontFile::Observer
{
36 explicit FontLoader(mojo::Shell
* shell
);
37 explicit FontLoader(mojo::ApplicationImpl
* application_impl
);
38 ~FontLoader() override
;
40 // Shuts down the background thread.
43 // SkFontConfigInterface:
44 bool matchFamilyName(const char family_name
[],
45 SkTypeface::Style requested
,
46 FontIdentity
* out_font_identifier
,
47 SkString
* out_family_name
,
48 SkTypeface::Style
* out_style
) override
;
49 SkStreamAsset
* openStream(const FontIdentity
& identity
) override
;
52 // internal::MappedFontFile::Observer:
53 void OnMappedFontFileDestroyed(internal::MappedFontFile
* f
) override
;
55 // Thread to own the mojo message pipe. Because FontLoader can be called on
56 // multiple threads, we create a dedicated thread to send and receive mojo
58 scoped_refptr
<internal::FontServiceThread
> thread_
;
60 // Lock preventing multiple threads from opening font file and accessing
61 // |mapped_font_files_| map at the same time.
64 // Maps font identity ID to the memory-mapped file with font data.
65 base::hash_map
<uint32_t, internal::MappedFontFile
*> mapped_font_files_
;
67 DISALLOW_COPY_AND_ASSIGN(FontLoader
);
70 } // namespace font_service
72 #endif // COMPONENTS_FONT_SERVICE_PUBLIC_CPP_FONT_LOADER_H_