OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 #include "chrome/common/attributed_string_coder_mac.h" |
| 6 |
| 7 #include <AppKit/AppKit.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_nsobject.h" |
| 11 #include "base/sys_string_conversions.h" |
| 12 #include "base/utf_string_conversions.h" |
| 13 #include "content/common/common_param_traits.h" |
| 14 #include "content/common/view_messages.h" |
| 15 #include "ipc/ipc_message_utils.h" |
| 16 |
| 17 namespace mac { |
| 18 |
| 19 // static |
| 20 const AttributedStringCoder::EncodedString* AttributedStringCoder::Encode( |
| 21 NSAttributedString* str) { |
| 22 // Create the return value. |
| 23 EncodedString* encoded_string = |
| 24 new EncodedString(base::SysNSStringToUTF16([str string])); |
| 25 // Iterate over all the attributes in the string. |
| 26 NSUInteger length = [str length]; |
| 27 for (NSUInteger i = 0; i < length; ) { |
| 28 NSRange effective_range; |
| 29 NSDictionary* ns_attributes = [str attributesAtIndex:i |
| 30 effectiveRange:&effective_range]; |
| 31 // Convert the attributes to IPC-friendly types. |
| 32 FontAttribute attrs(ns_attributes, ui::Range(effective_range)); |
| 33 // Only encode the attributes if the filtered set contains font information. |
| 34 if (attrs.ShouldEncode()) { |
| 35 encoded_string->attributes()->push_back(attrs); |
| 36 } |
| 37 // Advance the iterator to the position outside of the effective range. |
| 38 i = NSMaxRange(effective_range); |
| 39 } |
| 40 return encoded_string; |
| 41 } |
| 42 |
| 43 // static |
| 44 NSAttributedString* AttributedStringCoder::Decode( |
| 45 const AttributedStringCoder::EncodedString* str) { |
| 46 // Create the return value. |
| 47 NSString* plain_text = base::SysUTF16ToNSString(str->string()); |
| 48 scoped_nsobject<NSMutableAttributedString> decoded_string( |
| 49 [[NSMutableAttributedString alloc] initWithString:plain_text]); |
| 50 // Iterate over all the encoded attributes, attaching each to the string. |
| 51 const std::vector<FontAttribute> attributes = str->attributes(); |
| 52 for (std::vector<FontAttribute>::const_iterator it = attributes.begin(); |
| 53 it != attributes.end(); ++it) { |
| 54 [decoded_string addAttributes:it->ToAttributesDictionary() |
| 55 range:it->effective_range().ToNSRange()]; |
| 56 } |
| 57 return [decoded_string.release() autorelease]; |
| 58 } |
| 59 |
| 60 // Data Types ////////////////////////////////////////////////////////////////// |
| 61 |
| 62 AttributedStringCoder::EncodedString::EncodedString(string16 string) |
| 63 : string_(string) { |
| 64 } |
| 65 |
| 66 AttributedStringCoder::EncodedString::EncodedString() |
| 67 : string_() { |
| 68 } |
| 69 |
| 70 AttributedStringCoder::EncodedString::~EncodedString() { |
| 71 } |
| 72 |
| 73 AttributedStringCoder::FontAttribute::FontAttribute(NSDictionary* dict, |
| 74 ui::Range effective_range) |
| 75 : font_descriptor_(), |
| 76 effective_range_(effective_range) { |
| 77 NSFont* font = [dict objectForKey:NSFontAttributeName]; |
| 78 if (font) { |
| 79 font_descriptor_ = FontDescriptor(font); |
| 80 } |
| 81 } |
| 82 |
| 83 AttributedStringCoder::FontAttribute::FontAttribute(FontDescriptor font, |
| 84 ui::Range range) |
| 85 : font_descriptor_(font), |
| 86 effective_range_(range) { |
| 87 } |
| 88 |
| 89 AttributedStringCoder::FontAttribute::FontAttribute() |
| 90 : font_descriptor_(), |
| 91 effective_range_() { |
| 92 } |
| 93 |
| 94 AttributedStringCoder::FontAttribute::~FontAttribute() { |
| 95 } |
| 96 |
| 97 NSDictionary* AttributedStringCoder::FontAttribute::ToAttributesDictionary( |
| 98 void) const { |
| 99 DCHECK(ShouldEncode()); |
| 100 NSFont* font = font_descriptor_.ToNSFont(); |
| 101 return [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; |
| 102 } |
| 103 |
| 104 bool AttributedStringCoder::FontAttribute::ShouldEncode() const { |
| 105 return !font_descriptor_.font_name.empty(); |
| 106 } |
| 107 |
| 108 } // namespace mac |
| 109 |
| 110 // IPC ParamTraits specialization ////////////////////////////////////////////// |
| 111 |
| 112 namespace IPC { |
| 113 |
| 114 using mac::AttributedStringCoder; |
| 115 |
| 116 void ParamTraits<AttributedStringCoder::EncodedString>::Write( |
| 117 Message* m, const param_type& p) { |
| 118 WriteParam(m, p.string()); |
| 119 WriteParam(m, p.attributes()); |
| 120 } |
| 121 |
| 122 bool ParamTraits<AttributedStringCoder::EncodedString>::Read( |
| 123 const Message* m, void** iter, param_type* p) { |
| 124 bool success = true; |
| 125 |
| 126 string16 result; |
| 127 success &= ReadParam(m, iter, &result); |
| 128 *p = AttributedStringCoder::EncodedString(result); |
| 129 |
| 130 success &= ReadParam(m, iter, p->attributes()); |
| 131 return success; |
| 132 } |
| 133 |
| 134 void ParamTraits<AttributedStringCoder::EncodedString>::Log( |
| 135 const param_type& p, std::string* l) { |
| 136 l->append(UTF16ToUTF8(p.string())); |
| 137 } |
| 138 |
| 139 void ParamTraits<AttributedStringCoder::FontAttribute>::Write( |
| 140 Message* m, const param_type& p) { |
| 141 WriteParam(m, p.font_descriptor()); |
| 142 WriteParam(m, p.effective_range()); |
| 143 } |
| 144 |
| 145 bool ParamTraits<AttributedStringCoder::FontAttribute>::Read( |
| 146 const Message* m, void** iter, param_type* p) { |
| 147 bool success = true; |
| 148 |
| 149 FontDescriptor font; |
| 150 success &= ReadParam(m, iter, &font); |
| 151 |
| 152 ui::Range range; |
| 153 success &= ReadParam(m, iter, &range); |
| 154 |
| 155 if (success) { |
| 156 *p = AttributedStringCoder::FontAttribute(font, range); |
| 157 } |
| 158 return success; |
| 159 } |
| 160 |
| 161 void ParamTraits<AttributedStringCoder::FontAttribute>::Log( |
| 162 const param_type& p, std::string* l) { |
| 163 } |
| 164 |
| 165 } // namespace IPC |
OLD | NEW |