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