| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "base/clipboard.h" | 5 #include "base/clipboard.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/sys_string_conversions.h" |
| 11 | 12 |
| 12 namespace { | 13 namespace { |
| 13 | 14 |
| 14 // Would be nice if this were in UTCoreTypes.h, but it isn't | 15 // Would be nice if this were in UTCoreTypes.h, but it isn't |
| 15 const NSString* kUTTypeURLName = @"public.url-name"; | 16 const NSString* kUTTypeURLName = @"public.url-name"; |
| 16 | 17 |
| 17 NSString* nsStringForWString(const std::wstring& string) { | 18 NSString* NSStringForWString(const std::wstring& string) { |
| 18 string16 text16 = WideToUTF16(string); | 19 CFStringRef cfstring = base::SysWideToCFStringRef(string); |
| 19 return [NSString stringWithCharacters:text16.c_str() length:text16.length()]; | 20 NSString* nsstring = (NSString*)cfstring; |
| 21 [[nsstring retain] autorelease]; |
| 22 CFRelease(cfstring); |
| 23 return nsstring; |
| 24 } |
| 25 |
| 26 NSPasteboard* GetPasteboard() { |
| 27 // The pasteboard should not be nil in a UI session, but this handy DCHECK |
| 28 // can help track down problems if someone tries using clipboard code outside |
| 29 // of a UI session. |
| 30 NSPasteboard* pasteboard = [NSPasteboard generalPasteboard]; |
| 31 DCHECK(pasteboard); |
| 32 return pasteboard; |
| 20 } | 33 } |
| 21 | 34 |
| 22 } // namespace | 35 } // namespace |
| 23 | 36 |
| 24 Clipboard::Clipboard() { | 37 Clipboard::Clipboard() { |
| 25 } | 38 } |
| 26 | 39 |
| 27 Clipboard::~Clipboard() { | 40 Clipboard::~Clipboard() { |
| 28 } | 41 } |
| 29 | 42 |
| 30 void Clipboard::Clear() { | 43 void Clipboard::Clear() { |
| 31 NSPasteboard* pb = [NSPasteboard generalPasteboard]; | 44 NSPasteboard* pb = GetPasteboard(); |
| 32 [pb declareTypes:[NSArray array] owner:nil]; | 45 [pb declareTypes:[NSArray array] owner:nil]; |
| 33 } | 46 } |
| 34 | 47 |
| 35 void Clipboard::WriteText(const std::wstring& text) { | 48 void Clipboard::WriteText(const std::wstring& text) { |
| 36 NSPasteboard* pb = [NSPasteboard generalPasteboard]; | 49 NSPasteboard* pb = GetPasteboard(); |
| 37 [pb addTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; | 50 [pb addTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; |
| 38 [pb setString:nsStringForWString(text) forType:NSStringPboardType]; | 51 [pb setString:NSStringForWString(text) forType:NSStringPboardType]; |
| 39 } | 52 } |
| 40 | 53 |
| 41 void Clipboard::WriteHTML(const std::wstring& markup, | 54 void Clipboard::WriteHTML(const std::wstring& markup, |
| 42 const std::string& src_url) { | 55 const std::string& src_url) { |
| 43 // TODO(avi): src_url? | 56 // TODO(avi): src_url? |
| 44 NSPasteboard* pb = [NSPasteboard generalPasteboard]; | 57 NSPasteboard* pb = GetPasteboard(); |
| 45 [pb addTypes:[NSArray arrayWithObject:NSHTMLPboardType] owner:nil]; | 58 [pb addTypes:[NSArray arrayWithObject:NSHTMLPboardType] owner:nil]; |
| 46 [pb setString:nsStringForWString(markup) forType:NSHTMLPboardType]; | 59 [pb setString:NSStringForWString(markup) forType:NSHTMLPboardType]; |
| 47 } | 60 } |
| 48 | 61 |
| 49 void Clipboard::WriteBookmark(const std::wstring& title, | 62 void Clipboard::WriteBookmark(const std::wstring& title, |
| 50 const std::string& url) { | 63 const std::string& url) { |
| 51 WriteHyperlink(title, url); | 64 WriteHyperlink(title, url); |
| 52 } | 65 } |
| 53 | 66 |
| 54 void Clipboard::WriteHyperlink(const std::wstring& title, | 67 void Clipboard::WriteHyperlink(const std::wstring& title, |
| 55 const std::string& url) { | 68 const std::string& url) { |
| 56 // TODO(playmobil): In the Windows version of this function, an HTML | 69 // TODO(playmobil): In the Windows version of this function, an HTML |
| 57 // representation of the bookmark is also added to the clipboard, to support | 70 // representation of the bookmark is also added to the clipboard, to support |
| 58 // drag and drop of web shortcuts. I don't think we need to do this on the | 71 // drag and drop of web shortcuts. I don't think we need to do this on the |
| 59 // Mac, but we should double check later on. | 72 // Mac, but we should double check later on. |
| 60 NSURL* nsurl = [NSURL URLWithString: | 73 NSURL* nsurl = [NSURL URLWithString: |
| 61 [NSString stringWithUTF8String:url.c_str()]]; | 74 [NSString stringWithUTF8String:url.c_str()]]; |
| 62 NSString* nstitle = nsStringForWString(title); | 75 NSString* nstitle = NSStringForWString(title); |
| 63 | 76 |
| 64 NSPasteboard* pb = [NSPasteboard generalPasteboard]; | 77 NSPasteboard* pb = GetPasteboard(); |
| 65 // passing UTIs into the pasteboard methods is valid >= 10.5 | 78 // passing UTIs into the pasteboard methods is valid >= 10.5 |
| 66 [pb addTypes:[NSArray arrayWithObjects:NSURLPboardType, | 79 [pb addTypes:[NSArray arrayWithObjects:NSURLPboardType, |
| 67 kUTTypeURLName, nil] | 80 kUTTypeURLName, |
| 68 owner:nil]; | 81 nil] |
| 82 owner:nil]; |
| 69 [nsurl writeToPasteboard:pb]; | 83 [nsurl writeToPasteboard:pb]; |
| 70 [pb setString:nstitle forType:kUTTypeURLName]; | 84 [pb setString:nstitle forType:kUTTypeURLName]; |
| 71 } | 85 } |
| 72 | 86 |
| 73 void Clipboard::WriteFile(const std::wstring& file) { | 87 void Clipboard::WriteFile(const std::wstring& file) { |
| 74 std::vector<std::wstring> files; | 88 std::vector<std::wstring> files; |
| 75 files.push_back(file); | 89 files.push_back(file); |
| 76 WriteFiles(files); | 90 WriteFiles(files); |
| 77 } | 91 } |
| 78 | 92 |
| 79 void Clipboard::WriteFiles(const std::vector<std::wstring>& files) { | 93 void Clipboard::WriteFiles(const std::vector<std::wstring>& files) { |
| 80 NSMutableArray* fileList = [NSMutableArray arrayWithCapacity:files.size()]; | 94 NSMutableArray* fileList = [NSMutableArray arrayWithCapacity:files.size()]; |
| 81 for (unsigned int i = 0; i < files.size(); ++i) { | 95 for (size_t i = 0; i < files.size(); ++i) { |
| 82 [fileList addObject:nsStringForWString(files[i])]; | 96 [fileList addObject:NSStringForWString(files[i])]; |
| 83 } | 97 } |
| 84 | 98 |
| 85 NSPasteboard* pb = [NSPasteboard generalPasteboard]; | 99 NSPasteboard* pb = GetPasteboard(); |
| 86 [pb addTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:nil]; | 100 [pb addTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:nil]; |
| 87 [pb setPropertyList:fileList forType:NSFilenamesPboardType]; | 101 [pb setPropertyList:fileList forType:NSFilenamesPboardType]; |
| 88 } | 102 } |
| 89 | 103 |
| 90 bool Clipboard::IsFormatAvailable(NSString* format) const { | 104 bool Clipboard::IsFormatAvailable(NSString* format) const { |
| 91 NSPasteboard* pb = [NSPasteboard generalPasteboard]; | 105 NSPasteboard* pb = GetPasteboard(); |
| 92 NSArray* types = [pb types]; | 106 NSArray* types = [pb types]; |
| 93 | 107 |
| 94 return [types containsObject:format]; | 108 return [types containsObject:format]; |
| 95 } | 109 } |
| 96 | 110 |
| 97 void Clipboard::ReadText(std::wstring* result) const { | 111 void Clipboard::ReadText(std::wstring* result) const { |
| 98 NSPasteboard* pb = [NSPasteboard generalPasteboard]; | 112 NSPasteboard* pb = GetPasteboard(); |
| 99 NSString* contents = [pb stringForType:NSStringPboardType]; | 113 NSString* contents = [pb stringForType:NSStringPboardType]; |
| 100 | 114 |
| 101 UTF8ToWide([contents UTF8String], | 115 UTF8ToWide([contents UTF8String], |
| 102 [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding], | 116 [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding], |
| 103 result); | 117 result); |
| 104 } | 118 } |
| 105 | 119 |
| 106 void Clipboard::ReadAsciiText(std::string* result) const { | 120 void Clipboard::ReadAsciiText(std::string* result) const { |
| 107 NSPasteboard* pb = [NSPasteboard generalPasteboard]; | 121 NSPasteboard* pb = GetPasteboard(); |
| 108 NSString* contents = [pb stringForType:NSStringPboardType]; | 122 NSString* contents = [pb stringForType:NSStringPboardType]; |
| 109 | 123 |
| 110 *result = std::string([contents UTF8String]); | 124 if (!contents) |
| 125 result->clear(); |
| 126 else |
| 127 result->assign([contents UTF8String]); |
| 111 } | 128 } |
| 112 | 129 |
| 113 void Clipboard::ReadHTML(std::wstring* markup, std::string* src_url) const { | 130 void Clipboard::ReadHTML(std::wstring* markup, std::string* src_url) const { |
| 114 if (markup) { | 131 if (markup) { |
| 115 markup->clear(); | 132 NSPasteboard* pb = GetPasteboard(); |
| 116 | |
| 117 NSPasteboard* pb = [NSPasteboard generalPasteboard]; | |
| 118 NSArray *supportedTypes = [NSArray arrayWithObjects:NSHTMLPboardType, | 133 NSArray *supportedTypes = [NSArray arrayWithObjects:NSHTMLPboardType, |
| 119 NSStringPboardType, | 134 NSStringPboardType, |
| 120 nil]; | 135 nil]; |
| 121 NSString *bestType = [pb availableTypeFromArray:supportedTypes]; | 136 NSString *bestType = [pb availableTypeFromArray:supportedTypes]; |
| 122 NSString* contents = [pb stringForType:bestType]; | 137 NSString* contents = [pb stringForType:bestType]; |
| 123 UTF8ToWide([contents UTF8String], | 138 UTF8ToWide([contents UTF8String], |
| 124 [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding], | 139 [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding], |
| 125 markup); | 140 markup); |
| 126 } | 141 } |
| 127 | 142 |
| 128 // TODO(avi): src_url? | 143 // TODO(avi): src_url? |
| 129 if (src_url) | 144 if (src_url) |
| 130 src_url->clear(); | 145 src_url->clear(); |
| 131 } | 146 } |
| 132 | 147 |
| 133 void Clipboard::ReadBookmark(std::wstring* title, std::string* url) const { | 148 void Clipboard::ReadBookmark(std::wstring* title, std::string* url) const { |
| 134 NSPasteboard* pb = [NSPasteboard generalPasteboard]; | 149 NSPasteboard* pb = GetPasteboard(); |
| 135 | 150 |
| 136 if (title) { | 151 if (title) { |
| 137 title->clear(); | |
| 138 | |
| 139 NSString* contents = [pb stringForType:kUTTypeURLName]; | 152 NSString* contents = [pb stringForType:kUTTypeURLName]; |
| 140 UTF8ToWide([contents UTF8String], | 153 UTF8ToWide([contents UTF8String], |
| 141 [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding], | 154 [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding], |
| 142 title); | 155 title); |
| 143 } | 156 } |
| 144 | 157 |
| 145 if (url) { | 158 if (url) { |
| 146 url->clear(); | 159 NSString* url_string = [[NSURL URLFromPasteboard:pb] absoluteString]; |
| 147 | 160 if (!url_string) |
| 148 NSURL* nsurl = [NSURL URLFromPasteboard:pb]; | 161 url->clear(); |
| 149 *url = std::string([[nsurl absoluteString] UTF8String]); | 162 else |
| 163 url->assign([url_string UTF8String]); |
| 150 } | 164 } |
| 151 } | 165 } |
| 152 | 166 |
| 153 void Clipboard::ReadFile(std::wstring* file) const { | 167 void Clipboard::ReadFile(std::wstring* file) const { |
| 154 if (!file) { | 168 if (!file) { |
| 155 NOTREACHED(); | 169 NOTREACHED(); |
| 156 return; | 170 return; |
| 157 } | 171 } |
| 158 | 172 |
| 159 file->clear(); | 173 file->clear(); |
| 160 std::vector<std::wstring> files; | 174 std::vector<std::wstring> files; |
| 161 ReadFiles(&files); | 175 ReadFiles(&files); |
| 162 | 176 |
| 163 // Take the first file, if available. | 177 // Take the first file, if available. |
| 164 if (!files.empty()) | 178 if (!files.empty()) |
| 165 file->assign(files[0]); | 179 file->assign(files[0]); |
| 166 } | 180 } |
| 167 | 181 |
| 168 void Clipboard::ReadFiles(std::vector<std::wstring>* files) const { | 182 void Clipboard::ReadFiles(std::vector<std::wstring>* files) const { |
| 169 if (!files) { | 183 if (!files) { |
| 170 NOTREACHED(); | 184 NOTREACHED(); |
| 171 return; | 185 return; |
| 172 } | 186 } |
| 173 | 187 |
| 174 files->clear(); | 188 files->clear(); |
| 175 | 189 |
| 176 NSPasteboard* pb = [NSPasteboard generalPasteboard]; | 190 NSPasteboard* pb = GetPasteboard(); |
| 177 NSArray* fileList = [pb propertyListForType:NSFilenamesPboardType]; | 191 NSArray* fileList = [pb propertyListForType:NSFilenamesPboardType]; |
| 178 | 192 |
| 179 for (unsigned int i = 0; i < [fileList count]; ++i) { | 193 for (unsigned int i = 0; i < [fileList count]; ++i) { |
| 180 std::wstring file = UTF8ToWide([[fileList objectAtIndex:i] UTF8String]); | 194 std::wstring file = UTF8ToWide([[fileList objectAtIndex:i] UTF8String]); |
| 181 files->push_back(file); | 195 files->push_back(file); |
| 182 } | 196 } |
| 183 } | 197 } |
| 184 | 198 |
| 185 // static | 199 // static |
| 186 Clipboard::FormatType Clipboard::GetUrlFormatType() { | 200 Clipboard::FormatType Clipboard::GetUrlFormatType() { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 209 | 223 |
| 210 // static | 224 // static |
| 211 Clipboard::FormatType Clipboard::GetFilenameWFormatType() { | 225 Clipboard::FormatType Clipboard::GetFilenameWFormatType() { |
| 212 return NSFilenamesPboardType; | 226 return NSFilenamesPboardType; |
| 213 } | 227 } |
| 214 | 228 |
| 215 // static | 229 // static |
| 216 Clipboard::FormatType Clipboard::GetHtmlFormatType() { | 230 Clipboard::FormatType Clipboard::GetHtmlFormatType() { |
| 217 return NSHTMLPboardType; | 231 return NSHTMLPboardType; |
| 218 } | 232 } |
| OLD | NEW |