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

Side by Side Diff: chrome/browser/extensions/api/bookmarks/bookmarks_api.cc

Issue 242823002: Extract GetNodeByID() method from BookmarkModel. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/bookmarks/bookmarks_api.h" 5 #include "chrome/browser/extensions/api/bookmarks/bookmarks_api.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/i18n/file_util_icu.h" 9 #include "base/i18n/file_util_icu.h"
10 #include "base/i18n/time_formatting.h" 10 #include "base/i18n/time_formatting.h"
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 error_ = keys::kInvalidIdError; 125 error_ = keys::kInvalidIdError;
126 return false; 126 return false;
127 } 127 }
128 128
129 const BookmarkNode* BookmarksFunction::GetBookmarkNodeFromId( 129 const BookmarkNode* BookmarksFunction::GetBookmarkNodeFromId(
130 const std::string& id_string) { 130 const std::string& id_string) {
131 int64 id; 131 int64 id;
132 if (!GetBookmarkIdAsInt64(id_string, &id)) 132 if (!GetBookmarkIdAsInt64(id_string, &id))
133 return NULL; 133 return NULL;
134 134
135 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); 135 const BookmarkNode* node = bookmark_utils::GetNodeByID(
136 const BookmarkNode* node = model->GetNodeByID(id); 136 BookmarkModelFactory::GetForProfile(GetProfile()), id);
137 if (!node) 137 if (!node)
138 error_ = keys::kNoNodeError; 138 error_ = keys::kNoNodeError;
139 139
140 return node; 140 return node;
141 } 141 }
142 142
143 bool BookmarksFunction::EditBookmarksEnabled() { 143 bool BookmarksFunction::EditBookmarksEnabled() {
144 PrefService* prefs = user_prefs::UserPrefs::Get(GetProfile()); 144 PrefService* prefs = user_prefs::UserPrefs::Get(GetProfile());
145 if (prefs->GetBoolean(prefs::kEditBookmarksEnabled)) 145 if (prefs->GetBoolean(prefs::kEditBookmarksEnabled))
146 return true; 146 return true;
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after
521 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); 521 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile());
522 int64 parentId; 522 int64 parentId;
523 523
524 if (!params->bookmark.parent_id.get()) { 524 if (!params->bookmark.parent_id.get()) {
525 // Optional, default to "other bookmarks". 525 // Optional, default to "other bookmarks".
526 parentId = model->other_node()->id(); 526 parentId = model->other_node()->id();
527 } else { 527 } else {
528 if (!GetBookmarkIdAsInt64(*params->bookmark.parent_id, &parentId)) 528 if (!GetBookmarkIdAsInt64(*params->bookmark.parent_id, &parentId))
529 return false; 529 return false;
530 } 530 }
531 const BookmarkNode* parent = model->GetNodeByID(parentId); 531 const BookmarkNode* parent = bookmark_utils::GetNodeByID(model, parentId);
532 if (!parent) { 532 if (!parent) {
533 error_ = keys::kNoParentError; 533 error_ = keys::kNoParentError;
534 return false; 534 return false;
535 } 535 }
536 if (parent->is_root()) { // Can't create children of the root. 536 if (parent->is_root()) { // Can't create children of the root.
537 error_ = keys::kModifySpecialError; 537 error_ = keys::kModifySpecialError;
538 return false; 538 return false;
539 } 539 }
540 540
541 int index; 541 int index;
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
609 609
610 const BookmarkNode* parent = NULL; 610 const BookmarkNode* parent = NULL;
611 if (!params->destination.parent_id.get()) { 611 if (!params->destination.parent_id.get()) {
612 // Optional, defaults to current parent. 612 // Optional, defaults to current parent.
613 parent = node->parent(); 613 parent = node->parent();
614 } else { 614 } else {
615 int64 parentId; 615 int64 parentId;
616 if (!GetBookmarkIdAsInt64(*params->destination.parent_id, &parentId)) 616 if (!GetBookmarkIdAsInt64(*params->destination.parent_id, &parentId))
617 return false; 617 return false;
618 618
619 parent = model->GetNodeByID(parentId); 619 parent = bookmark_utils::GetNodeByID(model, parentId);
620 } 620 }
621 if (!parent) { 621 if (!parent) {
622 error_ = keys::kNoParentError; 622 error_ = keys::kNoParentError;
623 // TODO(erikkay) return an error message. 623 // TODO(erikkay) return an error message.
624 return false; 624 return false;
625 } 625 }
626 if (parent == model->root_node()) { 626 if (parent == model->root_node()) {
627 error_ = keys::kModifySpecialError; 627 error_ = keys::kModifySpecialError;
628 return false; 628 return false;
629 } 629 }
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
737 std::string parent_id; 737 std::string parent_id;
738 if (json->HasKey(keys::kParentIdKey)) { 738 if (json->HasKey(keys::kParentIdKey)) {
739 if (!json->GetString(keys::kParentIdKey, &parent_id)) 739 if (!json->GetString(keys::kParentIdKey, &parent_id))
740 return; 740 return;
741 } 741 }
742 BookmarkModel* model = BookmarkModelFactory::GetForProfile( 742 BookmarkModel* model = BookmarkModelFactory::GetForProfile(
743 Profile::FromBrowserContext(browser_context_)); 743 Profile::FromBrowserContext(browser_context_));
744 744
745 int64 parent_id_int64; 745 int64 parent_id_int64;
746 base::StringToInt64(parent_id, &parent_id_int64); 746 base::StringToInt64(parent_id, &parent_id_int64);
747 const BookmarkNode* parent = model->GetNodeByID(parent_id_int64); 747 const BookmarkNode* parent =
748 bookmark_utils::GetNodeByID(model, parent_id_int64);
748 if (!parent) 749 if (!parent)
749 return; 750 return;
750 751
751 std::string bucket_id = base::UTF16ToUTF8(parent->GetTitle()); 752 std::string bucket_id = base::UTF16ToUTF8(parent->GetTitle());
752 std::string title; 753 std::string title;
753 json->GetString(keys::kTitleKey, &title); 754 json->GetString(keys::kTitleKey, &title);
754 std::string url_string; 755 std::string url_string;
755 json->GetString(keys::kUrlKey, &url_string); 756 json->GetString(keys::kUrlKey, &url_string);
756 757
757 bucket_id += title; 758 bucket_id += title;
(...skipping 17 matching lines...) Expand all
775 IdList ids; 776 IdList ids;
776 bool invalid_id = false; 777 bool invalid_id = false;
777 if (!BookmarksRemoveFunction::ExtractIds(args, &ids, &invalid_id) || 778 if (!BookmarksRemoveFunction::ExtractIds(args, &ids, &invalid_id) ||
778 invalid_id) { 779 invalid_id) {
779 return; 780 return;
780 } 781 }
781 782
782 for (IdList::iterator it = ids.begin(); it != ids.end(); ++it) { 783 for (IdList::iterator it = ids.begin(); it != ids.end(); ++it) {
783 BookmarkModel* model = BookmarkModelFactory::GetForProfile( 784 BookmarkModel* model = BookmarkModelFactory::GetForProfile(
784 Profile::FromBrowserContext(browser_context_)); 785 Profile::FromBrowserContext(browser_context_));
785 const BookmarkNode* node = model->GetNodeByID(*it); 786 const BookmarkNode* node = bookmark_utils::GetNodeByID(model, *it);
786 if (!node || node->is_root()) 787 if (!node || node->is_root())
787 return; 788 return;
788 789
789 std::string bucket_id; 790 std::string bucket_id;
790 bucket_id += base::UTF16ToUTF8(node->parent()->GetTitle()); 791 bucket_id += base::UTF16ToUTF8(node->parent()->GetTitle());
791 bucket_id += base::UTF16ToUTF8(node->GetTitle()); 792 bucket_id += base::UTF16ToUTF8(node->GetTitle());
792 bucket_id += node->url().spec(); 793 bucket_id += node->url().spec();
793 buckets->push_back(GetBucket(base::SHA1HashString(bucket_id))); 794 buckets->push_back(GetBucket(base::SHA1HashString(bucket_id)));
794 } 795 }
795 } 796 }
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
1013 #if !defined(OS_ANDROID) 1014 #if !defined(OS_ANDROID)
1014 // Android does not have support for the standard exporter. 1015 // Android does not have support for the standard exporter.
1015 // TODO(jgreenwald): remove ifdef once extensions are no longer built on 1016 // TODO(jgreenwald): remove ifdef once extensions are no longer built on
1016 // Android. 1017 // Android.
1017 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL); 1018 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL);
1018 #endif 1019 #endif
1019 Release(); // Balanced in BookmarksIOFunction::SelectFile() 1020 Release(); // Balanced in BookmarksIOFunction::SelectFile()
1020 } 1021 }
1021 1022
1022 } // namespace extensions 1023 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698