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

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

Issue 437423005: Show paste option on right click of bookmark bar if some URL is copied from outside browser or from… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added unit tests Created 6 years, 4 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/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 if (remove_nodes) { 181 if (remove_nodes) {
182 ScopedGroupBookmarkActions group_cut(model); 182 ScopedGroupBookmarkActions group_cut(model);
183 for (size_t i = 0; i < filtered_nodes.size(); ++i) { 183 for (size_t i = 0; i < filtered_nodes.size(); ++i) {
184 int index = filtered_nodes[i]->parent()->GetIndexOf(filtered_nodes[i]); 184 int index = filtered_nodes[i]->parent()->GetIndexOf(filtered_nodes[i]);
185 if (index > -1) 185 if (index > -1)
186 model->Remove(filtered_nodes[i]->parent(), index); 186 model->Remove(filtered_nodes[i]->parent(), index);
187 } 187 }
188 } 188 }
189 } 189 }
190 190
191 bool ClipboardContainsValidUrl(base::string16& url_text) {
192 if (!ui::Clipboard::GetForCurrentThread()->IsFormatAvailable(
193 ui::Clipboard::GetUrlWFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE))
194 return false;
195
196 ui::Clipboard::GetForCurrentThread()->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE,
197 &url_text);
198 GURL url(url_text);
199 if (!url.is_valid())
200 return false;
201 return true;
202 }
203
191 void PasteFromClipboard(BookmarkModel* model, 204 void PasteFromClipboard(BookmarkModel* model,
192 const BookmarkNode* parent, 205 const BookmarkNode* parent,
193 int index) { 206 int index) {
194 if (!parent) 207 if (!parent)
195 return; 208 return;
196 209
197 BookmarkNodeData bookmark_data; 210 BookmarkNodeData bookmark_data;
211 base::string16 url_text;
198 if (!bookmark_data.ReadFromClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE)) 212 if (!bookmark_data.ReadFromClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE))
199 return; 213 return;
214 else if (!ClipboardContainsValidUrl(url_text))
215 return;
216 GURL url(url_text);
217 BookmarkNode node(url);
218 node.SetTitle(url_text);
219 BookmarkNodeData bookmark(&node);
220 bookmark_data = bookmark;
200 221
201 if (index == -1) 222 if (index == -1)
202 index = parent->child_count(); 223 index = parent->child_count();
203 ScopedGroupBookmarkActions group_paste(model); 224 ScopedGroupBookmarkActions group_paste(model);
204 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true); 225 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true);
205 } 226 }
206 227
207 bool CanPasteFromClipboard(BookmarkModel* model, const BookmarkNode* node) { 228 bool CanPasteFromClipboard(BookmarkModel* model, const BookmarkNode* node) {
208 if (!node || !model->client()->CanBeEditedByUser(node)) 229 if (!node || !model->client()->CanBeEditedByUser(node))
209 return false; 230 return false;
210 return BookmarkNodeData::ClipboardContainsBookmarks(); 231 base::string16 url_text;
232 if (BookmarkNodeData::ClipboardContainsBookmarks() ||
233 ClipboardContainsValidUrl(url_text))
234 return true;
235 return false;
211 } 236 }
212 237
213 std::vector<const BookmarkNode*> GetMostRecentlyModifiedUserFolders( 238 std::vector<const BookmarkNode*> GetMostRecentlyModifiedUserFolders(
214 BookmarkModel* model, 239 BookmarkModel* model,
215 size_t max_count) { 240 size_t max_count) {
216 std::vector<const BookmarkNode*> nodes; 241 std::vector<const BookmarkNode*> nodes;
217 ui::TreeNodeIterator<const BookmarkNode> iterator(model->root_node(), 242 ui::TreeNodeIterator<const BookmarkNode> iterator(model->root_node(),
218 PruneInvisibleFolders); 243 PruneInvisibleFolders);
219 244
220 while (iterator.has_next()) { 245 while (iterator.has_next()) {
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 } 462 }
438 return false; 463 return false;
439 } 464 }
440 465
441 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) { 466 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) {
442 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate. 467 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate.
443 return GetNodeByID(model->root_node(), id); 468 return GetNodeByID(model->root_node(), id);
444 } 469 }
445 470
446 } // namespace bookmarks 471 } // namespace bookmarks
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698