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/mapped_font_file.h"
7 #include "base/files/file_util.h"
8 #include "base/threading/thread_restrictions.h"
9 #include "skia/ext/refptr.h"
10 #include "skia/ext/skia_utils_base.h"
11 #include "third_party/skia/include/core/SkData.h"
12 #include "third_party/skia/include/core/SkStream.h"
14 namespace font_service
{
17 MappedFontFile::MappedFontFile(uint32_t font_id
)
18 : font_id_(font_id
), observer_(nullptr) {}
20 bool MappedFontFile::Initialize(base::File file
) {
21 base::ThreadRestrictions::ScopedAllowIO allow_mmap
;
22 return mapped_font_file_
.Initialize(file
.Pass());
25 SkMemoryStream
* MappedFontFile::CreateMemoryStream() {
26 DCHECK(mapped_font_file_
.IsValid());
27 auto data
= skia::AdoptRef(
28 SkData::NewWithProc(mapped_font_file_
.data(), mapped_font_file_
.length(),
29 &MappedFontFile::ReleaseProc
, this));
33 return new SkMemoryStream(data
.get());
36 MappedFontFile::~MappedFontFile() {
38 observer_
->OnMappedFontFileDestroyed(this);
42 void MappedFontFile::ReleaseProc(const void* ptr
, void* context
) {
43 base::ThreadRestrictions::ScopedAllowIO allow_munmap
;
44 static_cast<MappedFontFile
*>(context
)->Release();
47 } // namespace internal
48 } // namespace font_service