Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / common / mac / attributed_string_coder.h
blob35b3bf54a55403009bc2ff18ebc894235be96acf
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.
5 #ifndef CONTENT_COMMON_MAC_ATTRIBUTED_STRING_CODER_H_
6 #define CONTENT_COMMON_MAC_ATTRIBUTED_STRING_CODER_H_
8 #include <set>
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"
16 #if __OBJC__
17 @class NSAttributedString;
18 @class NSDictionary;
19 #else
20 class NSAttributedString;
21 class NSDictionary;
22 #endif
24 namespace mac {
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();
43 // Creates an autoreleased NSDictionary that can be attached to an
44 // NSAttributedString.
45 NSDictionary* ToAttributesDictionary() const;
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;
52 // Accessors:
53 FontDescriptor font_descriptor() const { return font_descriptor_; }
54 gfx::Range effective_range() const { return effective_range_; }
56 private:
57 FontDescriptor font_descriptor_;
58 gfx::Range effective_range_;
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();
69 // Accessors:
70 base::string16 string() const { return string_; }
71 const std::vector<FontAttribute>& attributes() const {
72 return attributes_;
74 std::vector<FontAttribute>* attributes() { return &attributes_; }
76 private:
77 // The plain-text string.
78 base::string16 string_;
79 // The set of attributes that style |string_|.
80 std::vector<FontAttribute> attributes_;
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);
87 // Returns an autoreleased NSAttributedString from an encoded representation.
88 static NSAttributedString* Decode(const EncodedString* str);
90 private:
91 AttributedStringCoder();
94 } // namespace mac
96 // IPC ParamTraits specialization //////////////////////////////////////////////
98 namespace IPC {
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);
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);
116 } // namespace IPC
118 #endif // CONTENT_COMMON_MAC_ATTRIBUTED_STRING_CODER_H_