| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "webkit/support/platform_support.h" | |
| 6 | |
| 7 #import <AppKit/AppKit.h> | |
| 8 #include <AvailabilityMacros.h> | |
| 9 #import <Foundation/Foundation.h> | |
| 10 #import <objc/objc-runtime.h> | |
| 11 | |
| 12 #include "base/base_paths.h" | |
| 13 #include "base/file_util.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/mac/bundle_locations.h" | |
| 16 #include "base/mac/mac_util.h" | |
| 17 #include "base/path_service.h" | |
| 18 #include "base/strings/string16.h" | |
| 19 #include "base/strings/utf_string_conversions.h" | |
| 20 #include "grit/webkit_resources.h" | |
| 21 #include "ui/base/resource/data_pack.h" | |
| 22 #include "webkit/plugins/npapi/plugin_list.h" | |
| 23 #import "webkit/support/drt_application_mac.h" | |
| 24 #import "webkit/support/mac/DumpRenderTreePasteboard.h" | |
| 25 #include "webkit/support/test_webkit_platform_support.h" | |
| 26 | |
| 27 static ui::DataPack* g_resource_data_pack = NULL; | |
| 28 | |
| 29 namespace webkit_support { | |
| 30 | |
| 31 static NSAutoreleasePool* autorelease_pool; | |
| 32 | |
| 33 void BeforeInitialize() { | |
| 34 [CrDrtApplication sharedApplication]; | |
| 35 autorelease_pool = [[NSAutoreleasePool alloc] init]; | |
| 36 DCHECK(autorelease_pool); | |
| 37 } | |
| 38 | |
| 39 #if OBJC_API_VERSION == 2 | |
| 40 static void SwizzleAllMethods(Class imposter, Class original) { | |
| 41 unsigned int imposterMethodCount = 0; | |
| 42 Method* imposterMethods = | |
| 43 class_copyMethodList(imposter, &imposterMethodCount); | |
| 44 | |
| 45 unsigned int originalMethodCount = 0; | |
| 46 Method* originalMethods = | |
| 47 class_copyMethodList(original, &originalMethodCount); | |
| 48 | |
| 49 for (unsigned int i = 0; i < imposterMethodCount; i++) { | |
| 50 SEL imposterMethodName = method_getName(imposterMethods[i]); | |
| 51 | |
| 52 // Attempt to add the method to the original class. If it fails, the method | |
| 53 // already exists and we should instead exchange the implementations. | |
| 54 if (class_addMethod(original, | |
| 55 imposterMethodName, | |
| 56 method_getImplementation(originalMethods[i]), | |
| 57 method_getTypeEncoding(originalMethods[i]))) { | |
| 58 continue; | |
| 59 } | |
| 60 | |
| 61 unsigned int j = 0; | |
| 62 for (; j < originalMethodCount; j++) { | |
| 63 SEL originalMethodName = method_getName(originalMethods[j]); | |
| 64 if (sel_isEqual(imposterMethodName, originalMethodName)) { | |
| 65 break; | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 // If class_addMethod failed above then the method must exist on the | |
| 70 // original class. | |
| 71 DCHECK(j < originalMethodCount) << "method wasn't found?"; | |
| 72 method_exchangeImplementations(imposterMethods[i], originalMethods[j]); | |
| 73 } | |
| 74 | |
| 75 if (imposterMethods) { | |
| 76 free(imposterMethods); | |
| 77 } | |
| 78 if (originalMethods) { | |
| 79 free(originalMethods); | |
| 80 } | |
| 81 } | |
| 82 #endif | |
| 83 | |
| 84 static void SwizzleNSPasteboard() { | |
| 85 // We replace NSPaseboard w/ the shim (from WebKit) that avoids having | |
| 86 // sideeffects w/ whatever the user does at the same time. | |
| 87 | |
| 88 Class imposterClass = objc_getClass("DumpRenderTreePasteboard"); | |
| 89 Class originalClass = objc_getClass("NSPasteboard"); | |
| 90 #if OBJC_API_VERSION == 0 | |
| 91 class_poseAs(imposterClass, originalClass); | |
| 92 #else | |
| 93 // Swizzle instance methods... | |
| 94 SwizzleAllMethods(imposterClass, originalClass); | |
| 95 // and then class methods. | |
| 96 SwizzleAllMethods(object_getClass(imposterClass), | |
| 97 object_getClass(originalClass)); | |
| 98 #endif | |
| 99 } | |
| 100 | |
| 101 void AfterInitialize() { | |
| 102 // Load a data pack. | |
| 103 g_resource_data_pack = new ui::DataPack(ui::SCALE_FACTOR_100P); | |
| 104 base::FilePath resources_pak_path; | |
| 105 PathService::Get(base::DIR_EXE, &resources_pak_path); | |
| 106 resources_pak_path = resources_pak_path.Append("Content Shell.app") | |
| 107 .Append("Contents") | |
| 108 .Append("Frameworks") | |
| 109 .Append("Content Shell Framework.framework") | |
| 110 .Append("Resources") | |
| 111 .Append("content_shell.pak"); | |
| 112 if (!g_resource_data_pack->LoadFromPath(resources_pak_path)) { | |
| 113 LOG(FATAL) << "failed to load DumpRenderTree.pak"; | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 void BeforeShutdown() { | |
| 118 } | |
| 119 | |
| 120 void AfterShutdown() { | |
| 121 [DumpRenderTreePasteboard releaseLocalPasteboards]; | |
| 122 [autorelease_pool drain]; | |
| 123 delete g_resource_data_pack; | |
| 124 } | |
| 125 | |
| 126 } // namespace webkit_support | |
| 127 | |
| 128 base::string16 TestWebKitPlatformSupport::GetLocalizedString(int message_id) { | |
| 129 // |g_resource_data_pack| is null on unit tests. | |
| 130 // But som unit tests reach GetLocalizedString(). | |
| 131 if (!g_resource_data_pack) | |
| 132 return base::string16(); | |
| 133 base::StringPiece res; | |
| 134 if (!g_resource_data_pack->GetStringPiece(message_id, &res)) { | |
| 135 LOG(FATAL) << "failed to load webkit string with id " << message_id; | |
| 136 } | |
| 137 | |
| 138 // Data packs hold strings as either UTF8 or UTF16. | |
| 139 base::string16 msg; | |
| 140 switch (g_resource_data_pack->GetTextEncodingType()) { | |
| 141 case ui::DataPack::UTF8: | |
| 142 msg = UTF8ToUTF16(res); | |
| 143 break; | |
| 144 case ui::DataPack::UTF16: | |
| 145 msg = base::string16(reinterpret_cast<const char16*>(res.data()), | |
| 146 res.length() / 2); | |
| 147 break; | |
| 148 case ui::DataPack::BINARY: | |
| 149 NOTREACHED(); | |
| 150 break; | |
| 151 } | |
| 152 | |
| 153 return msg; | |
| 154 } | |
| 155 | |
| 156 // Helper method for getting the path to the test shell resources directory. | |
| 157 static base::FilePath GetResourcesFilePath() { | |
| 158 base::FilePath path; | |
| 159 // We assume the application is bundled. | |
| 160 if (!base::mac::AmIBundled()) { | |
| 161 LOG(FATAL) << "Failed to locate resources. The applicaiton is not bundled."; | |
| 162 } | |
| 163 PathService::Get(base::DIR_EXE, &path); | |
| 164 path = path.Append(base::FilePath::kParentDirectory); | |
| 165 return path.AppendASCII("Resources"); | |
| 166 } | |
| 167 | |
| 168 base::StringPiece TestWebKitPlatformSupport::GetDataResource( | |
| 169 int resource_id, | |
| 170 ui::ScaleFactor scale_factor) { | |
| 171 switch (resource_id) { | |
| 172 case IDR_BROKENIMAGE: { | |
| 173 // Use webkit's broken image icon (16x16) | |
| 174 CR_DEFINE_STATIC_LOCAL(std::string, broken_image_data, ()); | |
| 175 if (broken_image_data.empty()) { | |
| 176 base::FilePath path = GetResourcesFilePath(); | |
| 177 // In order to match WebKit's colors for the missing image, we have to | |
| 178 // use a PNG. The GIF doesn't have the color range needed to correctly | |
| 179 // match the TIFF they use in Safari. | |
| 180 path = base::MakeAbsoluteFilePath(path.AppendASCII("missingImage.png")); | |
| 181 bool success = file_util::ReadFileToString(path, &broken_image_data); | |
| 182 if (!success) { | |
| 183 LOG(FATAL) << "Failed reading: " << path.value(); | |
| 184 } | |
| 185 } | |
| 186 return broken_image_data; | |
| 187 } | |
| 188 case IDR_TEXTAREA_RESIZER: { | |
| 189 // Use webkit's text area resizer image. | |
| 190 CR_DEFINE_STATIC_LOCAL(std::string, resize_corner_data, ()); | |
| 191 if (resize_corner_data.empty()) { | |
| 192 base::FilePath path = GetResourcesFilePath(); | |
| 193 path = base::MakeAbsoluteFilePath( | |
| 194 path.AppendASCII("textAreaResizeCorner.png")); | |
| 195 bool success = file_util::ReadFileToString(path, &resize_corner_data); | |
| 196 if (!success) { | |
| 197 LOG(FATAL) << "Failed reading: " << path.value(); | |
| 198 } | |
| 199 } | |
| 200 return resize_corner_data; | |
| 201 } | |
| 202 } | |
| 203 base::StringPiece res; | |
| 204 if (g_resource_data_pack) | |
| 205 g_resource_data_pack->GetStringPiece(resource_id, &res); | |
| 206 return res; | |
| 207 } | |
| OLD | NEW |