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

Side by Side Diff: components/bookmarks/browser/bookmark_utils.cc

Issue 446003002: Title: Same Bookmark url is getting pasted on the Bookmarkbar with same title. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changing vector to hash_set as suggested. Created 6 years, 2 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
OLDNEW
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
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 void UpdateCopiedBookmarkTitle(BookmarkModel* model,
208 const BookmarkNode* parent,
209 BookmarkNodeData* bookmark_data) {
sky 2014/09/30 15:45:16 BookmarkNodeData may contain any number of bookmar
Deepak 2014/10/01 05:13:09 Done.
210 base::hash_set<base::string16> titles;
211 base::string16 new_title = bookmark_data->elements[0].title;
212 for (int i = 0; i < parent->child_count(); i++) {
213 const BookmarkNode* node = parent->GetChild(i);
214 if (node->is_url() && (bookmark_data->elements[0].url == node->url()) &&
215 (new_title == node->GetTitle() ||
216 StartsWith(node->GetTitle(), new_title, false))) {
217 titles.insert(node->GetTitle());
218 }
219 }
220
221 if (titles.find(new_title) == titles.end()) {
222 new_title = bookmark_data->elements[0].title;
223 } else {
224 for (size_t i = 0; i < titles.size(); i++) {
225 new_title = base::UTF8ToUTF16(base::StringPrintf(
226 "%s(%lu)",
227 base::UTF16ToUTF8(bookmark_data->elements[0].title).c_str(),
228 i + 1));
229 if (titles.find(new_title) == titles.end())
230 break;
231 }
232 }
233
234 bookmark_data->elements[0].title = new_title;
235 }
236
204 void PasteFromClipboard(BookmarkModel* model, 237 void PasteFromClipboard(BookmarkModel* model,
205 const BookmarkNode* parent, 238 const BookmarkNode* parent,
206 int index) { 239 int index) {
207 if (!parent) 240 if (!parent)
208 return; 241 return;
209 242
210 BookmarkNodeData bookmark_data; 243 BookmarkNodeData bookmark_data;
211 if (!bookmark_data.ReadFromClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE)) { 244 if (!bookmark_data.ReadFromClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE)) {
212 GURL url = GetUrlFromClipboard(); 245 GURL url = GetUrlFromClipboard();
213 if (!url.is_valid()) 246 if (!url.is_valid())
214 return; 247 return;
215 BookmarkNode node(url); 248 BookmarkNode node(url);
216 node.SetTitle(base::ASCIIToUTF16(url.spec())); 249 node.SetTitle(base::ASCIIToUTF16(url.spec()));
217 bookmark_data = BookmarkNodeData(&node); 250 bookmark_data = BookmarkNodeData(&node);
218 } 251 }
219 if (index == -1) 252 if (index == -1)
220 index = parent->child_count(); 253 index = parent->child_count();
221 ScopedGroupBookmarkActions group_paste(model); 254 ScopedGroupBookmarkActions group_paste(model);
255
256 if (bookmark_data.elements.size() == 1 &&
257 model->IsBookmarked(bookmark_data.elements[0].url))
258 UpdateCopiedBookmarkTitle(model, parent, &bookmark_data);
259
222 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true); 260 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true);
223 } 261 }
224 262
225 bool CanPasteFromClipboard(BookmarkModel* model, const BookmarkNode* node) { 263 bool CanPasteFromClipboard(BookmarkModel* model, const BookmarkNode* node) {
226 if (!node || !model->client()->CanBeEditedByUser(node)) 264 if (!node || !model->client()->CanBeEditedByUser(node))
227 return false; 265 return false;
228 return (BookmarkNodeData::ClipboardContainsBookmarks() || 266 return (BookmarkNodeData::ClipboardContainsBookmarks() ||
229 GetUrlFromClipboard().is_valid()); 267 GetUrlFromClipboard().is_valid());
230 } 268 }
231 269
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 } 494 }
457 return false; 495 return false;
458 } 496 }
459 497
460 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) { 498 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) {
461 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate. 499 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate.
462 return GetNodeByID(model->root_node(), id); 500 return GetNodeByID(model->root_node(), id);
463 } 501 }
464 502
465 } // namespace bookmarks 503 } // namespace bookmarks
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698