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_SERVICE_THREAD_H_
6 #define COMPONENTS_FONT_SERVICE_PUBLIC_CPP_FONT_SERVICE_THREAD_H_
8 #include "base/files/file.h"
9 #include "base/threading/thread.h"
10 #include "components/font_service/public/interfaces/font_service.mojom.h"
11 #include "third_party/skia/include/core/SkStream.h"
12 #include "third_party/skia/include/core/SkTypeface.h"
13 #include "third_party/skia/include/ports/SkFontConfigInterface.h"
15 namespace font_service
{
20 // The thread which services font requests.
22 // The SkFontConfigInterface is a global singleton which can be accessed from
23 // multiple threads. However, mojo pipes are bound to a single thread. Because
24 // of this mismatch, we create a thread which owns the mojo pipe, sends and
25 // receives messages. The multiple threads which call through FontLoader class
26 // do blocking message calls to this thread.
27 class FontServiceThread
: public base::Thread
,
28 public base::RefCountedThreadSafe
<FontServiceThread
> {
30 explicit FontServiceThread(FontServicePtr font_service
);
32 // These methods are proxies which run on your thread, post a blocking task
33 // to the FontServiceThread, and wait on an event signaled from the callback.
34 bool MatchFamilyName(const char family_name
[],
35 SkTypeface::Style requested_style
,
36 SkFontConfigInterface::FontIdentity
* out_font_identity
,
37 SkString
* out_family_name
,
38 SkTypeface::Style
* out_style
);
39 scoped_refptr
<MappedFontFile
> OpenStream(
40 const SkFontConfigInterface::FontIdentity
& identity
);
43 friend class base::RefCountedThreadSafe
<FontServiceThread
>;
44 ~FontServiceThread() override
;
46 // Methods which run on the FontServiceThread. The public MatchFamilyName
47 // calls this method, this method calls the mojo interface, and sets up the
48 // callback to OnMatchFamilyNameComplete.
49 void MatchFamilyNameImpl(
50 base::WaitableEvent
* done_event
,
51 const char family_name
[],
52 SkTypeface::Style requested_style
,
54 SkFontConfigInterface::FontIdentity
* out_font_identity
,
55 SkString
* out_family_name
,
56 SkTypeface::Style
* out_style
);
58 // Called on the FontServiceThread in response to receiving a message from
59 // our MatchFamily mojo IPC. This writes the data returned by mojo, and then
60 // signals |done_event| to wake up the other thread.
61 void OnMatchFamilyNameComplete(
62 base::WaitableEvent
* done_event
,
64 SkFontConfigInterface::FontIdentity
* out_font_identity
,
65 SkString
* out_family_name
,
66 SkTypeface::Style
* out_style
,
67 FontIdentityPtr font_identity
,
68 mojo::String family_name
,
71 // Implementation of OpenStream; same threading restrictions as MatchFamily.
72 void OpenStreamImpl(base::WaitableEvent
* done_event
,
73 base::File
* output_file
,
74 const uint32_t id_number
);
75 void OnOpenStreamComplete(base::WaitableEvent
* done_event
,
76 base::File
* output_file
,
77 mojo::ScopedHandle handle
);
81 void CleanUp() override
;
83 // This member is used to safely pass data from one thread to another. It is
84 // set in the constructor and is consumed in Init().
85 mojo::InterfacePtrInfo
<FontService
> font_service_info_
;
87 // This member is set in Init(). It takes |font_service_info_|, which is
88 // non-thread bound, and binds it to the newly created thread.
89 mojo::InterfacePtr
<FontService
> font_service_
;
91 DISALLOW_COPY_AND_ASSIGN(FontServiceThread
);
94 } // namespace internal
95 } // namespace font_service
97 #endif // COMPONENTS_FONT_SERVICE_PUBLIC_CPP_FONT_SERVICE_THREAD_H_