OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 #ifndef CONTENT_TEST_DWRITE_FONT_FAKE_SENDER_WIN_H_ |
| 6 #define CONTENT_TEST_DWRITE_FONT_FAKE_SENDER_WIN_H_ |
| 7 |
| 8 #include <wrl.h> |
| 9 |
| 10 #include <utility> |
| 11 #include <vector> |
| 12 |
| 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/strings/string16.h" |
| 16 #include "ipc/ipc_message.h" |
| 17 #include "ipc/ipc_sender.h" |
| 18 |
| 19 namespace content { |
| 20 |
| 21 class FakeFontCollection; |
| 22 |
| 23 // Creates a new FakeFontCollection, seeded with some basic data, and returns a |
| 24 // Sender that can be used to interact with the collection. |
| 25 IPC::Sender* CreateFakeCollectionSender(); |
| 26 |
| 27 // Helper class for describing a font object. Use FakeFontCollection instead. |
| 28 class FakeFont { |
| 29 public: |
| 30 explicit FakeFont(const base::string16& name) : font_name_(name) {} |
| 31 |
| 32 ~FakeFont(); |
| 33 |
| 34 FakeFont& AddFilePath(const base::string16& file_path) { |
| 35 file_paths_.push_back(file_path); |
| 36 return *this; |
| 37 } |
| 38 |
| 39 FakeFont& AddFamilyName(const base::string16& locale, |
| 40 const base::string16& family_name) { |
| 41 family_names_.emplace_back(locale, family_name); |
| 42 return *this; |
| 43 } |
| 44 |
| 45 private: |
| 46 friend FakeFontCollection; |
| 47 base::string16 font_name_; |
| 48 std::vector<base::string16> file_paths_; |
| 49 std::vector<std::pair<base::string16, base::string16>> family_names_; |
| 50 |
| 51 DISALLOW_ASSIGN(FakeFont); |
| 52 }; |
| 53 |
| 54 // Implements a font collection that supports interaction through sending IPC |
| 55 // messages from dwrite_font_proxy_messages.h. |
| 56 // Usage: |
| 57 // Create a new FakeFontCollection. |
| 58 // Call AddFont() to add fonts. |
| 59 // For each font, call methods on the font to configure the font. |
| 60 // Note: the indices of the fonts will correspond to the order they were |
| 61 // added. The collection will not sort or reorder fonts in any way. |
| 62 // Call GetSender()/GetTrackingSender() to obtain an IPC::Sender. |
| 63 // Call Send() with DWriteFontProxyMsg_* to interact with the collection. |
| 64 // Call MessageCount()/GetMessage() to inspect the messages that were sent. |
| 65 // |
| 66 // The internal code flow for GetSender()/Send() is as follows: |
| 67 // GetSender() returns a new FakeSender, pointing back to the collection. |
| 68 // FakeSender::Send() will create a new ReplySender and call |
| 69 // ReplySender::OnMessageReceived() |
| 70 // ReplySender::OnMessageReceived() contains the message map, which will |
| 71 // internally call ReplySender::On*() and ReplySender::Send() |
| 72 // ReplySender::Send() will save the reply message, to be used later. |
| 73 // FakeSender::Send() will retrieve the reply message and save the output. |
| 74 class FakeFontCollection : public base::RefCounted<FakeFontCollection> { |
| 75 public: |
| 76 FakeFontCollection() = default; |
| 77 |
| 78 FakeFont& AddFont(const base::string16& font_name); |
| 79 |
| 80 unsigned int MessageCount(); |
| 81 |
| 82 IPC::Message* GetMessage(unsigned int index); |
| 83 |
| 84 IPC::Sender* GetSender(); |
| 85 |
| 86 // Like GetSender(), but will keep track of all sent messages for inspection. |
| 87 IPC::Sender* GetTrackingSender(); |
| 88 |
| 89 protected: |
| 90 // This class handles sending the reply message back to the "renderer" |
| 91 class ReplySender : public IPC::Sender { |
| 92 public: |
| 93 explicit ReplySender(FakeFontCollection* collection) |
| 94 : collection_(collection) {} |
| 95 |
| 96 ~ReplySender(); |
| 97 |
| 98 scoped_ptr<IPC::Message>& OnMessageReceived(const IPC::Message& msg); |
| 99 |
| 100 bool Send(IPC::Message* msg) override; |
| 101 |
| 102 private: |
| 103 void OnFindFamily(const base::string16& family_name, uint32* index); |
| 104 |
| 105 void OnGetFamilyCount(uint32* count); |
| 106 |
| 107 void OnGetFamilyNames( |
| 108 uint32 family_index, |
| 109 std::vector<std::pair<base::string16, base::string16>>* family_names); |
| 110 void OnGetFontFiles(uint32 family_index, |
| 111 std::vector<base::string16>* file_paths_); |
| 112 |
| 113 private: |
| 114 scoped_refptr<FakeFontCollection> collection_; |
| 115 scoped_ptr<IPC::Message> reply_; |
| 116 |
| 117 DISALLOW_COPY_AND_ASSIGN(ReplySender); |
| 118 }; |
| 119 |
| 120 // This class can be used by the "renderer" to send messages to the "browser" |
| 121 class FakeSender : public IPC::Sender { |
| 122 public: |
| 123 FakeSender(FakeFontCollection* collection, bool track_messages) |
| 124 : collection_(collection), track_messages_(track_messages) {} |
| 125 |
| 126 ~FakeSender(); |
| 127 |
| 128 bool Send(IPC::Message* message) override; |
| 129 |
| 130 private: |
| 131 bool track_messages_; |
| 132 scoped_refptr<FakeFontCollection> collection_; |
| 133 |
| 134 DISALLOW_COPY_AND_ASSIGN(FakeSender); |
| 135 }; |
| 136 |
| 137 void OnFindFamily(const base::string16& family_name, uint32* index); |
| 138 |
| 139 void OnGetFamilyCount(uint32* count); |
| 140 |
| 141 void OnGetFamilyNames( |
| 142 uint32 family_index, |
| 143 std::vector<std::pair<base::string16, base::string16>>* family_names); |
| 144 |
| 145 void OnGetFontFiles(uint32 family_index, |
| 146 std::vector<base::string16>* file_paths); |
| 147 |
| 148 std::unique_ptr<ReplySender> GetReplySender(); |
| 149 |
| 150 private: |
| 151 friend class base::RefCounted<FakeFontCollection>; |
| 152 ~FakeFontCollection(); |
| 153 |
| 154 std::vector<FakeFont> fonts_; |
| 155 std::vector<std::unique_ptr<IPC::Message>> messages_; |
| 156 |
| 157 DISALLOW_COPY_AND_ASSIGN(FakeFontCollection); |
| 158 }; |
| 159 |
| 160 } // namespace content |
| 161 |
| 162 #endif // CONTENT_TEST_DWRITE_FONT_FAKE_SENDER_WIN_H_ |
OLD | NEW |