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/containers/hash_tables.h" | |
11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
12 #include "base/i18n/case_conversion.h" | 13 #include "base/i18n/case_conversion.h" |
13 #include "base/i18n/string_search.h" | 14 #include "base/i18n/string_search.h" |
14 #include "base/metrics/user_metrics_action.h" | 15 #include "base/metrics/user_metrics_action.h" |
15 #include "base/prefs/pref_service.h" | 16 #include "base/prefs/pref_service.h" |
17 #include "base/strings/string_util.h" | |
18 #include "base/strings/stringprintf.h" | |
16 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
17 #include "base/time/time.h" | 20 #include "base/time/time.h" |
18 #include "components/bookmarks/browser/bookmark_client.h" | 21 #include "components/bookmarks/browser/bookmark_client.h" |
19 #include "components/bookmarks/browser/bookmark_model.h" | 22 #include "components/bookmarks/browser/bookmark_model.h" |
20 #include "components/bookmarks/browser/scoped_group_bookmark_actions.h" | 23 #include "components/bookmarks/browser/scoped_group_bookmark_actions.h" |
21 #include "components/bookmarks/common/bookmark_pref_names.h" | 24 #include "components/bookmarks/common/bookmark_pref_names.h" |
22 #include "components/pref_registry/pref_registry_syncable.h" | 25 #include "components/pref_registry/pref_registry_syncable.h" |
23 #include "components/query_parser/query_parser.h" | 26 #include "components/query_parser/query_parser.h" |
24 #include "net/base/net_util.h" | 27 #include "net/base/net_util.h" |
25 #include "ui/base/clipboard/clipboard.h" | 28 #include "ui/base/clipboard/clipboard.h" |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 if (remove_nodes) { | 197 if (remove_nodes) { |
195 ScopedGroupBookmarkActions group_cut(model); | 198 ScopedGroupBookmarkActions group_cut(model); |
196 for (size_t i = 0; i < filtered_nodes.size(); ++i) { | 199 for (size_t i = 0; i < filtered_nodes.size(); ++i) { |
197 int index = filtered_nodes[i]->parent()->GetIndexOf(filtered_nodes[i]); | 200 int index = filtered_nodes[i]->parent()->GetIndexOf(filtered_nodes[i]); |
198 if (index > -1) | 201 if (index > -1) |
199 model->Remove(filtered_nodes[i]->parent(), index); | 202 model->Remove(filtered_nodes[i]->parent(), index); |
200 } | 203 } |
201 } | 204 } |
202 } | 205 } |
203 | 206 |
207 // Updates |title| such that |url| and |title| pair are unique among the | |
208 // children of |parent|. | |
209 void MakeTitleUnique(const BookmarkModel* model, | |
210 const BookmarkNode* parent, | |
211 const GURL& url, | |
212 base::string16* title) { | |
213 base::hash_set<base::string16> titles; | |
214 for (int i = 0; i < parent->child_count(); i++) { | |
215 const BookmarkNode* node = parent->GetChild(i); | |
216 if (node->is_url() && (url == node->url()) && | |
217 StartsWith(node->GetTitle(), *title, false)) { | |
218 titles.insert(node->GetTitle()); | |
219 } | |
220 } | |
221 | |
222 if (titles.find(*title) == titles.end()) | |
223 return; | |
224 | |
225 for (size_t i = 0; i < titles.size(); i++) { | |
226 const base::string16 new_title( | |
227 *title + base::ASCIIToUTF16(base::StringPrintf(" (%lu)", i + 1))); | |
228 if (titles.find(new_title) == titles.end()) { | |
229 *title = new_title; | |
230 return; | |
231 } | |
232 } | |
233 } | |
sky
2014/10/09 17:14:24
Seems like there should be a NOTREACHED here, righ
Deepak
2014/10/10 05:06:50
Done.
| |
234 | |
204 void PasteFromClipboard(BookmarkModel* model, | 235 void PasteFromClipboard(BookmarkModel* model, |
205 const BookmarkNode* parent, | 236 const BookmarkNode* parent, |
206 int index) { | 237 int index) { |
207 if (!parent) | 238 if (!parent) |
208 return; | 239 return; |
209 | 240 |
210 BookmarkNodeData bookmark_data; | 241 BookmarkNodeData bookmark_data; |
211 if (!bookmark_data.ReadFromClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE)) { | 242 if (!bookmark_data.ReadFromClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE)) { |
212 GURL url = GetUrlFromClipboard(); | 243 GURL url = GetUrlFromClipboard(); |
213 if (!url.is_valid()) | 244 if (!url.is_valid()) |
214 return; | 245 return; |
215 BookmarkNode node(url); | 246 BookmarkNode node(url); |
216 node.SetTitle(base::ASCIIToUTF16(url.spec())); | 247 node.SetTitle(base::ASCIIToUTF16(url.spec())); |
217 bookmark_data = BookmarkNodeData(&node); | 248 bookmark_data = BookmarkNodeData(&node); |
218 } | 249 } |
219 if (index == -1) | 250 if (index == -1) |
220 index = parent->child_count(); | 251 index = parent->child_count(); |
221 ScopedGroupBookmarkActions group_paste(model); | 252 ScopedGroupBookmarkActions group_paste(model); |
253 | |
254 if (bookmark_data.elements.size() == 1 && | |
255 model->IsBookmarked(bookmark_data.elements[0].url)) { | |
256 MakeTitleUnique(model, | |
257 parent, | |
258 bookmark_data.elements[0].url, | |
259 &bookmark_data.elements[0].title); | |
260 } | |
261 | |
222 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true); | 262 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true); |
223 } | 263 } |
224 | 264 |
225 bool CanPasteFromClipboard(BookmarkModel* model, const BookmarkNode* node) { | 265 bool CanPasteFromClipboard(BookmarkModel* model, const BookmarkNode* node) { |
226 if (!node || !model->client()->CanBeEditedByUser(node)) | 266 if (!node || !model->client()->CanBeEditedByUser(node)) |
227 return false; | 267 return false; |
228 return (BookmarkNodeData::ClipboardContainsBookmarks() || | 268 return (BookmarkNodeData::ClipboardContainsBookmarks() || |
229 GetUrlFromClipboard().is_valid()); | 269 GetUrlFromClipboard().is_valid()); |
230 } | 270 } |
231 | 271 |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
456 } | 496 } |
457 return false; | 497 return false; |
458 } | 498 } |
459 | 499 |
460 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) { | 500 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) { |
461 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate. | 501 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate. |
462 return GetNodeByID(model->root_node(), id); | 502 return GetNodeByID(model->root_node(), id); |
463 } | 503 } |
464 | 504 |
465 } // namespace bookmarks | 505 } // namespace bookmarks |
OLD | NEW |