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

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 suggested test case and changed fucntion name. 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 URL if present in clipboard
sky 2014/09/08 16:26:18 Returns the URL from the clipboard. If there is no
146 GURL GetUrlFromClipboard() {
147 base::string16 url_text;
148 if (!ui::Clipboard::GetForCurrentThread()->IsFormatAvailable(
149 ui::Clipboard::GetUrlWFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE))
150 return GURL();
151
152 ui::Clipboard::GetForCurrentThread()->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE,
153 &url_text);
154 return GURL(url_text);
155 }
156
145 } // namespace 157 } // namespace
146 158
147 QueryFields::QueryFields() {} 159 QueryFields::QueryFields() {}
148 QueryFields::~QueryFields() {} 160 QueryFields::~QueryFields() {}
149 161
150 void CloneBookmarkNode(BookmarkModel* model, 162 void CloneBookmarkNode(BookmarkModel* model,
151 const std::vector<BookmarkNodeData::Element>& elements, 163 const std::vector<BookmarkNodeData::Element>& elements,
152 const BookmarkNode* parent, 164 const BookmarkNode* parent,
153 int index_to_add_at, 165 int index_to_add_at,
154 bool reset_node_times) { 166 bool reset_node_times) {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 } 200 }
189 } 201 }
190 202
191 void PasteFromClipboard(BookmarkModel* model, 203 void PasteFromClipboard(BookmarkModel* model,
192 const BookmarkNode* parent, 204 const BookmarkNode* parent,
193 int index) { 205 int index) {
194 if (!parent) 206 if (!parent)
195 return; 207 return;
196 208
197 BookmarkNodeData bookmark_data; 209 BookmarkNodeData bookmark_data;
198 if (!bookmark_data.ReadFromClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE)) 210 if (!bookmark_data.ReadFromClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE)) {
199 return; 211 GURL url = GetUrlFromClipboard();
200 212 if (!url.is_valid())
213 return;
214 BookmarkNode node(url);
215 node.SetTitle(base::ASCIIToUTF16(url.spec()));
216 bookmark_data = BookmarkNodeData(&node);
217 }
201 if (index == -1) 218 if (index == -1)
202 index = parent->child_count(); 219 index = parent->child_count();
203 ScopedGroupBookmarkActions group_paste(model); 220 ScopedGroupBookmarkActions group_paste(model);
204 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true); 221 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true);
205 } 222 }
206 223
207 bool CanPasteFromClipboard(BookmarkModel* model, const BookmarkNode* node) { 224 bool CanPasteFromClipboard(BookmarkModel* model, const BookmarkNode* node) {
208 if (!node || !model->client()->CanBeEditedByUser(node)) 225 if (!node || !model->client()->CanBeEditedByUser(node))
209 return false; 226 return false;
210 return BookmarkNodeData::ClipboardContainsBookmarks(); 227 return (BookmarkNodeData::ClipboardContainsBookmarks() ||
228 GetUrlFromClipboard().is_valid());
211 } 229 }
212 230
213 std::vector<const BookmarkNode*> GetMostRecentlyModifiedUserFolders( 231 std::vector<const BookmarkNode*> GetMostRecentlyModifiedUserFolders(
214 BookmarkModel* model, 232 BookmarkModel* model,
215 size_t max_count) { 233 size_t max_count) {
216 std::vector<const BookmarkNode*> nodes; 234 std::vector<const BookmarkNode*> nodes;
217 ui::TreeNodeIterator<const BookmarkNode> iterator(model->root_node(), 235 ui::TreeNodeIterator<const BookmarkNode> iterator(model->root_node(),
218 PruneInvisibleFolders); 236 PruneInvisibleFolders);
219 237
220 while (iterator.has_next()) { 238 while (iterator.has_next()) {
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 } 455 }
438 return false; 456 return false;
439 } 457 }
440 458
441 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) { 459 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) {
442 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate. 460 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate.
443 return GetNodeByID(model->root_node(), id); 461 return GetNodeByID(model->root_node(), id);
444 } 462 }
445 463
446 } // namespace bookmarks 464 } // namespace bookmarks
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698