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_
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"
17 @
class NSAttributedString
;
20 class NSAttributedString
;
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
{
34 // A C++ IPC-friendly representation of the NSFontAttributeName attribute
38 FontAttribute(NSDictionary
* ns_attributes
, gfx::Range effective_range
);
39 FontAttribute(FontDescriptor font
, gfx::Range range
);
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;
53 FontDescriptor
font_descriptor() const { return font_descriptor_
; }
54 gfx::Range
effective_range() const { return effective_range_
; }
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.
65 explicit EncodedString(base::string16 string
);
70 base::string16
string() const { return string_
; }
71 const std::vector
<FontAttribute
>& attributes() const {
74 std::vector
<FontAttribute
>* attributes() { return &attributes_
; }
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
);
91 AttributedStringCoder();
96 // IPC ParamTraits specialization //////////////////////////////////////////////
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
, base::PickleIterator
* iter
, param_type
* r
);
105 static void Log(const param_type
& p
, std::string
* l
);
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
, base::PickleIterator
* iter
, param_type
* r
);
113 static void Log(const param_type
& p
, std::string
* l
);
118 #endif // CONTENT_COMMON_MAC_ATTRIBUTED_STRING_CODER_H_