OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 CHROME_COMMON_ATTRIBUTED_STRING_CODER_H_ | |
6 #define CHROME_COMMON_ATTRIBUTED_STRING_CODER_H_ | |
7 | |
8 #include <set> | |
9 | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/string16.h" | |
12 #include "ipc/ipc_message_utils.h" | |
13 #include "ui/base/range/range.h" | |
14 | |
15 #if __OBJC__ | |
16 @class NSAttributedString; | |
17 @class NSDictionary; | |
18 #else | |
19 class NSAttributedString; | |
20 class NSDictionary; | |
21 #endif | |
22 | |
23 namespace mac { | |
24 | |
25 // This class will serialize the font information of an NSAttributedString so | |
26 // that it can be sent over IPC. This class only stores the information of the | |
27 // NSFontAttributeName. The motive is that of security: using NSArchiver and | |
28 // friends to send objects from the renderer to the browser could lead to | |
29 // deserialization of arbitrary objects. This class restricts serialization to | |
30 // a specific object class and specific attributes of that object. | |
31 class AttributedStringCoder { | |
32 public: | |
33 // A C++ IPC-friendly representation of the NSFontAttributeName attribute | |
34 // set. | |
35 class FontAttribute { | |
36 public: | |
37 FontAttribute(NSDictionary* ns_attributes, ui::Range effective_range); | |
38 FontAttribute(std::string name, float size, ui::Range range); | |
39 FontAttribute(); | |
40 | |
41 // Creates an autoreleased NSDictionary that can be attached to an | |
42 // NSAttributedString. | |
43 NSDictionary* ToAttributesDictionary() const; | |
44 | |
45 // Whether or not the attribute should be placed in the EncodedString. This | |
46 // can return false, e.g. if the Cocoa-based constructor can't find any | |
47 // information to encode. | |
48 bool ShouldEncode() const; | |
49 | |
50 // Accessors: | |
51 std::string font_name() const { return font_name_; } | |
52 float font_size() const { return font_size_; } | |
53 ui::Range effective_range() const { return effective_range_; } | |
54 | |
55 private: | |
56 std::string font_name_; | |
57 float font_size_; | |
58 ui::Range effective_range_; | |
59 }; | |
60 | |
61 // A class that contains the pertitent information from an NSAttributedString, | |
Avi (use Gerrit)
2011/04/11 20:22:16
pertinent
Robert Sesek
2011/04/25 20:58:18
Done.
| |
62 // which can be serialized over IPC. | |
63 class EncodedString { | |
64 public: | |
65 explicit EncodedString(string16 string); | |
66 EncodedString(); | |
67 | |
68 // Accessors: | |
69 string16 string() const { return string_; } | |
70 const std::vector<FontAttribute>& attributes() const { | |
71 return attributes_; | |
72 } | |
73 std::vector<FontAttribute>* attributes() { return &attributes_; } | |
74 | |
75 private: | |
76 // The plain-text string. | |
77 string16 string_; | |
78 // The set of attributes that style |string_|. | |
79 std::vector<FontAttribute> attributes_; | |
80 }; | |
81 | |
82 // Takes an NSAttributedString, extracts the pertinent attributes, and returns | |
83 // an object that represents it. Caller owns the result. | |
84 static const EncodedString* Encode(NSAttributedString* str); | |
85 | |
86 // Returns an autoreleased NSAttributedString from an encoded representation. | |
87 static NSAttributedString* Decode(const EncodedString* str); | |
88 | |
89 private: | |
90 AttributedStringCoder(); | |
91 }; | |
92 | |
93 } // namespace mac | |
94 | |
95 // IPC ParamTraits specialization ////////////////////////////////////////////// | |
96 | |
97 namespace IPC { | |
98 | |
99 template <> | |
100 struct ParamTraits<mac::AttributedStringCoder::EncodedString> { | |
101 typedef mac::AttributedStringCoder::EncodedString param_type; | |
102 static void Write(Message* m, const param_type& p); | |
103 static bool Read(const Message* m, void** iter, param_type* r); | |
104 static void Log(const param_type& p, std::string* l); | |
105 }; | |
106 | |
107 template <> | |
108 struct ParamTraits<mac::AttributedStringCoder::FontAttribute> { | |
109 typedef mac::AttributedStringCoder::FontAttribute param_type; | |
110 static void Write(Message* m, const param_type& p); | |
111 static bool Read(const Message* m, void** iter, param_type* r); | |
112 static void Log(const param_type& p, std::string* l); | |
113 }; | |
114 | |
115 } // namespace IPC | |
116 | |
117 #endif // CHROME_COMMON_ATTRIBUTED_STRING_CODER_H_ | |
OLD | NEW |