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_utils.h" | 5 #include "components/bookmarks/browser/bookmark_utils.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/i18n/case_conversion.h" | 12 #include "base/i18n/case_conversion.h" |
13 #include "base/i18n/string_search.h" | 13 #include "base/i18n/string_search.h" |
14 #include "base/metrics/user_metrics_action.h" | 14 #include "base/metrics/user_metrics_action.h" |
15 #include "base/prefs/pref_service.h" | 15 #include "base/prefs/pref_service.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
18 #include "components/bookmarks/browser/bookmark_client.h" | 18 #include "components/bookmarks/browser/bookmark_client.h" |
19 #include "components/bookmarks/browser/bookmark_model.h" | 19 #include "components/bookmarks/browser/bookmark_model.h" |
20 #include "components/bookmarks/browser/scoped_group_bookmark_actions.h" | 20 #include "components/bookmarks/browser/scoped_group_bookmark_actions.h" |
21 #include "components/bookmarks/common/bookmark_pref_names.h" | 21 #include "components/bookmarks/common/bookmark_pref_names.h" |
22 #include "components/pref_registry/pref_registry_syncable.h" | 22 #include "components/pref_registry/pref_registry_syncable.h" |
23 #include "components/query_parser/query_parser.h" | 23 #include "components/query_parser/query_parser.h" |
24 #include "net/base/net_util.h" | 24 #include "net/base/net_util.h" |
| 25 #include "ui/base/clipboard/clipboard.h" |
25 #include "ui/base/models/tree_node_iterator.h" | 26 #include "ui/base/models/tree_node_iterator.h" |
26 #include "url/gurl.h" | 27 #include "url/gurl.h" |
27 | 28 |
28 using base::Time; | 29 using base::Time; |
29 | 30 |
30 namespace bookmarks { | 31 namespace bookmarks { |
31 | 32 |
32 namespace { | 33 namespace { |
33 | 34 |
34 // The maximum length of URL or title returned by the Cleanup functions. | 35 // The maximum length of URL or title returned by the Cleanup functions. |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 | 137 |
137 // If we're in the middle of an escape sequence, truncate just before it. | 138 // If we're in the middle of an escape sequence, truncate just before it. |
138 if (url[kCleanedUpUrlMaxLength - 1] == '%') | 139 if (url[kCleanedUpUrlMaxLength - 1] == '%') |
139 return url.substr(0, kCleanedUpUrlMaxLength - 1); | 140 return url.substr(0, kCleanedUpUrlMaxLength - 1); |
140 if (url[kCleanedUpUrlMaxLength - 2] == '%') | 141 if (url[kCleanedUpUrlMaxLength - 2] == '%') |
141 return url.substr(0, kCleanedUpUrlMaxLength - 2); | 142 return url.substr(0, kCleanedUpUrlMaxLength - 2); |
142 | 143 |
143 return url.substr(0, kCleanedUpUrlMaxLength); | 144 return url.substr(0, kCleanedUpUrlMaxLength); |
144 } | 145 } |
145 | 146 |
| 147 // Returns the URL from the clipboard. If there is no URL an empty URL is |
| 148 // returned. |
| 149 GURL GetUrlFromClipboard() { |
| 150 base::string16 url_text; |
| 151 #if !defined(OS_IOS) |
| 152 ui::Clipboard::GetForCurrentThread()->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, |
| 153 &url_text); |
| 154 #endif |
| 155 return GURL(url_text); |
| 156 } |
| 157 |
146 } // namespace | 158 } // namespace |
147 | 159 |
148 QueryFields::QueryFields() {} | 160 QueryFields::QueryFields() {} |
149 QueryFields::~QueryFields() {} | 161 QueryFields::~QueryFields() {} |
150 | 162 |
151 void CloneBookmarkNode(BookmarkModel* model, | 163 void CloneBookmarkNode(BookmarkModel* model, |
152 const std::vector<BookmarkNodeData::Element>& elements, | 164 const std::vector<BookmarkNodeData::Element>& elements, |
153 const BookmarkNode* parent, | 165 const BookmarkNode* parent, |
154 int index_to_add_at, | 166 int index_to_add_at, |
155 bool reset_node_times) { | 167 bool reset_node_times) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 } | 201 } |
190 } | 202 } |
191 | 203 |
192 void PasteFromClipboard(BookmarkModel* model, | 204 void PasteFromClipboard(BookmarkModel* model, |
193 const BookmarkNode* parent, | 205 const BookmarkNode* parent, |
194 int index) { | 206 int index) { |
195 if (!parent) | 207 if (!parent) |
196 return; | 208 return; |
197 | 209 |
198 BookmarkNodeData bookmark_data; | 210 BookmarkNodeData bookmark_data; |
199 if (!bookmark_data.ReadFromClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE)) | 211 if (!bookmark_data.ReadFromClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE)) { |
200 return; | 212 GURL url = GetUrlFromClipboard(); |
201 | 213 if (!url.is_valid()) |
| 214 return; |
| 215 BookmarkNode node(url); |
| 216 node.SetTitle(base::ASCIIToUTF16(url.spec())); |
| 217 bookmark_data = BookmarkNodeData(&node); |
| 218 } |
202 if (index == -1) | 219 if (index == -1) |
203 index = parent->child_count(); | 220 index = parent->child_count(); |
204 ScopedGroupBookmarkActions group_paste(model); | 221 ScopedGroupBookmarkActions group_paste(model); |
205 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true); | 222 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true); |
206 } | 223 } |
207 | 224 |
208 bool CanPasteFromClipboard(BookmarkModel* model, const BookmarkNode* node) { | 225 bool CanPasteFromClipboard(BookmarkModel* model, const BookmarkNode* node) { |
209 if (!node || !model->client()->CanBeEditedByUser(node)) | 226 if (!node || !model->client()->CanBeEditedByUser(node)) |
210 return false; | 227 return false; |
211 return BookmarkNodeData::ClipboardContainsBookmarks(); | 228 return (BookmarkNodeData::ClipboardContainsBookmarks() || |
| 229 GetUrlFromClipboard().is_valid()); |
212 } | 230 } |
213 | 231 |
214 std::vector<const BookmarkNode*> GetMostRecentlyModifiedUserFolders( | 232 std::vector<const BookmarkNode*> GetMostRecentlyModifiedUserFolders( |
215 BookmarkModel* model, | 233 BookmarkModel* model, |
216 size_t max_count) { | 234 size_t max_count) { |
217 std::vector<const BookmarkNode*> nodes; | 235 std::vector<const BookmarkNode*> nodes; |
218 ui::TreeNodeIterator<const BookmarkNode> iterator( | 236 ui::TreeNodeIterator<const BookmarkNode> iterator( |
219 model->root_node(), base::Bind(&PruneInvisibleFolders)); | 237 model->root_node(), base::Bind(&PruneInvisibleFolders)); |
220 | 238 |
221 while (iterator.has_next()) { | 239 while (iterator.has_next()) { |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 } | 456 } |
439 return false; | 457 return false; |
440 } | 458 } |
441 | 459 |
442 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) { | 460 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) { |
443 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate. | 461 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate. |
444 return GetNodeByID(model->root_node(), id); | 462 return GetNodeByID(model->root_node(), id); |
445 } | 463 } |
446 | 464 |
447 } // namespace bookmarks | 465 } // namespace bookmarks |
OLD | NEW |