Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(949)

Side by Side Diff: content/browser/font_access/font_access_service_impl.cc

Issue 1615133002: Implement API for accessing fonts installed locally on the system. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: OWNERS Mojo. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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.
4 #include "content/browser/font_access/font_access_service_impl.h"
5
6 #include "base/files/file.h"
7 #include "base/files/file_util.h"
8 #include "base/sequenced_task_runner.h"
9 #include "content/browser/font_access/font_getter.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "mojo/public/cpp/system/buffer.h"
12 #include "net/base/mime_util.h"
13
14 namespace content {
15 FontAccessServiceImpl::FontAccessServiceImpl(
16 base::Callback<scoped_ptr<FontCacheMap>(void)> getter,
17 mojo::InterfaceRequest<FontAccessService> request)
18 : binding_(this, std::move(request)),
19 cache_map_getter_(getter),
20 weak_factory_(this) {}
21
22 FontAccessServiceImpl::~FontAccessServiceImpl() {}
23
24 void FontAccessServiceImpl::GetFontList(const GetFontListCallback& callback) {
25 ClobberCacheAndReply(base::Bind(&FontAccessServiceImpl::GetFontListFromCache,
26 weak_factory_.GetWeakPtr(), callback));
27 }
28
29 void FontAccessServiceImpl::GetFontData(const mojo::String& family,
30 const mojo::String& style,
31 const GetFontDataCallback& callback) {
32 std::string path = FindPathInCache(family.get(), style.get());
33 // If service doesn't exist, or if the cache missed.
34 if (path.empty()) {
35 ClobberCacheAndReply(base::Bind(&FontAccessServiceImpl::GetFontDataInternal,
36 weak_factory_.GetWeakPtr(), family.get(),
37 style.get(), callback));
38 return;
39 }
40
41 BrowserThread::PostTaskAndReplyWithResult(
42 BrowserThread::FILE, FROM_HERE,
43 base::Bind(&FontAccessServiceImpl::GetFileInfo, path),
44 base::Bind(&FontAccessServiceImpl::GetFileDataCallback,
45 weak_factory_.GetWeakPtr(), callback, path));
46 }
47
48 void FontAccessServiceImpl::Create(
49 mojo::InterfaceRequest<FontAccessService> request) {
50 new FontAccessServiceImpl(base::Bind(&content::GetFontCacheMap),
51 std::move(request));
52 }
53
54 FontAccessServiceImpl::BlobInfo::BlobInfo() : size(0) {}
55 FontAccessServiceImpl::BlobInfo::~BlobInfo() {}
56
57 void FontAccessServiceImpl::GetFontListFromCache(
58 const GetFontListCallback& callback) {
59 FontCacheMap::iterator iter;
60 mojo::Array<FontDescriptionPtr> fontInfos(0);
61 for (iter = font_cache_map_->begin(); iter != font_cache_map_->end();
62 ++iter) {
63 FontStyleMap::iterator faceIter;
64 for (faceIter = iter->second.begin(); faceIter != iter->second.end();
65 ++faceIter) {
66 FontDescriptionPtr fontPtr =
67 FontDescription::New();
68 fontPtr->family = iter->first;
69 fontPtr->style = faceIter->first;
70 fontPtr->fullName = faceIter->second.fullName;
71 fontInfos.push_back(std::move(fontPtr));
72 }
73 }
74 callback.Run(std::move(fontInfos));
75 }
76
77 void FontAccessServiceImpl::GetFontDataInternal(
78 const std::string& family,
79 const std::string& style,
80 const GetFontDataCallback& callback) {
81 std::string path = FindPathInCache(family, style);
82 if (path == "") {
83 mojo::SharedBuffer buffer(0);
84 callback.Run(std::move(buffer.handle), 0);
85 return;
86 }
87
88 BrowserThread::PostTaskAndReplyWithResult(
89 BrowserThread::FILE, FROM_HERE,
90 base::Bind(&FontAccessServiceImpl::GetFileInfo, path),
91 base::Bind(&FontAccessServiceImpl::GetFileDataCallback,
92 weak_factory_.GetWeakPtr(), callback, path));
93 }
94
95 std::string FontAccessServiceImpl::FindPathInCache(
96 const std::string& family,
97 const std::string& style) {
98 if (!font_cache_map_.get())
99 return std::string();
100
101 FontCacheMap::iterator iter;
102 iter = font_cache_map_->find(family);
103 if (iter != font_cache_map_->end()) {
104 FontStyleMap::iterator faceIter;
105 faceIter = iter->second.find(style);
106 if (faceIter != iter->second.end()) {
107 return faceIter->second.path;
108 }
109 }
110 return std::string();
111 }
112
113 void FontAccessServiceImpl::ClobberCacheAndReply(const base::Closure closure) {
114 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
115 scoped_refptr<base::SequencedTaskRunner> tr = pool->GetSequencedTaskRunner(
116 pool->GetNamedSequenceToken(std::string(kFontAccessToken)));
117 base::PostTaskAndReplyWithResult(
118 tr.get(), FROM_HERE, cache_map_getter_,
119 base::Bind(&FontAccessServiceImpl::SwapCacheAndReply,
120 weak_factory_.GetWeakPtr(), closure));
121 }
122
123 void FontAccessServiceImpl::SwapCacheAndReply(const base::Closure closure,
124 scoped_ptr<FontCacheMap> cache) {
125 font_cache_map_.swap(cache);
126 closure.Run();
127 }
128
129 // static
130 FontAccessServiceImpl::BlobInfo FontAccessServiceImpl::GetFileInfo(
131 const std::string path) {
132 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
133 base::FilePath filepath(base::FilePath::FromUTF8Unsafe(path));
134 base::File::Info info;
135 base::GetFileInfo(filepath, &info);
136
137 BlobInfo realInfo;
138 realInfo.size = info.size;
139 realInfo.last_modified = info.last_modified;
140 net::GetMimeTypeFromFile(filepath, &realInfo.mime);
141
142 return realInfo;
143 }
144
145 // static
146 mojo::ScopedSharedBufferHandle FontAccessServiceImpl::GetSharedBuffer(
147 const std::string path,
148 uint64_t size) {
149 mojo::SharedBuffer buffer(size);
150 base::FilePath filepath(base::FilePath::FromUTF8Unsafe(path));
151 base::File file(filepath, base::File::FLAG_OPEN | base::File::FLAG_READ);
152 void* data;
153 mojo::MapBuffer(buffer.handle.get(), 0, size, &data,
154 MOJO_MAP_BUFFER_FLAG_NONE);
155 file.Read(0, static_cast<char*>(data), size);
156
157 return std::move(buffer.handle);
158 }
159
160 void FontAccessServiceImpl::GetFileDataCallback(
161 const FontAccessService::GetFontDataCallback& callback,
162 const std::string& path,
163 BlobInfo info) {
164 BrowserThread::PostTaskAndReplyWithResult(
165 BrowserThread::FILE, FROM_HERE,
166 base::Bind(&FontAccessServiceImpl::GetSharedBuffer, path, info.size),
167 base::Bind(&FontAccessServiceImpl::PassBufferAndSize, callback,
168 info.size));
169 }
170
171 void FontAccessServiceImpl::PassBufferAndSize(
172 const FontAccessService::GetFontDataCallback& callback,
173 uint64_t size,
174 mojo::ScopedSharedBufferHandle handle) {
175 callback.Run(std::move(handle), size);
176 }
177
178 } // content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698