Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: content/common/mac/font_loader.mm

Issue 10052006: Merge 131324 - Mac: OOP font loading should run on FILE thread. (Closed) Base URL: svn://svn.chromium.org/chrome/branches/1084/src/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/common/mac/font_loader.h ('k') | content/common/sandbox_mac_fontloading_unittest.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/common/mac/font_loader.h" 5 #include "content/common/mac/font_loader.h"
6 6
7 #import <Cocoa/Cocoa.h> 7 #import <Cocoa/Cocoa.h>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/file_path.h" 10 #include "base/file_path.h"
11 #include "base/file_util.h" 11 #include "base/file_util.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/mac/foundation_util.h" 13 #include "base/mac/foundation_util.h"
14 #include "base/mac/mac_util.h" 14 #include "base/mac/mac_util.h"
15 #include "base/mac/scoped_cftyperef.h" 15 #include "base/mac/scoped_cftyperef.h"
16 #include "base/sys_string_conversions.h" 16 #include "base/sys_string_conversions.h"
17 #include "base/threading/thread_restrictions.h"
18 #include "content/common/mac/font_descriptor.h"
17 19
18 extern "C" { 20 extern "C" {
19 21
20 // Work around http://crbug.com/93191, a really nasty memory smasher bug. 22 // Work around http://crbug.com/93191, a really nasty memory smasher bug.
21 // On Mac OS X 10.7 ("Lion"), ATS writes to memory it doesn't own. 23 // On Mac OS X 10.7 ("Lion"), ATS writes to memory it doesn't own.
22 // SendDeactivateFontsInContainerMessage, called by ATSFontDeactivate, 24 // SendDeactivateFontsInContainerMessage, called by ATSFontDeactivate,
23 // may trash memory whenever dlsym(RTLD_DEFAULT, 25 // may trash memory whenever dlsym(RTLD_DEFAULT,
24 // "_CTFontManagerUnregisterFontForData") returns NULL. In that case, it tries 26 // "_CTFontManagerUnregisterFontForData") returns NULL. In that case, it tries
25 // to locate that symbol in the CoreText framework, doing some extremely 27 // to locate that symbol in the CoreText framework, doing some extremely
26 // sloppy string handling resulting in a likelihood that the string 28 // sloppy string handling resulting in a likelihood that the string
(...skipping 18 matching lines...) Expand all
45 // SendDeactivateFontsInContainerMessage on 10.6 ("Snow Leopard") appears to 47 // SendDeactivateFontsInContainerMessage on 10.6 ("Snow Leopard") appears to
46 // share this bug but this sort of memory corruption wasn't detected until 48 // share this bug but this sort of memory corruption wasn't detected until
47 // 10.7. The implementation in 10.5 ("Leopard") does not have this problem. 49 // 10.7. The implementation in 10.5 ("Leopard") does not have this problem.
48 __attribute__((visibility("default"))) 50 __attribute__((visibility("default")))
49 void _CTFontManagerUnregisterFontForData(NSUInteger, int) { 51 void _CTFontManagerUnregisterFontForData(NSUInteger, int) {
50 } 52 }
51 53
52 } // extern "C" 54 } // extern "C"
53 55
54 // static 56 // static
55 bool FontLoader::LoadFontIntoBuffer(NSFont* font_to_encode, 57 void FontLoader::LoadFont(const FontDescriptor& font,
56 base::SharedMemory* font_data, 58 FontLoader::Result* result) {
57 uint32* font_data_size, 59 base::ThreadRestrictions::AssertIOAllowed();
58 uint32* font_id) { 60 DCHECK(result);
59 DCHECK(font_data); 61 result->font_data_size = 0;
60 DCHECK(font_data_size); 62 result->font_id = 0;
61 DCHECK(font_id);
62 *font_data_size = 0;
63 *font_id = 0;
64 63
64 NSFont* font_to_encode = font.ToNSFont();
65 // Used only for logging. 65 // Used only for logging.
66 std::string font_name([[font_to_encode fontName] UTF8String]); 66 std::string font_name([[font_to_encode fontName] UTF8String]);
67 67
68 // Load appropriate NSFont. 68 // Load appropriate NSFont.
69 if (!font_to_encode) { 69 if (!font_to_encode) {
70 DLOG(ERROR) << "Failed to load font " << font_name; 70 DLOG(ERROR) << "Failed to load font " << font_name;
71 return false; 71 return;
72 } 72 }
73 73
74 // NSFont -> ATSFontRef. 74 // NSFont -> ATSFontRef.
75 ATSFontRef ats_font = 75 ATSFontRef ats_font =
76 CTFontGetPlatformFont(reinterpret_cast<CTFontRef>(font_to_encode), NULL); 76 CTFontGetPlatformFont(reinterpret_cast<CTFontRef>(font_to_encode), NULL);
77 if (!ats_font) { 77 if (!ats_font) {
78 DLOG(ERROR) << "Conversion to ATSFontRef failed for " << font_name; 78 DLOG(ERROR) << "Conversion to ATSFontRef failed for " << font_name;
79 return false; 79 return;
80 } 80 }
81 81
82 // Retrieve the ATSFontContainerRef corresponding to the font file we want to 82 // Retrieve the ATSFontContainerRef corresponding to the font file we want to
83 // load. This is a unique identifier that allows the caller determine if the 83 // load. This is a unique identifier that allows the caller determine if the
84 // font file in question is already loaded. 84 // font file in question is already loaded.
85 COMPILE_ASSERT(sizeof(ATSFontContainerRef) == sizeof(font_id), 85 COMPILE_ASSERT(sizeof(ATSFontContainerRef) == sizeof(&result->font_id),
86 uint32_cant_hold_fontcontainer_ref); 86 uint32_cant_hold_fontcontainer_ref);
87 ATSFontContainerRef fontContainer = kATSFontContainerRefUnspecified; 87 ATSFontContainerRef fontContainer = kATSFontContainerRefUnspecified;
88 if (ATSFontGetContainer(ats_font, 0, &fontContainer) != noErr) { 88 if (ATSFontGetContainer(ats_font, 0, &fontContainer) != noErr) {
89 DLOG(ERROR) << "Failed to get font container ref for " << font_name; 89 DLOG(ERROR) << "Failed to get font container ref for " << font_name;
90 return false; 90 return;
91 } 91 }
92 92
93 // ATSFontRef -> File path. 93 // ATSFontRef -> File path.
94 // Warning: Calling this function on a font activated from memory will result 94 // Warning: Calling this function on a font activated from memory will result
95 // in failure with a -50 - paramErr. This may occur if 95 // in failure with a -50 - paramErr. This may occur if
96 // CreateCGFontFromBuffer() is called in the same process as this function 96 // CreateCGFontFromBuffer() is called in the same process as this function
97 // e.g. when writing a unit test that exercises these two functions together. 97 // e.g. when writing a unit test that exercises these two functions together.
98 // If said unit test were to load a system font and activate it from memory 98 // If said unit test were to load a system font and activate it from memory
99 // it becomes impossible for the system to the find the original file ref 99 // it becomes impossible for the system to the find the original file ref
100 // since the font now lives in memory as far as it's concerned. 100 // since the font now lives in memory as far as it's concerned.
101 FSRef font_fsref; 101 FSRef font_fsref;
102 if (ATSFontGetFileReference(ats_font, &font_fsref) != noErr) { 102 if (ATSFontGetFileReference(ats_font, &font_fsref) != noErr) {
103 DLOG(ERROR) << "Failed to find font file for " << font_name; 103 DLOG(ERROR) << "Failed to find font file for " << font_name;
104 return false; 104 return;
105 } 105 }
106 FilePath font_path = FilePath(base::mac::PathFromFSRef(font_fsref)); 106 FilePath font_path = FilePath(base::mac::PathFromFSRef(font_fsref));
107 107
108 // Load file into shared memory buffer. 108 // Load file into shared memory buffer.
109 int64 font_file_size_64 = -1; 109 int64 font_file_size_64 = -1;
110 if (!file_util::GetFileSize(font_path, &font_file_size_64)) { 110 if (!file_util::GetFileSize(font_path, &font_file_size_64)) {
111 DLOG(ERROR) << "Couldn't get font file size for " << font_path.value(); 111 DLOG(ERROR) << "Couldn't get font file size for " << font_path.value();
112 return false; 112 return;
113 } 113 }
114 114
115 if (font_file_size_64 <= 0 || font_file_size_64 >= kint32max) { 115 if (font_file_size_64 <= 0 || font_file_size_64 >= kint32max) {
116 DLOG(ERROR) << "Bad size for font file " << font_path.value(); 116 DLOG(ERROR) << "Bad size for font file " << font_path.value();
117 return false; 117 return;
118 } 118 }
119 119
120 int32 font_file_size_32 = static_cast<int32>(font_file_size_64); 120 int32 font_file_size_32 = static_cast<int32>(font_file_size_64);
121 if (!font_data->CreateAndMapAnonymous(font_file_size_32)) { 121 if (!result->font_data.CreateAndMapAnonymous(font_file_size_32)) {
122 DLOG(ERROR) << "Failed to create shmem area for " << font_name; 122 DLOG(ERROR) << "Failed to create shmem area for " << font_name;
123 return false; 123 return;
124 } 124 }
125 125
126 int32 amt_read = file_util::ReadFile(font_path, 126 int32 amt_read = file_util::ReadFile(font_path,
127 reinterpret_cast<char*>(font_data->memory()), 127 reinterpret_cast<char*>(result->font_data.memory()),
128 font_file_size_32); 128 font_file_size_32);
129 if (amt_read != font_file_size_32) { 129 if (amt_read != font_file_size_32) {
130 DLOG(ERROR) << "Failed to read font data for " << font_path.value(); 130 DLOG(ERROR) << "Failed to read font data for " << font_path.value();
131 return false; 131 return;
132 } 132 }
133 133
134 *font_data_size = font_file_size_32; 134 result->font_data_size = font_file_size_32;
135 *font_id = fontContainer; 135 result->font_id = fontContainer;
136 return true;
137 } 136 }
138 137
139 // static 138 // static
140 bool FontLoader::CGFontRefFromBuffer(base::SharedMemoryHandle font_data, 139 bool FontLoader::CGFontRefFromBuffer(base::SharedMemoryHandle font_data,
141 uint32 font_data_size, 140 uint32 font_data_size,
142 CGFontRef* out) { 141 CGFontRef* out) {
143 *out = NULL; 142 *out = NULL;
144 143
145 using base::SharedMemory; 144 using base::SharedMemory;
146 DCHECK(SharedMemory::IsHandleValid(font_data)); 145 DCHECK(SharedMemory::IsHandleValid(font_data));
(...skipping 10 matching lines...) Expand all
157 if (!provider) 156 if (!provider)
158 return false; 157 return false;
159 158
160 *out = CGFontCreateWithDataProvider(provider.get()); 159 *out = CGFontCreateWithDataProvider(provider.get());
161 160
162 if (*out == NULL) 161 if (*out == NULL)
163 return false; 162 return false;
164 163
165 return true; 164 return true;
166 } 165 }
OLDNEW
« no previous file with comments | « content/common/mac/font_loader.h ('k') | content/common/sandbox_mac_fontloading_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698