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

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: 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"
nasko 2016/02/12 22:05:18 Empty line between the comment and the first inclu
Daniel Nishi 2016/02/12 22:49:39 Done.
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(
nasko 2016/02/12 22:05:17 Empty line after namespace and before the construc
Daniel Nishi 2016/02/12 22:49:39 Done.
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,
nasko 2016/02/12 22:05:18 What's the point of having a cache if every call t
Daniel Nishi 2016/02/12 22:49:39 The list itself may grow stale, but the cache allo
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() {}
nasko 2016/02/12 22:05:17 Empty line between the two methods.
Daniel Nishi 2016/02/12 22:49:39 Done.
56
57 void FontAccessServiceImpl::GetFontListFromCache(
58 const GetFontListCallback& callback) {
59 FontCacheMap::iterator iter;
60 mojo::Array<FontDescriptionPtr> font_infos(0);
61 for (auto& entry : *font_cache_map_) {
nasko 2016/02/12 22:05:18 font_cache_map_.get()
Daniel Nishi 2016/02/12 22:49:39 The operator* does a .get() and dereferences it. I
62 for (auto& face_entry : entry.second) {
63 FontDescriptionPtr font_ptr =
64 FontDescription::New();
65 font_ptr->family = entry.first;
66 font_ptr->style = face_entry.first;
67 font_ptr->fullName = face_entry.second.full_name;
68 font_infos.push_back(std::move(font_ptr));
69 }
70 }
71 callback.Run(std::move(font_infos));
72 }
73
74 void FontAccessServiceImpl::GetFontDataInternal(
75 const std::string& family,
76 const std::string& style,
77 const GetFontDataCallback& callback) {
78 std::string path = FindPathInCache(family, style);
79 if (path == "") {
80 callback.Run(mojo::ScopedHandleBase<mojo::SharedBufferHandle>(), 0);
81 return;
82 }
83
84 BrowserThread::PostTaskAndReplyWithResult(
85 BrowserThread::FILE, FROM_HERE,
86 base::Bind(&FontAccessServiceImpl::GetFileInfo, path),
87 base::Bind(&FontAccessServiceImpl::GetFileDataCallback,
88 weak_factory_.GetWeakPtr(), callback, path));
89 }
90
91 std::string FontAccessServiceImpl::FindPathInCache(
nasko 2016/02/12 22:05:17 Why is a method looking for a path returning a std
Daniel Nishi 2016/02/12 22:49:39 base::FilePath is now used.
92 const std::string& family,
93 const std::string& style) {
94 if (!font_cache_map_.get())
95 return std::string();
96
97 FontCacheMap::iterator iter;
98 iter = font_cache_map_->find(family);
99 if (iter != font_cache_map_->end()) {
100 FontStyleMap::iterator faceIter;
101 faceIter = iter->second.find(style);
102 if (faceIter != iter->second.end()) {
103 return faceIter->second.path;
104 }
105 }
106 return std::string();
107 }
108
109 void FontAccessServiceImpl::ClobberCacheAndReply(const base::Closure closure) {
110 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
111 scoped_refptr<base::SequencedTaskRunner> tr = pool->GetSequencedTaskRunner(
112 pool->GetNamedSequenceToken(std::string(kFontAccessToken)));
113 base::PostTaskAndReplyWithResult(
114 tr.get(), FROM_HERE, cache_map_getter_,
115 base::Bind(&FontAccessServiceImpl::SwapCacheAndReply,
116 weak_factory_.GetWeakPtr(), closure));
117 }
118
119 void FontAccessServiceImpl::SwapCacheAndReply(const base::Closure closure,
120 scoped_ptr<FontCacheMap> cache) {
121 font_cache_map_.swap(cache);
122 closure.Run();
123 }
124
125 // static
126 FontAccessServiceImpl::BlobInfo FontAccessServiceImpl::GetFileInfo(
127 const std::string& path) {
128 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
129 base::FilePath filepath(base::FilePath::FromUTF8Unsafe(path));
130 base::File::Info info;
131 BlobInfo realInfo;
nasko 2016/02/12 22:05:17 real_info
Daniel Nishi 2016/02/12 22:49:39 Done.
132 realInfo.is_valid = base::GetFileInfo(filepath, &info);
133 if (!realInfo.is_valid) {
134 return realInfo;
nasko 2016/02/12 22:05:18 style: Indent only two spaces. One line if stateme
Daniel Nishi 2016/02/12 22:49:39 Done.
135 }
136
137 realInfo.size = info.size;
138 realInfo.last_modified = info.last_modified;
139 net::GetMimeTypeFromFile(filepath, &realInfo.mime);
140
141 return realInfo;
142 }
143
144 // static
145 mojo::ScopedSharedBufferHandle FontAccessServiceImpl::GetSharedBuffer(
146 const std::string& path,
147 uint64_t size) {
148 mojo::SharedBuffer buffer(size);
149 base::FilePath filepath(base::FilePath::FromUTF8Unsafe(path));
150 base::File file(filepath, base::File::FLAG_OPEN | base::File::FLAG_READ);
151 void* data;
152 mojo::MapBuffer(buffer.handle.get(), 0, size, &data,
153 MOJO_MAP_BUFFER_FLAG_NONE);
154 bool valid_read = file.Read(0, static_cast<char*>(data), size);
155 if (!valid_read) {
156 return mojo::ScopedHandleBase<mojo::SharedBufferHandle>();
nasko 2016/02/12 22:05:18 style: Indent only two spaces. One line if stateme
Daniel Nishi 2016/02/12 22:49:39 Done.
157 }
158
159
nasko 2016/02/12 22:05:18 No need for two empty lines, one is fine.
Daniel Nishi 2016/02/12 22:49:39 Done.
160 return std::move(buffer.handle);
161 }
162
163 void FontAccessServiceImpl::GetFileDataCallback(
164 const FontAccessService::GetFontDataCallback& callback,
165 const std::string& path,
166 BlobInfo info) {
167 if (!info.is_valid) {
168 callback.Run(mojo::ScopedHandleBase<mojo::SharedBufferHandle>(), 0);
nasko 2016/02/12 22:05:18 style: Indent only two spaces.
Daniel Nishi 2016/02/12 22:49:39 Done.
169 return;
170 }
171
172 BrowserThread::PostTaskAndReplyWithResult(
173 BrowserThread::FILE, FROM_HERE,
174 base::Bind(&FontAccessServiceImpl::GetSharedBuffer, path, info.size),
175 base::Bind(&FontAccessServiceImpl::PassBufferAndSize, callback,
176 info.size));
177 }
178
179 void FontAccessServiceImpl::PassBufferAndSize(
180 const FontAccessService::GetFontDataCallback& callback,
181 uint64_t size,
182 mojo::ScopedSharedBufferHandle handle) {
183 callback.Run(std::move(handle), size);
184 }
185
186 } // content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698