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