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

Side by Side Diff: chrome/browser/bookmarks/bookmark_drag_data.cc

Issue 159815: Refactor bookmark clipboard code to be cross platform. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: fix UMR Created 11 years, 4 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
OLDNEW
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 "chrome/browser/bookmarks/bookmark_drag_data.h" 5 #include "chrome/browser/bookmarks/bookmark_drag_data.h"
6 6
7 #include "base/basictypes.h"
8 #include "base/pickle.h"
9 #include "base/scoped_clipboard_writer.h"
10 #include "base/string_util.h"
11 #include "chrome/browser/bookmarks/bookmark_model.h"
12 #include "chrome/browser/profile.h"
13 #include "chrome/common/url_constants.h"
14 #include "chrome/browser/browser_process.h"
15
7 // TODO(port): Port this file. 16 // TODO(port): Port this file.
8 #if defined(TOOLKIT_VIEWS) 17 #if defined(TOOLKIT_VIEWS)
9 #include "app/os_exchange_data.h" 18 #include "app/os_exchange_data.h"
10 #else 19 #else
11 #include "chrome/common/temp_scaffolding_stubs.h" 20 #include "chrome/common/temp_scaffolding_stubs.h"
12 #endif 21 #endif
13 #include "base/basictypes.h" 22
14 #include "base/pickle.h" 23 const char* BookmarkDragData::kClipboardFormatString =
15 #include "base/string_util.h" 24 "chromium/x-bookmark-entries";
16 #include "chrome/browser/bookmarks/bookmark_model.h"
17 #include "chrome/browser/profile.h"
18 #include "chrome/common/url_constants.h"
19 25
20 #if defined(OS_WIN) 26 #if defined(OS_WIN)
21 static CLIPFORMAT clipboard_format = 0; 27 static CLIPFORMAT clipboard_format = 0;
22 28
23 static void RegisterFormat() { 29 static void RegisterFormat() {
24 if (clipboard_format == 0) { 30 if (clipboard_format == 0) {
25 clipboard_format = RegisterClipboardFormat(L"chrome/x-bookmark-entries"); 31 clipboard_format =
32 ::RegisterClipboardFormat(ASCIIToWide(
33 BookmarkDragData::kClipboardFormatString).c_str());
26 DCHECK(clipboard_format); 34 DCHECK(clipboard_format);
27 } 35 }
28 } 36 }
29 #endif 37 #endif
30 38
31 BookmarkDragData::Element::Element(const BookmarkNode* node) 39 BookmarkDragData::Element::Element(const BookmarkNode* node)
32 : is_url(node->is_url()), 40 : is_url(node->is_url()),
33 url(node->GetURL()), 41 url(node->GetURL()),
34 title(node->GetTitle()), 42 title(node->GetTitle()),
35 id_(node->id()) { 43 id_(node->id()) {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 BookmarkDragData::BookmarkDragData(const BookmarkNode* node) { 87 BookmarkDragData::BookmarkDragData(const BookmarkNode* node) {
80 elements.push_back(Element(node)); 88 elements.push_back(Element(node));
81 } 89 }
82 90
83 BookmarkDragData::BookmarkDragData( 91 BookmarkDragData::BookmarkDragData(
84 const std::vector<const BookmarkNode*>& nodes) { 92 const std::vector<const BookmarkNode*>& nodes) {
85 for (size_t i = 0; i < nodes.size(); ++i) 93 for (size_t i = 0; i < nodes.size(); ++i)
86 elements.push_back(Element(nodes[i])); 94 elements.push_back(Element(nodes[i]));
87 } 95 }
88 96
97 #if !defined(OS_MACOSX)
98 void BookmarkDragData::WriteToClipboard(Profile* profile) const {
99 ScopedClipboardWriter scw(g_browser_process->clipboard());
100
101 // If there is only one element and it is a URL, write the URL to the
102 // clipboard.
103 if (elements.size() == 1 && elements[0].is_url) {
104 scw.WriteBookmark(WideToUTF16Hack(elements[0].title),
105 elements[0].url.spec());
106 }
107
108 Pickle pickle;
109 WriteToPickle(profile, &pickle);
110 scw.WritePickledData(pickle, kClipboardFormatString);
111 }
112
113 bool BookmarkDragData::ReadFromClipboard() {
114 std::string data;
115 Clipboard* clipboard = g_browser_process->clipboard();
116 clipboard->ReadData(kClipboardFormatString, &data);
117
118 if (!data.empty()) {
119 Pickle pickle(data.data(), data.size());
120 if (ReadFromPickle(&pickle))
121 return true;
122 }
123
124 string16 title;
125 std::string url;
126 clipboard->ReadBookmark(&title, &url);
127 if (!url.empty()) {
128 Element element;
129 element.is_url = true;
130 element.url = GURL(url);
131 element.title = UTF16ToWideHack(title);
132
133 elements.clear();
134 elements.push_back(element);
135 return true;
136 }
137
138 return false;
139 }
140 #endif // !defined(OS_MACOSX)
141
89 #if defined(OS_WIN) 142 #if defined(OS_WIN)
90 void BookmarkDragData::Write(Profile* profile, OSExchangeData* data) const { 143 void BookmarkDragData::Write(Profile* profile, OSExchangeData* data) const {
91 RegisterFormat(); 144 RegisterFormat();
92 145
93 DCHECK(data); 146 DCHECK(data);
94 147
95 // If there is only one element and it is a URL, write the URL to the 148 // If there is only one element and it is a URL, write the URL to the
96 // clipboard. 149 // clipboard.
97 if (elements.size() == 1 && elements[0].is_url) { 150 if (elements.size() == 1 && elements[0].is_url) {
98 if (elements[0].url.SchemeIs(chrome::kJavaScriptScheme)) { 151 if (elements[0].url.SchemeIs(chrome::kJavaScriptScheme)) {
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 265
213 bool BookmarkDragData::IsFromProfile(Profile* profile) const { 266 bool BookmarkDragData::IsFromProfile(Profile* profile) const {
214 // An empty path means the data is not associated with any profile. 267 // An empty path means the data is not associated with any profile.
215 return (!profile_path_.empty() && 268 return (!profile_path_.empty() &&
216 #if defined(WCHAR_T_IS_UTF16) 269 #if defined(WCHAR_T_IS_UTF16)
217 profile->GetPath().ToWStringHack() == profile_path_); 270 profile->GetPath().ToWStringHack() == profile_path_);
218 #elif defined(WCHAR_T_IS_UTF32) 271 #elif defined(WCHAR_T_IS_UTF32)
219 profile->GetPath().value() == profile_path_); 272 profile->GetPath().value() == profile_path_);
220 #endif 273 #endif
221 } 274 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698