Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/bookmarks/browser/bookmark_pasteboard_helper_mac.h" | 5 #include "components/bookmarks/browser/bookmark_pasteboard_helper_mac.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 | 24 |
| 25 // Pasteboard type used to store profile path to determine which profile | 25 // Pasteboard type used to store profile path to determine which profile |
| 26 // a set of bookmarks came from. | 26 // a set of bookmarks came from. |
| 27 NSString* const kChromiumProfilePathPboardType = | 27 NSString* const kChromiumProfilePathPboardType = |
| 28 @"ChromiumProfilePathPboardType"; | 28 @"ChromiumProfilePathPboardType"; |
| 29 | 29 |
| 30 // Internal bookmark ID for a bookmark node. Used only when moving inside | 30 // Internal bookmark ID for a bookmark node. Used only when moving inside |
| 31 // of one profile. | 31 // of one profile. |
| 32 NSString* const kChromiumBookmarkId = @"ChromiumBookmarkId"; | 32 NSString* const kChromiumBookmarkId = @"ChromiumBookmarkId"; |
| 33 | 33 |
| 34 // Internal bookmark meta info dictionary for a bookmark node. | |
| 35 NSString* const kChromiumBookmarkMetaInfo = @"ChromiumBookmarkMetaInfo"; | |
| 36 | |
| 34 // Mac WebKit uses this type, declared in | 37 // Mac WebKit uses this type, declared in |
| 35 // WebKit/mac/History/WebURLsWithTitles.h. | 38 // WebKit/mac/History/WebURLsWithTitles.h. |
| 36 NSString* const kCrWebURLsWithTitlesPboardType = @"WebURLsWithTitlesPboardType"; | 39 NSString* const kCrWebURLsWithTitlesPboardType = @"WebURLsWithTitlesPboardType"; |
| 37 | 40 |
| 38 // Keys for the type of node in BookmarkDictionaryListPboardType. | 41 // Keys for the type of node in BookmarkDictionaryListPboardType. |
| 39 NSString* const kWebBookmarkType = @"WebBookmarkType"; | 42 NSString* const kWebBookmarkType = @"WebBookmarkType"; |
| 40 | 43 |
| 41 NSString* const kWebBookmarkTypeList = @"WebBookmarkTypeList"; | 44 NSString* const kWebBookmarkTypeList = @"WebBookmarkTypeList"; |
| 42 | 45 |
| 43 NSString* const kWebBookmarkTypeLeaf = @"WebBookmarkTypeLeaf"; | 46 NSString* const kWebBookmarkTypeLeaf = @"WebBookmarkTypeLeaf"; |
| 44 | 47 |
| 48 BookmarkNode::MetaInfoMap MetaInfoMapFromDictionary(NSDictionary* dictionary) { | |
| 49 BookmarkNode::MetaInfoMap meta_info_map; | |
| 50 | |
| 51 for (NSString* key in dictionary) { | |
| 52 NSString* value = dictionary[key]; | |
| 53 meta_info_map[base::SysNSStringToUTF8(key)] = | |
| 54 base::SysNSStringToUTF8(value); | |
| 55 } | |
| 56 | |
| 57 return meta_info_map; | |
| 58 } | |
| 59 | |
| 45 void ConvertPlistToElements(NSArray* input, | 60 void ConvertPlistToElements(NSArray* input, |
| 46 std::vector<BookmarkNodeData::Element>& elements) { | 61 std::vector<BookmarkNodeData::Element>& elements) { |
| 47 NSUInteger len = [input count]; | 62 NSUInteger len = [input count]; |
| 48 for (NSUInteger i = 0; i < len; ++i) { | 63 for (NSUInteger i = 0; i < len; ++i) { |
| 49 NSDictionary* pboardBookmark = [input objectAtIndex:i]; | 64 NSDictionary* pboardBookmark = [input objectAtIndex:i]; |
| 50 scoped_ptr<BookmarkNode> new_node(new BookmarkNode(GURL())); | 65 scoped_ptr<BookmarkNode> new_node(new BookmarkNode(GURL())); |
| 51 int64 node_id = | 66 int64 node_id = |
| 52 [[pboardBookmark objectForKey:kChromiumBookmarkId] longLongValue]; | 67 [[pboardBookmark objectForKey:kChromiumBookmarkId] longLongValue]; |
| 53 new_node->set_id(node_id); | 68 new_node->set_id(node_id); |
| 69 | |
| 70 NSDictionary* metaInfoDictionary = | |
| 71 [pboardBookmark objectForKey:kChromiumBookmarkMetaInfo]; | |
| 72 if (metaInfoDictionary) | |
| 73 new_node->SetMetaInfoMap(MetaInfoMapFromDictionary(metaInfoDictionary)); | |
| 74 | |
| 54 BOOL is_folder = [[pboardBookmark objectForKey:kWebBookmarkType] | 75 BOOL is_folder = [[pboardBookmark objectForKey:kWebBookmarkType] |
| 55 isEqualToString:kWebBookmarkTypeList]; | 76 isEqualToString:kWebBookmarkTypeList]; |
| 56 if (is_folder) { | 77 if (is_folder) { |
| 57 new_node->set_type(BookmarkNode::FOLDER); | 78 new_node->set_type(BookmarkNode::FOLDER); |
| 58 NSString* title = [pboardBookmark objectForKey:@"Title"]; | 79 NSString* title = [pboardBookmark objectForKey:@"Title"]; |
| 59 new_node->SetTitle(base::SysNSStringToUTF16(title)); | 80 new_node->SetTitle(base::SysNSStringToUTF16(title)); |
| 60 } else { | 81 } else { |
| 61 new_node->set_type(BookmarkNode::URL); | 82 new_node->set_type(BookmarkNode::URL); |
| 62 NSDictionary* uriDictionary = | 83 NSDictionary* uriDictionary = |
| 63 [pboardBookmark objectForKey:@"URIDictionary"]; | 84 [pboardBookmark objectForKey:@"URIDictionary"]; |
| 64 NSString* title = [uriDictionary objectForKey:@"title"]; | 85 NSString* title = [uriDictionary objectForKey:@"title"]; |
| 65 NSString* urlString = [pboardBookmark objectForKey:@"URLString"]; | 86 NSString* urlString = [pboardBookmark objectForKey:@"URLString"]; |
| 66 new_node->SetTitle(base::SysNSStringToUTF16(title)); | 87 new_node->SetTitle(base::SysNSStringToUTF16(title)); |
| 67 new_node->set_url(GURL(base::SysNSStringToUTF8(urlString))); | 88 new_node->set_url(GURL(base::SysNSStringToUTF8(urlString))); |
| 68 } | 89 } |
| 69 BookmarkNodeData::Element e = BookmarkNodeData::Element(new_node.get()); | 90 BookmarkNodeData::Element e = BookmarkNodeData::Element(new_node.get()); |
| 70 if(is_folder) | 91 if (is_folder) { |
| 71 ConvertPlistToElements([pboardBookmark objectForKey:@"Children"], | 92 ConvertPlistToElements([pboardBookmark objectForKey:@"Children"], |
| 72 e.children); | 93 e.children); |
| 94 } | |
| 73 elements.push_back(e); | 95 elements.push_back(e); |
| 74 } | 96 } |
| 75 } | 97 } |
| 76 | 98 |
| 77 bool ReadBookmarkDictionaryListPboardType( | 99 bool ReadBookmarkDictionaryListPboardType( |
| 78 NSPasteboard* pb, | 100 NSPasteboard* pb, |
| 79 std::vector<BookmarkNodeData::Element>& elements) { | 101 std::vector<BookmarkNodeData::Element>& elements) { |
| 80 NSArray* bookmarks = | 102 NSArray* bookmarks = |
| 81 [pb propertyListForType:kBookmarkDictionaryListPboardType]; | 103 [pb propertyListForType:kBookmarkDictionaryListPboardType]; |
| 82 if (!bookmarks) | 104 if (!bookmarks) |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 title = [pb stringForType:NSStringPboardType]; | 150 title = [pb stringForType:NSStringPboardType]; |
| 129 | 151 |
| 130 BookmarkNodeData::Element element; | 152 BookmarkNodeData::Element element; |
| 131 element.is_url = true; | 153 element.is_url = true; |
| 132 element.url = GURL(urlString); | 154 element.url = GURL(urlString); |
| 133 element.title = base::SysNSStringToUTF16(title); | 155 element.title = base::SysNSStringToUTF16(title); |
| 134 elements.push_back(element); | 156 elements.push_back(element); |
| 135 return true; | 157 return true; |
| 136 } | 158 } |
| 137 | 159 |
| 160 NSDictionary* DictionaryFromBookmarkMetaInfo( | |
|
sky
2014/07/30 17:17:48
I'm surprised we don't have some general function
Jiang Jiang
2014/07/31 10:05:56
Can't find anything in base at least.
| |
| 161 const BookmarkNode::MetaInfoMap& meta_info_map) { | |
| 162 NSMutableDictionary* dictionary = | |
| 163 [[NSMutableDictionary alloc] initWithCapacity:meta_info_map.size()]; | |
|
Avi (use Gerrit)
2014/07/30 17:45:10
You can do
NSMutableDictionary* dictionary = [NSM
Jiang Jiang
2014/07/31 10:05:56
Done.
| |
| 164 | |
| 165 for (BookmarkNode::MetaInfoMap::const_iterator it = meta_info_map.begin(); | |
| 166 it != meta_info_map.end(); ++it) { | |
| 167 dictionary[base::SysUTF8ToNSString(it->first)] = | |
| 168 base::SysUTF8ToNSString(it->second); | |
| 169 } | |
| 170 | |
| 171 return [dictionary autorelease]; | |
| 172 } | |
| 173 | |
| 138 NSArray* GetPlistForBookmarkList( | 174 NSArray* GetPlistForBookmarkList( |
| 139 const std::vector<BookmarkNodeData::Element>& elements) { | 175 const std::vector<BookmarkNodeData::Element>& elements) { |
| 140 NSMutableArray* plist = [NSMutableArray array]; | 176 NSMutableArray* plist = [NSMutableArray array]; |
| 141 for (size_t i = 0; i < elements.size(); ++i) { | 177 for (size_t i = 0; i < elements.size(); ++i) { |
| 142 BookmarkNodeData::Element element = elements[i]; | 178 BookmarkNodeData::Element element = elements[i]; |
| 179 NSDictionary* metaInfoDictionary = | |
| 180 DictionaryFromBookmarkMetaInfo(element.meta_info_map); | |
| 143 if (element.is_url) { | 181 if (element.is_url) { |
| 144 NSString* title = base::SysUTF16ToNSString(element.title); | 182 NSString* title = base::SysUTF16ToNSString(element.title); |
| 145 NSString* url = base::SysUTF8ToNSString(element.url.spec()); | 183 NSString* url = base::SysUTF8ToNSString(element.url.spec()); |
| 146 int64 elementId = element.id(); | 184 int64 elementId = element.id(); |
| 147 NSNumber* idNum = [NSNumber numberWithLongLong:elementId]; | 185 NSNumber* idNum = [NSNumber numberWithLongLong:elementId]; |
| 148 NSDictionary* uriDictionary = [NSDictionary dictionaryWithObjectsAndKeys: | 186 NSDictionary* uriDictionary = [NSDictionary dictionaryWithObjectsAndKeys: |
| 149 title, @"title", nil]; | 187 title, @"title", nil]; |
| 150 NSDictionary* object = [NSDictionary dictionaryWithObjectsAndKeys: | 188 NSDictionary* object = [NSDictionary dictionaryWithObjectsAndKeys: |
| 151 uriDictionary, @"URIDictionary", | 189 uriDictionary, @"URIDictionary", |
| 152 url, @"URLString", | 190 url, @"URLString", |
| 153 kWebBookmarkTypeLeaf, kWebBookmarkType, | 191 kWebBookmarkTypeLeaf, kWebBookmarkType, |
| 154 idNum, kChromiumBookmarkId, | 192 idNum, kChromiumBookmarkId, |
| 193 metaInfoDictionary, kChromiumBookmarkMetaInfo, | |
| 155 nil]; | 194 nil]; |
| 156 [plist addObject:object]; | 195 [plist addObject:object]; |
| 157 } else { | 196 } else { |
| 158 NSString* title = base::SysUTF16ToNSString(element.title); | 197 NSString* title = base::SysUTF16ToNSString(element.title); |
| 159 NSArray* children = GetPlistForBookmarkList(element.children); | 198 NSArray* children = GetPlistForBookmarkList(element.children); |
| 160 int64 elementId = element.id(); | 199 int64 elementId = element.id(); |
| 161 NSNumber* idNum = [NSNumber numberWithLongLong:elementId]; | 200 NSNumber* idNum = [NSNumber numberWithLongLong:elementId]; |
| 162 NSDictionary* object = [NSDictionary dictionaryWithObjectsAndKeys: | 201 NSDictionary* object = [NSDictionary dictionaryWithObjectsAndKeys: |
| 163 title, @"Title", | 202 title, @"Title", |
| 164 children, @"Children", | 203 children, @"Children", |
| 165 kWebBookmarkTypeList, kWebBookmarkType, | 204 kWebBookmarkTypeList, kWebBookmarkType, |
| 166 idNum, kChromiumBookmarkId, | 205 idNum, kChromiumBookmarkId, |
| 206 metaInfoDictionary, kChromiumBookmarkMetaInfo, | |
| 167 nil]; | 207 nil]; |
| 168 [plist addObject:object]; | 208 [plist addObject:object]; |
| 169 } | 209 } |
| 170 } | 210 } |
| 171 return plist; | 211 return plist; |
| 172 } | 212 } |
| 173 | 213 |
| 174 void WriteBookmarkDictionaryListPboardType( | 214 void WriteBookmarkDictionaryListPboardType( |
| 175 NSPasteboard* pb, | 215 NSPasteboard* pb, |
| 176 const std::vector<BookmarkNodeData::Element>& elements) { | 216 const std::vector<BookmarkNodeData::Element>& elements) { |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 bool PasteboardContainsBookmarks(ui::ClipboardType type) { | 320 bool PasteboardContainsBookmarks(ui::ClipboardType type) { |
| 281 NSPasteboard* pb = PasteboardFromType(type); | 321 NSPasteboard* pb = PasteboardFromType(type); |
| 282 | 322 |
| 283 NSArray* availableTypes = | 323 NSArray* availableTypes = |
| 284 [NSArray arrayWithObjects:kBookmarkDictionaryListPboardType, | 324 [NSArray arrayWithObjects:kBookmarkDictionaryListPboardType, |
| 285 kCrWebURLsWithTitlesPboardType, | 325 kCrWebURLsWithTitlesPboardType, |
| 286 NSURLPboardType, | 326 NSURLPboardType, |
| 287 nil]; | 327 nil]; |
| 288 return [pb availableTypeFromArray:availableTypes] != nil; | 328 return [pb availableTypeFromArray:availableTypes] != nil; |
| 289 } | 329 } |
| OLD | NEW |