Chromium Code Reviews| Index: content/common/fontmgr_messages.cc |
| =================================================================== |
| --- content/common/fontmgr_messages.cc (revision 0) |
| +++ content/common/fontmgr_messages.cc (working copy) |
| @@ -0,0 +1,213 @@ |
| +// Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/common/fontmgr_messages.h" |
| + |
| +#include <string> |
| + |
| +#include "skia/ext/refptr.h" |
| +#include "third_party/skia/include/core/SkDataTable.h" |
| +#include "third_party/skia/include/core/SkStream.h" |
| +#include "third_party/skia/include/core/SkString.h" |
| +#include "third_party/skia/include/ports/SkFontStyle.h" |
| +#include "third_party/skia/include/ports/SkRemotableFontMgr.h" |
| + |
| +namespace IPC { |
| + |
| +// skia::RefPtr<SkDataTable> |
| +void ParamTraits<skia::RefPtr<SkDataTable> >::Write( |
| + Message* m, |
| + const param_type& p) { |
| + if (!p) { |
| + m->WriteInt(-1); |
| + return; |
| + } |
| + |
| + int count = p->count(); |
| + m->WriteInt(count); |
| + for (int i = 0; i < count; ++i) { |
| + m->WriteData(reinterpret_cast<const char *>(p->at(i)), p->atSize(i)); |
|
cpu_(ooo_6.6-7.5)
2014/03/26 20:02:38
we want to avoid this 'bag of bytes' approach to I
|
| + } |
| +} |
| + |
| +bool ParamTraits<skia::RefPtr<SkDataTable> >::Read( |
| + const Message* m, |
| + PickleIterator* iter, |
| + param_type* r) { |
| + int count; |
| + if (!m->ReadInt(iter, &count)) |
| + return false; |
| + |
| + if (count < 0) { |
| + r->clear(); |
| + return true; |
| + } |
| + |
| + if (count == 0) { |
| + *r = skia::AdoptRef(SkDataTable::NewEmpty()); |
| + return true; |
| + } |
| + |
| + SkDataTableBuilder builder(1024); |
|
cpu_(ooo_6.6-7.5)
2014/03/26 20:02:38
is there a limit in count? or the 1024 is just adv
|
| + for (int i = 0; i < count; ++i) { |
| + const char* data; |
| + int len; |
| + if (!m->ReadData(iter, &data, &len)) |
| + return false; |
| + |
| + builder.append(data, len); |
| + } |
| + |
| + *r = skia::AdoptRef(builder.detachDataTable()); |
| + return true; |
| +} |
| + |
| +void ParamTraits<skia::RefPtr<SkDataTable> >::Log( |
| + const param_type& p, |
| + std::string* l) { |
| + l->append(base::StringPrintf("SkDataTable: %d", p->count())); |
| +} |
| + |
| +// skia::RefPtr<SkRemotableFontIdentitySet> |
| +void ParamTraits<skia::RefPtr<SkRemotableFontIdentitySet> >::Write( |
| + Message* m, |
| + const param_type& p) { |
| + if (!p) { |
| + m->WriteInt(-1); |
| + return; |
| + } |
| + |
| + int count = p->count(); |
| + const void* data = reinterpret_cast<const void*>(&p->at(0)); |
| + int length = static_cast<int>(count * sizeof(SkFontIdentity)); |
| + count >= 0 && m->WriteInt(count) && m->WriteBytes(data, length); |
|
cpu_(ooo_6.6-7.5)
2014/03/26 20:02:38
we don't use that style in chrome afaik.
|
| +} |
| + |
| +bool ParamTraits<skia::RefPtr<SkRemotableFontIdentitySet> >::Read( |
| + const Message* m, |
| + PickleIterator* iter, |
| + param_type* r) { |
| + int count; |
| + if (!m->ReadInt(iter, &count)) |
| + return false; |
| + |
| + if (count < 0) { |
|
cpu_(ooo_6.6-7.5)
2014/03/26 20:02:38
count = -1 ?
|
| + r->clear(); |
| + return true; |
| + } |
| + |
| + int length = static_cast<int>(count * sizeof(SkFontIdentity)); |
|
cpu_(ooo_6.6-7.5)
2014/03/26 20:02:38
potential integer overflow at the * step.
|
| + const char* data; |
| + if (!m->ReadBytes(iter, &data, length)) |
| + return false; |
| + |
| + if (count == 0) { |
| + *r = skia::AdoptRef(SkRemotableFontIdentitySet::NewEmpty()); |
| + return true; |
| + } |
| + |
| + SkFontIdentity* fontIds = NULL; |
| + *r = skia::AdoptRef(new SkRemotableFontIdentitySet(count, &fontIds)); |
| + memcpy(fontIds, data, length); |
| + return true; |
| +} |
| + |
| +void ParamTraits<skia::RefPtr<SkRemotableFontIdentitySet> >::Log( |
| + const param_type& p, |
| + std::string* l) { |
| + l->append("SkRemotableFontIdentitySet"); |
| + for (int i = 0; i < p->count(); ++i) { |
| + LogParam(p->at(i), l); |
| + } |
| +} |
| + |
| +// skia::RefPtr<SkStreamAsset> |
| +void ParamTraits<skia::RefPtr<SkStreamAsset> >::Write( |
| + Message* m, |
| + const param_type& p) { |
| + if (!p) { |
| + m->WriteInt(-1); |
| + return; |
| + } |
| + |
| + if (!p->hasLength()) { |
| + m->WriteInt(0) && m->WriteBytes(NULL, 0); |
|
cpu_(ooo_6.6-7.5)
2014/03/26 20:02:38
same comment here of line 84 and elsewhere.
|
| + return; |
| + } |
| + |
| + int length = p->getLength(); |
| + const void* data = p->getMemoryBase(); |
| + if (data) { |
| + length >= 0 && m->WriteInt(length) && m->WriteBytes(data, length); |
| + return; |
| + } |
| + |
| + // TODO(bungeman): avoid this silly copy? |
| + void* copy = malloc(length); |
| + p->read(copy, length); |
| + length >= 0 && m->WriteInt(length) && m->WriteBytes(data, length); |
| + free(copy); |
| +} |
| + |
| +bool ParamTraits<skia::RefPtr<SkStreamAsset> >::Read( |
| + const Message* m, |
| + PickleIterator* iter, |
| + param_type* r) { |
| + int length; |
| + if (!m->ReadInt(iter, &length)) |
| + return false; |
| + |
| + if (length < 0) { |
| + r->clear(); |
| + return true; |
| + } |
| + |
| + const char* data; |
| + if (!m->ReadBytes(iter, &data, length)) |
| + return false; |
| + |
|
cpu_(ooo_6.6-7.5)
2014/03/26 20:02:38
same comment as line 30.
Since the fonts are sent
|
| + *r = skia::AdoptRef(new SkMemoryStream(data, length, true)); |
| + return true; |
| +} |
| + |
| +void ParamTraits<skia::RefPtr<SkStreamAsset> >::Log( |
| + const param_type& p, |
| + std::string* l) { |
| + l->append(base::StringPrintf("SkStreamAsset: %d", p->getLength())); |
| +} |
| + |
| +// SkFontStyle |
| +void ParamTraits<SkFontStyle>::Write(Message* m, const param_type& p) { |
| + m->WriteInt(p.weight()); |
| + m->WriteInt(p.width()); |
| + m->WriteInt(p.slant()); |
| +} |
| + |
| +bool ParamTraits<SkFontStyle>::Read( |
| + const Message* m, |
| + PickleIterator* iter, |
| + param_type* r) { |
| + int weight; |
| + if (!m->ReadInt(iter, &weight)) |
| + return false; |
| + |
| + int width; |
| + if (!m->ReadInt(iter, &width)) |
| + return false; |
| + |
| + int slant; |
| + if (!m->ReadInt(iter, &slant)) |
| + return false; |
| + |
| + *r = SkFontStyle(weight, width, (SkFontStyle::Slant)slant); |
| + return true; |
| +} |
| + |
| +void ParamTraits<SkFontStyle>::Log(const param_type& p, std::string* l) { |
| + l->append(base::StringPrintf( |
| + "SkFontStyle: weight:%d width:%d slant:%d", |
| + p.weight(), p.width(), static_cast<int>(p.slant()))); |
| +} |
| + |
| +} // namespace IPC |