| 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() {} | |
| 40 | |
| 41 FakeFont& FakeFontCollection::AddFont(const base::string16& font_name) { | |
| 42 fonts_.emplace_back(font_name); | |
| 43 return fonts_.back(); | |
| 44 } | |
| 45 | |
| 46 size_t FakeFontCollection::MessageCount() { | |
| 47 return messages_.size(); | |
| 48 } | |
| 49 | |
| 50 IPC::Message* FakeFontCollection::GetMessage(size_t index) { | |
| 51 return messages_[index].get(); | |
| 52 } | |
| 53 | |
| 54 IPC::Sender* FakeFontCollection::GetSender() { | |
| 55 return new FakeSender(this, false /* track_messages */); | |
| 56 } | |
| 57 | |
| 58 IPC::Sender* FakeFontCollection::GetTrackingSender() { | |
| 59 return new FakeSender(this, true /* track_messages */); | |
| 60 } | |
| 61 | |
| 62 FakeFontCollection::ReplySender::~ReplySender() {} | |
| 63 | |
| 64 scoped_ptr<IPC::Message>& FakeFontCollection::ReplySender::OnMessageReceived( | |
| 65 const IPC::Message& msg) { | |
| 66 reply_.reset(); | |
| 67 bool handled = true; | |
| 68 IPC_BEGIN_MESSAGE_MAP(ReplySender, msg) | |
| 69 IPC_MESSAGE_HANDLER(DWriteFontProxyMsg_FindFamily, OnFindFamily) | |
| 70 IPC_MESSAGE_HANDLER(DWriteFontProxyMsg_GetFamilyCount, OnGetFamilyCount) | |
| 71 IPC_MESSAGE_HANDLER(DWriteFontProxyMsg_GetFamilyNames, OnGetFamilyNames) | |
| 72 IPC_MESSAGE_HANDLER(DWriteFontProxyMsg_GetFontFiles, OnGetFontFiles) | |
| 73 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 74 IPC_END_MESSAGE_MAP() | |
| 75 return reply_; | |
| 76 } | |
| 77 | |
| 78 bool FakeFontCollection::ReplySender::Send(IPC::Message* msg) { | |
| 79 reply_.reset(msg); | |
| 80 return true; | |
| 81 } | |
| 82 | |
| 83 void FakeFontCollection::ReplySender::OnFindFamily( | |
| 84 const base::string16& family_name, | |
| 85 uint32_t* index) { | |
| 86 collection_->OnFindFamily(family_name, index); | |
| 87 } | |
| 88 | |
| 89 void FakeFontCollection::ReplySender::OnGetFamilyCount(uint32_t* count) { | |
| 90 collection_->OnGetFamilyCount(count); | |
| 91 } | |
| 92 | |
| 93 void FakeFontCollection::ReplySender::OnGetFamilyNames( | |
| 94 uint32_t family_index, | |
| 95 std::vector<DWriteStringPair>* family_names) { | |
| 96 collection_->OnGetFamilyNames(family_index, family_names); | |
| 97 } | |
| 98 | |
| 99 void FakeFontCollection::ReplySender::OnGetFontFiles( | |
| 100 uint32_t family_index, | |
| 101 std::vector<base::string16>* file_paths) { | |
| 102 collection_->OnGetFontFiles(family_index, file_paths); | |
| 103 } | |
| 104 | |
| 105 FakeFontCollection::FakeSender::~FakeSender() {} | |
| 106 | |
| 107 bool FakeFontCollection::FakeSender::Send(IPC::Message* message) { | |
| 108 std::unique_ptr<IPC::Message> incoming_message; | |
| 109 if (track_messages_) | |
| 110 collection_->messages_.emplace_back(message); | |
| 111 else | |
| 112 incoming_message.reset(message); // Ensure message is deleted. | |
| 113 std::unique_ptr<ReplySender> sender = collection_->GetReplySender(); | |
| 114 scoped_ptr<IPC::Message> reply; | |
| 115 reply.swap(sender->OnMessageReceived(*message)); | |
| 116 | |
| 117 IPC::SyncMessage* sync_message = reinterpret_cast<IPC::SyncMessage*>(message); | |
| 118 scoped_ptr<IPC::MessageReplyDeserializer> serializer( | |
| 119 sync_message->GetReplyDeserializer()); | |
| 120 serializer->SerializeOutputParameters(*(reply.get())); | |
| 121 return true; | |
| 122 } | |
| 123 | |
| 124 void FakeFontCollection::OnFindFamily(const base::string16& family_name, | |
| 125 uint32_t* index) { | |
| 126 for (size_t n = 0; n < fonts_.size(); n++) { | |
| 127 if (_wcsicmp(family_name.data(), fonts_[n].font_name_.data()) == 0) { | |
| 128 *index = n; | |
| 129 return; | |
| 130 } | |
| 131 } | |
| 132 *index = UINT32_MAX; | |
| 133 } | |
| 134 | |
| 135 void FakeFontCollection::OnGetFamilyCount(uint32_t* count) { | |
| 136 *count = fonts_.size(); | |
| 137 } | |
| 138 | |
| 139 void FakeFontCollection::OnGetFamilyNames( | |
| 140 uint32_t family_index, | |
| 141 std::vector<DWriteStringPair>* family_names) { | |
| 142 if (family_index >= fonts_.size()) | |
| 143 return; | |
| 144 *family_names = fonts_[family_index].family_names_; | |
| 145 } | |
| 146 | |
| 147 void FakeFontCollection::OnGetFontFiles( | |
| 148 uint32_t family_index, | |
| 149 std::vector<base::string16>* file_paths) { | |
| 150 if (family_index >= fonts_.size()) | |
| 151 return; | |
| 152 *file_paths = fonts_[family_index].file_paths_; | |
| 153 } | |
| 154 | |
| 155 std::unique_ptr<FakeFontCollection::ReplySender> | |
| 156 FakeFontCollection::GetReplySender() { | |
| 157 return std::unique_ptr<FakeFontCollection::ReplySender>( | |
| 158 new FakeFontCollection::ReplySender(this)); | |
| 159 } | |
| 160 | |
| 161 FakeFontCollection::~FakeFontCollection() {} | |
| 162 | |
| 163 } // namespace content | |
| OLD | NEW |