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

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: Created new test assertion Created 6 years, 3 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 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 135
136 // If we're in the middle of an escape sequence, truncate just before it. 136 // If we're in the middle of an escape sequence, truncate just before it.
137 if (url[kCleanedUpUrlMaxLength - 1] == '%') 137 if (url[kCleanedUpUrlMaxLength - 1] == '%')
138 return url.substr(0, kCleanedUpUrlMaxLength - 1); 138 return url.substr(0, kCleanedUpUrlMaxLength - 1);
139 if (url[kCleanedUpUrlMaxLength - 2] == '%') 139 if (url[kCleanedUpUrlMaxLength - 2] == '%')
140 return url.substr(0, kCleanedUpUrlMaxLength - 2); 140 return url.substr(0, kCleanedUpUrlMaxLength - 2);
141 141
142 return url.substr(0, kCleanedUpUrlMaxLength); 142 return url.substr(0, kCleanedUpUrlMaxLength);
143 } 143 }
144 144
145 // Returns the URL from the clipboard. If there is no URL an empty URL is
146 // returned.
147 GURL GetUrlFromClipboard() {
148 base::string16 url_text;
149 if (!ui::Clipboard::GetForCurrentThread()->IsFormatAvailable(
150 ui::Clipboard::GetUrlWFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE))
151 return GURL();
152
153 ui::Clipboard::GetForCurrentThread()->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE,
154 &url_text);
155 return GURL(url_text);
156 }
157
145 } // namespace 158 } // namespace
146 159
147 QueryFields::QueryFields() {} 160 QueryFields::QueryFields() {}
148 QueryFields::~QueryFields() {} 161 QueryFields::~QueryFields() {}
149 162
150 void CloneBookmarkNode(BookmarkModel* model, 163 void CloneBookmarkNode(BookmarkModel* model,
151 const std::vector<BookmarkNodeData::Element>& elements, 164 const std::vector<BookmarkNodeData::Element>& elements,
152 const BookmarkNode* parent, 165 const BookmarkNode* parent,
153 int index_to_add_at, 166 int index_to_add_at,
154 bool reset_node_times) { 167 bool reset_node_times) {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 } 201 }
189 } 202 }
190 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;
198 if (!bookmark_data.ReadFromClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE)) 211 if (!bookmark_data.ReadFromClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE)) {
199 return; 212 GURL url = GetUrlFromClipboard();
200 213 if (!url.is_valid())
214 return;
215 BookmarkNode node(url);
216 node.SetTitle(base::ASCIIToUTF16(url.spec()));
217 bookmark_data = BookmarkNodeData(&node);
218 }
201 if (index == -1) 219 if (index == -1)
202 index = parent->child_count(); 220 index = parent->child_count();
203 ScopedGroupBookmarkActions group_paste(model); 221 ScopedGroupBookmarkActions group_paste(model);
204 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true); 222 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true);
205 } 223 }
206 224
207 bool CanPasteFromClipboard(BookmarkModel* model, const BookmarkNode* node) { 225 bool CanPasteFromClipboard(BookmarkModel* model, const BookmarkNode* node) {
208 if (!node || !model->client()->CanBeEditedByUser(node)) 226 if (!node || !model->client()->CanBeEditedByUser(node))
209 return false; 227 return false;
210 return BookmarkNodeData::ClipboardContainsBookmarks(); 228 return (BookmarkNodeData::ClipboardContainsBookmarks() ||
229 GetUrlFromClipboard().is_valid());
211 } 230 }
212 231
213 std::vector<const BookmarkNode*> GetMostRecentlyModifiedUserFolders( 232 std::vector<const BookmarkNode*> GetMostRecentlyModifiedUserFolders(
214 BookmarkModel* model, 233 BookmarkModel* model,
215 size_t max_count) { 234 size_t max_count) {
216 std::vector<const BookmarkNode*> nodes; 235 std::vector<const BookmarkNode*> nodes;
217 ui::TreeNodeIterator<const BookmarkNode> iterator(model->root_node(), 236 ui::TreeNodeIterator<const BookmarkNode> iterator(model->root_node(),
218 PruneInvisibleFolders); 237 PruneInvisibleFolders);
219 238
220 while (iterator.has_next()) { 239 while (iterator.has_next()) {
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 } 456 }
438 return false; 457 return false;
439 } 458 }
440 459
441 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) { 460 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) {
442 // 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.
443 return GetNodeByID(model->root_node(), id); 462 return GetNodeByID(model->root_node(), id);
444 } 463 }
445 464
446 } // namespace bookmarks 465 } // namespace bookmarks
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698