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/font_service_app.h"
7 #include "base/files/file.h"
8 #include "base/files/file_path.h"
9 #include "mojo/application/public/cpp/application_connection.h"
10 #include "mojo/platform_handle/platform_handle_functions.h"
12 COMPILE_ASSERT(static_cast<uint32
>(SkTypeface::kNormal
) ==
13 static_cast<uint32
>(font_service::TYPEFACE_STYLE_NORMAL
),
14 typeface_flags_should_match
);
15 COMPILE_ASSERT(static_cast<uint32
>(SkTypeface::kBold
) ==
16 static_cast<uint32
>(font_service::TYPEFACE_STYLE_BOLD
),
17 typeface_flags_should_match
);
18 COMPILE_ASSERT(static_cast<uint32
>(SkTypeface::kItalic
) ==
19 static_cast<uint32
>(font_service::TYPEFACE_STYLE_ITALIC
),
20 typeface_flags_should_match
);
22 static_cast<uint32
>(SkTypeface::kBoldItalic
) ==
23 static_cast<uint32
>(font_service::TYPEFACE_STYLE_BOLD_ITALIC
),
24 typeface_flags_should_match
);
28 mojo::ScopedHandle
GetHandleForPath(const base::FilePath
& path
) {
30 return mojo::ScopedHandle();
32 mojo::ScopedHandle to_pass
;
33 base::File
file(path
, base::File::FLAG_OPEN
| base::File::FLAG_READ
);
34 if (!file
.IsValid()) {
35 LOG(WARNING
) << "file not valid, path=" << path
.value();
36 return mojo::ScopedHandle();
39 MojoHandle mojo_handle
;
40 MojoResult create_result
=
41 MojoCreatePlatformHandleWrapper(file
.TakePlatformFile(), &mojo_handle
);
42 if (create_result
!= MOJO_RESULT_OK
) {
43 LOG(WARNING
) << "unable to create wrapper, path=" << path
.value()
44 << "result=" << create_result
;
45 return mojo::ScopedHandle();
48 return mojo::ScopedHandle(mojo::Handle(mojo_handle
)).Pass();
53 namespace font_service
{
55 FontServiceApp::FontServiceApp() {}
57 FontServiceApp::~FontServiceApp() {}
59 void FontServiceApp::Initialize(mojo::ApplicationImpl
* app
) {}
61 bool FontServiceApp::ConfigureIncomingConnection(
62 mojo::ApplicationConnection
* connection
) {
63 connection
->AddService(this);
67 void FontServiceApp::Create(mojo::ApplicationConnection
* connection
,
68 mojo::InterfaceRequest
<FontService
> request
) {
69 bindings_
.AddBinding(this, request
.Pass());
72 void FontServiceApp::MatchFamilyName(const mojo::String
& family_name
,
73 TypefaceStyle requested_style
,
74 const MatchFamilyNameCallback
& callback
) {
75 SkFontConfigInterface::FontIdentity result_identity
;
76 SkString result_family
;
77 SkTypeface::Style result_style
;
78 SkFontConfigInterface
* fc
=
79 SkFontConfigInterface::GetSingletonDirectInterface();
80 const bool r
= fc
->matchFamilyName(
81 family_name
.data(), static_cast<SkTypeface::Style
>(requested_style
),
82 &result_identity
, &result_family
, &result_style
);
85 callback
.Run(nullptr, "", TYPEFACE_STYLE_NORMAL
);
89 // Stash away the returned path, so we can give it an ID (index)
90 // which will later be given to us in a request to open the file.
91 int index
= FindOrAddPath(result_identity
.fString
);
93 FontIdentityPtr
identity(FontIdentity::New());
94 identity
->id
= static_cast<uint32_t>(index
);
95 identity
->ttc_index
= result_identity
.fTTCIndex
;
96 identity
->str_representation
= result_identity
.fString
.c_str();
98 callback
.Run(identity
.Pass(), result_family
.c_str(),
99 static_cast<TypefaceStyle
>(result_style
));
102 void FontServiceApp::OpenStream(uint32_t id_number
,
103 const OpenStreamCallback
& callback
) {
104 mojo::ScopedHandle handle
;
105 if (id_number
< static_cast<uint32_t>(paths_
.count())) {
107 GetHandleForPath(base::FilePath(paths_
[id_number
]->c_str())).Pass();
110 callback
.Run(handle
.Pass());
113 int FontServiceApp::FindOrAddPath(const SkString
& path
) {
114 int count
= paths_
.count();
115 for (int i
= 0; i
< count
; ++i
) {
116 if (path
== *paths_
[i
])
119 *paths_
.append() = new SkString(path
);
123 } // namespace font_service