Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Commit

Permalink
[Merge to 2743] [Mac] Fix regression with copying URLs from the Omnibox.
Browse files Browse the repository at this point in the history
> The function ClipboardUtil::PasteboardItemFromUrl was incorrectly attempting to
> create a public UTI using UTIFromPboardType. The resulting NSPasteboardItem was
> not correctly writing to the NSPasteboard. Using the public UTIs directly fixes
> this problem.
>
> BUG=618771
>
> Review-Url: https://codereview.chromium.org/2080603007
> Cr-Commit-Position: refs/heads/master@{#401134}

(cherry picked from commit 23d52ab)

Review URL: https://codereview.chromium.org/2094553003 .

Cr-Commit-Position: refs/branch-heads/2743@{#455}
Cr-Branched-From: 2b3ae3b-refs/heads/master@{#394939}
  • Loading branch information
erikchen committed Jun 23, 2016
1 parent 88d1c7e commit 2d84e10
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
20 changes: 6 additions & 14 deletions ui/base/clipboard/clipboard_util_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

namespace {
NSString* const kWebURLsWithTitlesPboardType = @"WebURLsWithTitlesPboardType";
NSString* const kCorePasteboardFlavorType_url =
@"CorePasteboardFlavorType 0x75726C20"; // 'url ' url
NSString* const kCorePasteboardFlavorType_urln =
@"CorePasteboardFlavorType 0x75726C6E"; // 'urln' title
NSString* const kPublicUrl = @"public.url";
NSString* const kPublicUrlName = @"public.url-name";

// It's much more convenient to return an NSString than a
// base::ScopedCFTypeRef<CFStringRef>, since the methods on NSPasteboardItem
Expand Down Expand Up @@ -68,12 +66,8 @@
}

[item setString:urlString forType:NSPasteboardTypeString];

[item setData:[urlString dataUsingEncoding:NSUTF8StringEncoding]
forType:UTIFromPboardType(kCorePasteboardFlavorType_url)];

[item setData:[title dataUsingEncoding:NSUTF8StringEncoding]
forType:UTIFromPboardType(kCorePasteboardFlavorType_urln)];
[item setString:urlString forType:kPublicUrl];
[item setString:title forType:kPublicUrlName];
return item;
}

Expand Down Expand Up @@ -101,14 +95,12 @@

//static
NSString* ClipboardUtil::GetTitleFromPasteboardURL(NSPasteboard* pboard) {
return
[pboard stringForType:UTIFromPboardType(kCorePasteboardFlavorType_urln)];
return [pboard stringForType:kPublicUrlName];
}

//static
NSString* ClipboardUtil::GetURLFromPasteboardURL(NSPasteboard* pboard) {
return
[pboard stringForType:UTIFromPboardType(kCorePasteboardFlavorType_url)];
return [pboard stringForType:kPublicUrl];
}

// static
Expand Down
25 changes: 25 additions & 0 deletions ui/base/clipboard/clipboard_util_mac_unittest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
class ClipboardUtilMacTest : public PlatformTest {
public:
ClipboardUtilMacTest() { }

NSDictionary* DictionaryFromPasteboard(NSPasteboard* pboard) {
NSArray* types = [pboard types];
NSMutableDictionary* data = [NSMutableDictionary dictionary];
for (NSString* type in types) {
[data setObject:[pboard dataForType:type] forKey:type];
}
return data;
}
};

TEST_F(ClipboardUtilMacTest, PasteboardItemFromUrl) {
Expand Down Expand Up @@ -96,4 +105,20 @@
}
}

TEST_F(ClipboardUtilMacTest, CompareToWriteToPasteboard) {
NSString* urlString = @"https://www.cnn.com/";

base::scoped_nsobject<NSPasteboardItem> item(
ui::ClipboardUtil::PasteboardItemFromUrl(urlString, nil));
scoped_refptr<ui::UniquePasteboard> pasteboard = new ui::UniquePasteboard;
[pasteboard->get() writeObjects:@[ item ]];

scoped_refptr<ui::UniquePasteboard> pboard = new ui::UniquePasteboard;
[pboard->get() setDataForURL:urlString title:urlString];

NSDictionary* data1 = DictionaryFromPasteboard(pasteboard->get());
NSDictionary* data2 = DictionaryFromPasteboard(pboard->get());
EXPECT_NSEQ(data1, data2);
}

} // namespace

0 comments on commit 2d84e10

Please sign in to comment.