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

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: removing changes from header file. 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
« no previous file with comments | « no previous file | components/bookmarks/browser/bookmark_utils_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // This function will returns new title for the bookmark if some bookmark with
208 // same |title|and |url| is already exist as sibling, else returns original
209 // |title|.
210 base::string16 UpdateCopiedBookmarkTitle(const BookmarkModel* model,
211 const BookmarkNode* parent,
212 const GURL& url,
213 const base::string16& title) {
sky 2014/10/01 19:28:26 This should be a pointer and modified in place.
Deepak 2014/10/02 07:19:45 Done.
214 base::hash_set<base::string16> titles;
215 base::string16 new_title = title;
216 for (int i = 0; i < parent->child_count(); i++) {
217 const BookmarkNode* node = parent->GetChild(i);
218 if (node->is_url() && (url == node->url()) &&
219 (new_title == node->GetTitle() ||
sky 2014/10/01 19:28:25 Doesn't the StartsWith cover this case too?
Deepak 2014/10/02 07:19:45 Done.
220 StartsWith(node->GetTitle(), new_title, false))) {
221 titles.insert(node->GetTitle());
222 }
223 }
224
225 if (titles.find(new_title) == titles.end())
226 return new_title;
227
228 for (size_t i = 0; i < titles.size(); i++) {
229 new_title = base::UTF8ToUTF16(
230 base::StringPrintf("%s(%lu)", base::UTF16ToUTF8(title).c_str(), i + 1));
sky 2014/10/01 19:28:26 Add a space here.
sky 2014/10/01 19:28:26 All this conversion is ugly. Can you do just one c
Deepak 2014/10/02 07:19:45 Done.
Deepak 2014/10/02 07:19:45 As I am creating a new title everytime by changing
231 if (titles.find(new_title) == titles.end())
232 break;
sky 2014/10/01 19:28:26 You can return here, right?
Deepak 2014/10/02 07:19:45 Done.
233 }
234 return 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 bookmark_data.elements[0].title =
259 UpdateCopiedBookmarkTitle(model,
260 parent,
261 bookmark_data.elements[0].url,
262 bookmark_data.elements[0].title);
263 }
264
222 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true); 265 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true);
223 } 266 }
224 267
225 bool CanPasteFromClipboard(BookmarkModel* model, const BookmarkNode* node) { 268 bool CanPasteFromClipboard(BookmarkModel* model, const BookmarkNode* node) {
226 if (!node || !model->client()->CanBeEditedByUser(node)) 269 if (!node || !model->client()->CanBeEditedByUser(node))
227 return false; 270 return false;
228 return (BookmarkNodeData::ClipboardContainsBookmarks() || 271 return (BookmarkNodeData::ClipboardContainsBookmarks() ||
229 GetUrlFromClipboard().is_valid()); 272 GetUrlFromClipboard().is_valid());
230 } 273 }
231 274
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 } 499 }
457 return false; 500 return false;
458 } 501 }
459 502
460 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) { 503 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) {
461 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate. 504 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate.
462 return GetNodeByID(model->root_node(), id); 505 return GetNodeByID(model->root_node(), id);
463 } 506 }
464 507
465 } // namespace bookmarks 508 } // namespace bookmarks
OLDNEW
« no previous file with comments | « no previous file | components/bookmarks/browser/bookmark_utils_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698