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