summaryrefslogtreecommitdiffstats
path: root/content/common/mac/attributed_string_coder.mm
blob: ed377bb91d1374045be73e6548d2b256be3c31b9 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// 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.

#include "content/common/mac/attributed_string_coder.h"

#include <AppKit/AppKit.h>

#include "base/logging.h"
#include "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "content/common/view_messages.h"
#include "content/public/common/common_param_traits.h"
#include "ipc/ipc_message_utils.h"

namespace mac {

// static
const AttributedStringCoder::EncodedString* AttributedStringCoder::Encode(
    NSAttributedString* str) {
  // Create the return value.
  EncodedString* encoded_string =
      new EncodedString(base::SysNSStringToUTF16([str string]));
  // Iterate over all the attributes in the string.
  NSUInteger length = [str length];
  for (NSUInteger i = 0; i < length; ) {
    NSRange effective_range;
    NSDictionary* ns_attributes = [str attributesAtIndex:i
                                          effectiveRange:&effective_range];
    // Convert the attributes to IPC-friendly types.
    FontAttribute attrs(ns_attributes, gfx::Range(effective_range));
    // Only encode the attributes if the filtered set contains font information.
    if (attrs.ShouldEncode()) {
      encoded_string->attributes()->push_back(attrs);
    }
    // Advance the iterator to the position outside of the effective range.
    i = NSMaxRange(effective_range);
  }
  return encoded_string;
}

// static
NSAttributedString* AttributedStringCoder::Decode(
    const AttributedStringCoder::EncodedString* str) {
  // Create the return value.
  NSString* plain_text = base::SysUTF16ToNSString(str->string());
  base::scoped_nsobject<NSMutableAttributedString> decoded_string(
      [[NSMutableAttributedString alloc] initWithString:plain_text]);
  // Iterate over all the encoded attributes, attaching each to the string.
  const std::vector<FontAttribute> attributes = str->attributes();
  for (std::vector<FontAttribute>::const_iterator it = attributes.begin();
       it != attributes.end(); ++it) {
    // Protect against ranges that are outside the range of the string.
    const gfx::Range& range = it->effective_range();
    if (range.GetMin() > [plain_text length] ||
        range.GetMax() > [plain_text length]) {
      continue;
    }
    [decoded_string addAttributes:it->ToAttributesDictionary()
                            range:range.ToNSRange()];
  }
  return [decoded_string.release() autorelease];
}

// Data Types //////////////////////////////////////////////////////////////////

AttributedStringCoder::EncodedString::EncodedString(base::string16 string)
    : string_(string) {
}

AttributedStringCoder::EncodedString::EncodedString()
    : string_() {
}

AttributedStringCoder::EncodedString::~EncodedString() {
}

AttributedStringCoder::FontAttribute::FontAttribute(NSDictionary* dict,
                                                    gfx::Range effective_range)
    : font_descriptor_(),
      effective_range_(effective_range) {
  NSFont* font = [dict objectForKey:NSFontAttributeName];
  if (font) {
    font_descriptor_ = FontDescriptor(font);
  }
}

AttributedStringCoder::FontAttribute::FontAttribute(FontDescriptor font,
                                                    gfx::Range range)
    : font_descriptor_(font),
      effective_range_(range) {
}

AttributedStringCoder::FontAttribute::FontAttribute()
    : font_descriptor_(),
      effective_range_() {
}

AttributedStringCoder::FontAttribute::~FontAttribute() {
}

NSDictionary*
AttributedStringCoder::FontAttribute::ToAttributesDictionary() const {
  DCHECK(ShouldEncode());
  NSFont* font = font_descriptor_.ToNSFont();
  if (!font)
    return [NSDictionary dictionary];
  return [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
}

bool AttributedStringCoder::FontAttribute::ShouldEncode() const {
  return !font_descriptor_.font_name.empty();
}

}  // namespace mac

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

namespace IPC {

using mac::AttributedStringCoder;

void ParamTraits<AttributedStringCoder::EncodedString>::Write(
    Message* m, const param_type& p) {
  WriteParam(m, p.string());
  WriteParam(m, p.attributes());
}

bool ParamTraits<AttributedStringCoder::EncodedString>::Read(
    const Message* m, PickleIterator* iter, param_type* p) {
  bool success = true;

  base::string16 result;
  success &= ReadParam(m, iter, &result);
  *p = AttributedStringCoder::EncodedString(result);

  success &= ReadParam(m, iter, p->attributes());
  return success;
}

void ParamTraits<AttributedStringCoder::EncodedString>::Log(
    const param_type& p, std::string* l) {
  l->append(base::UTF16ToUTF8(p.string()));
}

void ParamTraits<AttributedStringCoder::FontAttribute>::Write(
    Message* m, const param_type& p) {
  WriteParam(m, p.font_descriptor());
  WriteParam(m, p.effective_range());
}

bool ParamTraits<AttributedStringCoder::FontAttribute>::Read(
    const Message* m, PickleIterator* iter, param_type* p) {
  bool success = true;

  FontDescriptor font;
  success &= ReadParam(m, iter, &font);

  gfx::Range range;
  success &= ReadParam(m, iter, &range);

  if (success) {
    *p = AttributedStringCoder::FontAttribute(font, range);
  }
  return success;
}

void ParamTraits<AttributedStringCoder::FontAttribute>::Log(
    const param_type& p, std::string* l) {
}

}  // namespace IPC