summaryrefslogtreecommitdiffstats
path: root/content/common/mac/attributed_string_coder.h
blob: 629beedff4ead5360ad03393435f8eb9c6d12fd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_COMMON_MAC_ATTRIBUTED_STRING_CODER_H_
#define CONTENT_COMMON_MAC_ATTRIBUTED_STRING_CODER_H_

#include <set>

#include "base/strings/string16.h"
#include "content/common/content_export.h"
#include "content/common/mac/font_descriptor.h"
#include "ipc/ipc_message_utils.h"
#include "ui/gfx/range/range.h"

#if __OBJC__
@class NSAttributedString;
@class NSDictionary;
#else
class NSAttributedString;
class NSDictionary;
#endif

namespace mac {

// This class will serialize the font information of an NSAttributedString so
// that it can be sent over IPC. This class only stores the information of the
// NSFontAttributeName. The motive is that of security: using NSArchiver and
// friends to send objects from the renderer to the browser could lead to
// deserialization of arbitrary objects. This class restricts serialization to
// a specific object class and specific attributes of that object.
class CONTENT_EXPORT AttributedStringCoder {
 public:
  // A C++ IPC-friendly representation of the NSFontAttributeName attribute
  // set.
  class FontAttribute {
   public:
    FontAttribute(NSDictionary* ns_attributes, gfx::Range effective_range);
    FontAttribute(FontDescriptor font, gfx::Range range);
    FontAttribute();
    ~FontAttribute();

    // Creates an autoreleased NSDictionary that can be attached to an
    // NSAttributedString.
    NSDictionary* ToAttributesDictionary() const;

    // Whether or not the attribute should be placed in the EncodedString. This
    // can return false, e.g. if the Cocoa-based constructor can't find any
    // information to encode.
    bool ShouldEncode() const;

    // Accessors:
    FontDescriptor font_descriptor() const { return font_descriptor_; }
    gfx::Range effective_range() const { return effective_range_; }

   private:
    FontDescriptor font_descriptor_;
    gfx::Range effective_range_;
  };

  // A class that contains the pertinent information from an NSAttributedString,
  // which can be serialized over IPC.
  class EncodedString {
   public:
    explicit EncodedString(string16 string);
    EncodedString();
    ~EncodedString();

    // Accessors:
    string16 string() const { return string_; }
    const std::vector<FontAttribute>& attributes() const {
      return attributes_;
    }
    std::vector<FontAttribute>* attributes() { return &attributes_; }

   private:
    // The plain-text string.
    string16 string_;
    // The set of attributes that style |string_|.
    std::vector<FontAttribute> attributes_;
  };

  // Takes an NSAttributedString, extracts the pertinent attributes, and returns
  // an object that represents it. Caller owns the result.
  static const EncodedString* Encode(NSAttributedString* str);

  // Returns an autoreleased NSAttributedString from an encoded representation.
  static NSAttributedString* Decode(const EncodedString* str);

 private:
  AttributedStringCoder();
};

}  // namespace mac

// IPC ParamTraits specialization //////////////////////////////////////////////

namespace IPC {

template <>
struct ParamTraits<mac::AttributedStringCoder::EncodedString> {
  typedef mac::AttributedStringCoder::EncodedString param_type;
  static void Write(Message* m, const param_type& p);
  static bool Read(const Message* m, PickleIterator* iter, param_type* r);
  static void Log(const param_type& p, std::string* l);
};

template <>
struct ParamTraits<mac::AttributedStringCoder::FontAttribute> {
  typedef mac::AttributedStringCoder::FontAttribute param_type;
  static void Write(Message* m, const param_type& p);
  static bool Read(const Message* m, PickleIterator* iter, param_type* r);
  static void Log(const param_type& p, std::string* l);
};

}  // namespace IPC

#endif  // CONTENT_COMMON_MAC_ATTRIBUTED_STRING_CODER_H_