| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "content/test/dwrite_font_fake_sender_win.h" | |
| 6 | |
| 7 #include <shlobj.h> | |
| 8 | |
| 9 #include "content/common/dwrite_font_proxy_messages.h" | |
| 10 | |
| 11 namespace content { | |
| 12 | |
| 13 void AddFamily(const base::string16& font_path, | |
| 14 const base::string16& family_name, | |
| 15 const base::string16& base_family_name, | |
| 16 FakeFontCollection* collection) { | |
| 17 collection->AddFont(family_name) | |
| 18 .AddFamilyName(L"en-us", family_name) | |
| 19 .AddFilePath(font_path + L"\\" + base_family_name + L".ttf") | |
| 20 .AddFilePath(font_path + L"\\" + base_family_name + L"bd.ttf") | |
| 21 .AddFilePath(font_path + L"\\" + base_family_name + L"bi.ttf") | |
| 22 .AddFilePath(font_path + L"\\" + base_family_name + L"i.ttf"); | |
| 23 } | |
| 24 | |
| 25 IPC::Sender* CreateFakeCollectionSender() { | |
| 26 std::vector<base::char16> font_path_chars; | |
| 27 font_path_chars.resize(MAX_PATH); | |
| 28 SHGetSpecialFolderPath(nullptr /* hwndOwner - reserved */, | |
| 29 font_path_chars.data(), CSIDL_FONTS, | |
| 30 FALSE /* fCreate */); | |
| 31 base::string16 font_path(font_path_chars.data()); | |
| 32 scoped_refptr<FakeFontCollection> fake_collection = new FakeFontCollection(); | |
| 33 AddFamily(font_path, L"Arial", L"arial", fake_collection.get()); | |
| 34 AddFamily(font_path, L"Courier New", L"cour", fake_collection.get()); | |
| 35 AddFamily(font_path, L"Times New Roman", L"times", fake_collection.get()); | |
| 36 return fake_collection->GetSender(); | |
| 37 } | |
| 38 | |
| 39 FakeFont::FakeFont(const base::string16& name) : font_name_(name) {} | |
| 40 | |
| 41 FakeFont::~FakeFont() = default; | |
| 42 | |
| 43 FakeFontCollection::FakeFontCollection() = default; | |
| 44 | |
| 45 FakeFont& FakeFontCollection::AddFont(const base::string16& font_name) { | |
| 46 fonts_.emplace_back(font_name); | |
| 47 return fonts_.back(); | |
| 48 } | |
| 49 | |
| 50 size_t FakeFontCollection::MessageCount() { | |
| 51 return messages_.size(); | |
| 52 } | |
| 53 | |
| 54 IPC::Message* FakeFontCollection::GetMessage(size_t index) { | |
| 55 return messages_[index].get(); | |
| 56 } | |
| 57 | |
| 58 IPC::Sender* FakeFontCollection::GetSender() { | |
| 59 return new FakeSender(this, false /* track_messages */); | |
| 60 } | |
| 61 | |
| 62 IPC::Sender* FakeFontCollection::GetTrackingSender() { | |
| 63 return new FakeSender(this, true /* track_messages */); | |
| 64 } | |
| 65 | |
| 66 FakeFontCollection::ReplySender::ReplySender(FakeFontCollection* collection) | |
| 67 : collection_(collection) {} | |
| 68 | |
| 69 FakeFontCollection::ReplySender::~ReplySender() = default; | |
| 70 | |
| 71 scoped_ptr<IPC::Message>& FakeFontCollection::ReplySender::OnMessageReceived( | |
| 72 const IPC::Message& msg) { | |
| 73 reply_.reset(); | |
| 74 bool handled = true; | |
| 75 IPC_BEGIN_MESSAGE_MAP(ReplySender, msg) | |
| 76 IPC_MESSAGE_HANDLER(DWriteFontProxyMsg_FindFamily, OnFindFamily) | |
| 77 IPC_MESSAGE_HANDLER(DWriteFontProxyMsg_GetFamilyCount, OnGetFamilyCount) | |
| 78 IPC_MESSAGE_HANDLER(DWriteFontProxyMsg_GetFamilyNames, OnGetFamilyNames) | |
| 79 IPC_MESSAGE_HANDLER(DWriteFontProxyMsg_GetFontFiles, OnGetFontFiles) | |
| 80 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 81 IPC_END_MESSAGE_MAP() | |
| 82 return reply_; | |
| 83 } | |
| 84 | |
| 85 bool FakeFontCollection::ReplySender::Send(IPC::Message* msg) { | |
| 86 reply_.reset(msg); | |
| 87 return true; | |
| 88 } | |
| 89 | |
| 90 void FakeFontCollection::ReplySender::OnFindFamily( | |
| 91 const base::string16& family_name, | |
| 92 uint32_t* index) { | |
| 93 collection_->OnFindFamily(family_name, index); | |
| 94 } | |
| 95 | |
| 96 void FakeFontCollection::ReplySender::OnGetFamilyCount(uint32_t* count) { | |
| 97 collection_->OnGetFamilyCount(count); | |
| 98 } | |
| 99 | |
| 100 void FakeFontCollection::ReplySender::OnGetFamilyNames( | |
| 101 uint32_t family_index, | |
| 102 std::vector<DWriteStringPair>* family_names) { | |
| 103 collection_->OnGetFamilyNames(family_index, family_names); | |
| 104 } | |
| 105 | |
| 106 void FakeFontCollection::ReplySender::OnGetFontFiles( | |
| 107 uint32_t family_index, | |
| 108 std::vector<base::string16>* file_paths) { | |
| 109 collection_->OnGetFontFiles(family_index, file_paths); | |
| 110 } | |
| 111 | |
| 112 FakeFontCollection::FakeSender::FakeSender(FakeFontCollection* collection, | |
| 113 bool track_messages) | |
| 114 : track_messages_(track_messages), collection_(collection) {} | |
| 115 | |
| 116 FakeFontCollection::FakeSender::~FakeSender() = default; | |
| 117 | |
| 118 bool FakeFontCollection::FakeSender::Send(IPC::Message* message) { | |
| 119 std::unique_ptr<IPC::Message> incoming_message; | |
| 120 if (track_messages_) | |
| 121 collection_->messages_.emplace_back(message); | |
| 122 else | |
| 123 incoming_message.reset(message); // Ensure message is deleted. | |
| 124 std::unique_ptr<ReplySender> sender = collection_->GetReplySender(); | |
| 125 scoped_ptr<IPC::Message> reply; | |
| 126 reply.swap(sender->OnMessageReceived(*message)); | |
| 127 | |
| 128 IPC::SyncMessage* sync_message = reinterpret_cast<IPC::SyncMessage*>(message); | |
| 129 scoped_ptr<IPC::MessageReplyDeserializer> serializer( | |
| 130 sync_message->GetReplyDeserializer()); | |
| 131 serializer->SerializeOutputParameters(*(reply.get())); | |
| 132 return true; | |
| 133 } | |
| 134 | |
| 135 void FakeFontCollection::OnFindFamily(const base::string16& family_name, | |
| 136 uint32_t* index) { | |
| 137 for (size_t n = 0; n < fonts_.size(); n++) { | |
| 138 if (_wcsicmp(family_name.data(), fonts_[n].font_name_.data()) == 0) { | |
| 139 *index = n; | |
| 140 return; | |
| 141 } | |
| 142 } | |
| 143 *index = UINT32_MAX; | |
| 144 } | |
| 145 | |
| 146 void FakeFontCollection::OnGetFamilyCount(uint32_t* count) { | |
| 147 *count = fonts_.size(); | |
| 148 } | |
| 149 | |
| 150 void FakeFontCollection::OnGetFamilyNames( | |
| 151 uint32_t family_index, | |
| 152 std::vector<DWriteStringPair>* family_names) { | |
| 153 if (family_index >= fonts_.size()) | |
| 154 return; | |
| 155 *family_names = fonts_[family_index].family_names_; | |
| 156 } | |
| 157 | |
| 158 void FakeFontCollection::OnGetFontFiles( | |
| 159 uint32_t family_index, | |
| 160 std::vector<base::string16>* file_paths) { | |
| 161 if (family_index >= fonts_.size()) | |
| 162 return; | |
| 163 *file_paths = fonts_[family_index].file_paths_; | |
| 164 } | |
| 165 | |
| 166 std::unique_ptr<FakeFontCollection::ReplySender> | |
| 167 FakeFontCollection::GetReplySender() { | |
| 168 return std::unique_ptr<FakeFontCollection::ReplySender>( | |
| 169 new FakeFontCollection::ReplySender(this)); | |
| 170 } | |
| 171 | |
| 172 FakeFontCollection::~FakeFontCollection() = default; | |
| 173 | |
| 174 } // namespace content | |
| OLD | NEW |