OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_node_data.h" | 5 #include "chrome/browser/bookmarks/bookmark_node_data.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/pickle.h" | 10 #include "base/pickle.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/bookmarks/bookmark_model.h" | 13 #include "chrome/browser/bookmarks/bookmark_model.h" |
14 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 14 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
17 #include "net/base/escape.h" | 17 #include "net/base/escape.h" |
18 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 18 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
19 | 19 |
20 #if defined(OS_MACOSX) | 20 #if defined(OS_MACOSX) |
21 #include "chrome/browser/bookmarks/bookmark_pasteboard_helper_mac.h" | 21 #include "chrome/browser/bookmarks/bookmark_pasteboard_helper_mac.h" |
22 #else | 22 #else |
23 #include "chrome/browser/browser_process.h" | 23 #include "chrome/browser/browser_process.h" |
kaiwang
2012/09/06 22:11:47
Remove this include?
| |
24 #endif | 24 #endif |
25 | 25 |
26 const char* BookmarkNodeData::kClipboardFormatString = | 26 const char* BookmarkNodeData::kClipboardFormatString = |
27 "chromium/x-bookmark-entries"; | 27 "chromium/x-bookmark-entries"; |
28 | 28 |
29 BookmarkNodeData::Element::Element() : is_url(false), id_(0) { | 29 BookmarkNodeData::Element::Element() : is_url(false), id_(0) { |
30 } | 30 } |
31 | 31 |
32 BookmarkNodeData::Element::Element(const BookmarkNode* node) | 32 BookmarkNodeData::Element::Element(const BookmarkNode* node) |
33 : is_url(node->is_url()), | 33 : is_url(node->is_url()), |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
134 element.url = url; | 134 element.url = url; |
135 element.is_url = true; | 135 element.is_url = true; |
136 | 136 |
137 elements.push_back(element); | 137 elements.push_back(element); |
138 | 138 |
139 return true; | 139 return true; |
140 } | 140 } |
141 | 141 |
142 #if !defined(OS_MACOSX) | 142 #if !defined(OS_MACOSX) |
143 void BookmarkNodeData::WriteToClipboard(Profile* profile) const { | 143 void BookmarkNodeData::WriteToClipboard(Profile* profile) const { |
144 ui::ScopedClipboardWriter scw(g_browser_process->clipboard(), | 144 ui::ScopedClipboardWriter scw(ui::Clipboard::GetForCurrentThread(), |
145 ui::Clipboard::BUFFER_STANDARD); | 145 ui::Clipboard::BUFFER_STANDARD); |
146 | 146 |
147 // If there is only one element and it is a URL, write the URL to the | 147 // If there is only one element and it is a URL, write the URL to the |
148 // clipboard. | 148 // clipboard. |
149 if (elements.size() == 1 && elements[0].is_url) { | 149 if (elements.size() == 1 && elements[0].is_url) { |
150 const string16& title = elements[0].title; | 150 const string16& title = elements[0].title; |
151 const std::string url = elements[0].url.spec(); | 151 const std::string url = elements[0].url.spec(); |
152 | 152 |
153 scw.WriteBookmark(title, url); | 153 scw.WriteBookmark(title, url); |
154 scw.WriteHyperlink(net::EscapeForHTML(title), url); | 154 scw.WriteHyperlink(net::EscapeForHTML(title), url); |
155 | 155 |
156 // Also write the URL to the clipboard as text so that it can be pasted | 156 // Also write the URL to the clipboard as text so that it can be pasted |
157 // into text fields. We use WriteText instead of WriteURL because we don't | 157 // into text fields. We use WriteText instead of WriteURL because we don't |
158 // want to clobber the X clipboard when the user copies out of the omnibox | 158 // want to clobber the X clipboard when the user copies out of the omnibox |
159 // on Linux (on Windows and Mac, there is no difference between these | 159 // on Linux (on Windows and Mac, there is no difference between these |
160 // functions). | 160 // functions). |
161 scw.WriteText(UTF8ToUTF16(url)); | 161 scw.WriteText(UTF8ToUTF16(url)); |
162 } | 162 } |
163 | 163 |
164 Pickle pickle; | 164 Pickle pickle; |
165 WriteToPickle(profile, &pickle); | 165 WriteToPickle(profile, &pickle); |
166 scw.WritePickledData( | 166 scw.WritePickledData( |
167 pickle, ui::Clipboard::GetFormatType(kClipboardFormatString)); | 167 pickle, ui::Clipboard::GetFormatType(kClipboardFormatString)); |
168 } | 168 } |
169 | 169 |
170 bool BookmarkNodeData::ReadFromClipboard() { | 170 bool BookmarkNodeData::ReadFromClipboard() { |
171 std::string data; | 171 std::string data; |
172 ui::Clipboard* clipboard = g_browser_process->clipboard(); | 172 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); |
173 clipboard->ReadData(ui::Clipboard::GetFormatType(kClipboardFormatString), | 173 clipboard->ReadData(ui::Clipboard::GetFormatType(kClipboardFormatString), |
174 &data); | 174 &data); |
175 | 175 |
176 if (!data.empty()) { | 176 if (!data.empty()) { |
177 Pickle pickle(data.data(), data.size()); | 177 Pickle pickle(data.data(), data.size()); |
178 if (ReadFromPickle(&pickle)) | 178 if (ReadFromPickle(&pickle)) |
179 return true; | 179 return true; |
180 } | 180 } |
181 | 181 |
182 string16 title; | 182 string16 title; |
183 std::string url; | 183 std::string url; |
184 clipboard->ReadBookmark(&title, &url); | 184 clipboard->ReadBookmark(&title, &url); |
185 if (!url.empty()) { | 185 if (!url.empty()) { |
186 Element element; | 186 Element element; |
187 element.is_url = true; | 187 element.is_url = true; |
188 element.url = GURL(url); | 188 element.url = GURL(url); |
189 element.title = title; | 189 element.title = title; |
190 | 190 |
191 elements.clear(); | 191 elements.clear(); |
192 elements.push_back(element); | 192 elements.push_back(element); |
193 return true; | 193 return true; |
194 } | 194 } |
195 | 195 |
196 return false; | 196 return false; |
197 } | 197 } |
198 | 198 |
199 bool BookmarkNodeData::ClipboardContainsBookmarks() { | 199 bool BookmarkNodeData::ClipboardContainsBookmarks() { |
200 return g_browser_process->clipboard()->IsFormatAvailable( | 200 return ui::Clipboard::GetForCurrentThread()->IsFormatAvailable( |
201 ui::Clipboard::GetFormatType(kClipboardFormatString), | 201 ui::Clipboard::GetFormatType(kClipboardFormatString), |
202 ui::Clipboard::BUFFER_STANDARD); | 202 ui::Clipboard::BUFFER_STANDARD); |
203 } | 203 } |
204 #else | 204 #else |
205 void BookmarkNodeData::WriteToClipboard(Profile* profile) const { | 205 void BookmarkNodeData::WriteToClipboard(Profile* profile) const { |
206 bookmark_pasteboard_helper_mac::WriteToPasteboard( | 206 bookmark_pasteboard_helper_mac::WriteToPasteboard( |
207 bookmark_pasteboard_helper_mac::kCopyPastePasteboard, | 207 bookmark_pasteboard_helper_mac::kCopyPastePasteboard, |
208 elements, | 208 elements, |
209 profile_path_.value()); | 209 profile_path_.value()); |
210 } | 210 } |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
345 DCHECK(profile_path_.empty()); | 345 DCHECK(profile_path_.empty()); |
346 | 346 |
347 if (profile) | 347 if (profile) |
348 profile_path_ = profile->GetPath(); | 348 profile_path_ = profile->GetPath(); |
349 } | 349 } |
350 | 350 |
351 bool BookmarkNodeData::IsFromProfile(Profile* profile) const { | 351 bool BookmarkNodeData::IsFromProfile(Profile* profile) const { |
352 // An empty path means the data is not associated with any profile. | 352 // An empty path means the data is not associated with any profile. |
353 return !profile_path_.empty() && profile_path_ == profile->GetPath(); | 353 return !profile_path_.empty() && profile_path_ == profile->GetPath(); |
354 } | 354 } |
OLD | NEW |