Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 int64 parentId; | 147 int64 parentId; |
| 148 | 148 |
| 149 if (!details.parent_id.get()) { | 149 if (!details.parent_id.get()) { |
| 150 // Optional, default to "other bookmarks". | 150 // Optional, default to "other bookmarks". |
| 151 parentId = model->other_node()->id(); | 151 parentId = model->other_node()->id(); |
| 152 } else { | 152 } else { |
| 153 if (!GetBookmarkIdAsInt64(*details.parent_id, &parentId)) | 153 if (!GetBookmarkIdAsInt64(*details.parent_id, &parentId)) |
| 154 return NULL; | 154 return NULL; |
| 155 } | 155 } |
| 156 const BookmarkNode* parent = GetBookmarkNodeByID(model, parentId); | 156 const BookmarkNode* parent = GetBookmarkNodeByID(model, parentId); |
| 157 if (!parent) { | 157 if (!CanBeModified(parent)) |
| 158 error_ = keys::kNoParentError; | |
| 159 return NULL; | 158 return NULL; |
| 160 } | |
| 161 if (parent->is_root()) { // Can't create children of the root. | |
| 162 error_ = keys::kModifySpecialError; | |
| 163 return NULL; | |
| 164 } | |
| 165 | 159 |
| 166 int index; | 160 int index; |
| 167 if (!details.index.get()) { // Optional (defaults to end). | 161 if (!details.index.get()) { // Optional (defaults to end). |
| 168 index = parent->child_count(); | 162 index = parent->child_count(); |
| 169 } else { | 163 } else { |
| 170 index = *details.index; | 164 index = *details.index; |
| 171 if (index > parent->child_count() || index < 0) { | 165 if (index > parent->child_count() || index < 0) { |
| 172 error_ = keys::kInvalidIndexError; | 166 error_ = keys::kInvalidIndexError; |
| 173 return NULL; | 167 return NULL; |
| 174 } | 168 } |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 204 } | 198 } |
| 205 | 199 |
| 206 bool BookmarksFunction::EditBookmarksEnabled() { | 200 bool BookmarksFunction::EditBookmarksEnabled() { |
| 207 PrefService* prefs = user_prefs::UserPrefs::Get(GetProfile()); | 201 PrefService* prefs = user_prefs::UserPrefs::Get(GetProfile()); |
| 208 if (prefs->GetBoolean(prefs::kEditBookmarksEnabled)) | 202 if (prefs->GetBoolean(prefs::kEditBookmarksEnabled)) |
| 209 return true; | 203 return true; |
| 210 error_ = keys::kEditBookmarksDisabled; | 204 error_ = keys::kEditBookmarksDisabled; |
| 211 return false; | 205 return false; |
| 212 } | 206 } |
| 213 | 207 |
| 208 bool BookmarksFunction::CanBeModified(const BookmarkNode* node) { | |
| 209 if (!node) { | |
| 210 error_ = keys::kNoParentError; | |
| 211 return false; | |
| 212 } | |
| 213 if (node->is_root()) { | |
| 214 error_ = keys::kModifySpecialError; | |
| 215 return false; | |
| 216 } | |
| 217 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); | |
| 218 if (model->IsManaged(node)) { | |
| 219 error_ = keys::kModifyManagedError; | |
| 220 return false; | |
| 221 } | |
| 222 return true; | |
| 223 } | |
| 224 | |
| 214 void BookmarksFunction::BookmarkModelChanged() { | 225 void BookmarksFunction::BookmarkModelChanged() { |
| 215 } | 226 } |
| 216 | 227 |
| 217 void BookmarksFunction::BookmarkModelLoaded(BookmarkModel* model, | 228 void BookmarksFunction::BookmarkModelLoaded(BookmarkModel* model, |
| 218 bool ids_reassigned) { | 229 bool ids_reassigned) { |
| 219 model->RemoveObserver(this); | 230 model->RemoveObserver(this); |
| 220 RunOnReady(); | 231 RunOnReady(); |
| 221 Release(); // Balanced in RunOnReady(). | 232 Release(); // Balanced in RunOnReady(). |
| 222 } | 233 } |
| 223 | 234 |
| (...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 623 if (!params->destination.parent_id.get()) { | 634 if (!params->destination.parent_id.get()) { |
| 624 // Optional, defaults to current parent. | 635 // Optional, defaults to current parent. |
| 625 parent = node->parent(); | 636 parent = node->parent(); |
| 626 } else { | 637 } else { |
| 627 int64 parentId; | 638 int64 parentId; |
| 628 if (!GetBookmarkIdAsInt64(*params->destination.parent_id, &parentId)) | 639 if (!GetBookmarkIdAsInt64(*params->destination.parent_id, &parentId)) |
| 629 return false; | 640 return false; |
| 630 | 641 |
| 631 parent = GetBookmarkNodeByID(model, parentId); | 642 parent = GetBookmarkNodeByID(model, parentId); |
| 632 } | 643 } |
| 633 if (!parent) { | 644 if (!CanBeModified(parent)) |
| 634 error_ = keys::kNoParentError; | |
| 635 // TODO(erikkay) return an error message. | |
| 636 return false; | 645 return false; |
| 637 } | 646 if (model->IsManaged(node)) { |
|
not at google - send to devlin
2014/06/03 18:09:16
why not !CanBeModified(node)?
Joao da Silva
2014/06/05 17:10:32
I thought some of the checks in CanBeModified are
| |
| 638 if (parent == model->root_node()) { | 647 error_ = keys::kModifyManagedError; |
| 639 error_ = keys::kModifySpecialError; | |
| 640 return false; | 648 return false; |
| 641 } | 649 } |
| 642 | 650 |
| 643 int index; | 651 int index; |
| 644 if (params->destination.index.get()) { // Optional (defaults to end). | 652 if (params->destination.index.get()) { // Optional (defaults to end). |
| 645 index = *params->destination.index; | 653 index = *params->destination.index; |
| 646 if (index > parent->child_count() || index < 0) { | 654 if (index > parent->child_count() || index < 0) { |
| 647 error_ = keys::kInvalidIndexError; | 655 error_ = keys::kInvalidIndexError; |
| 648 return false; | 656 return false; |
| 649 } | 657 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 688 std::string url_string; | 696 std::string url_string; |
| 689 if (params->changes.url.get()) | 697 if (params->changes.url.get()) |
| 690 url_string = *params->changes.url; | 698 url_string = *params->changes.url; |
| 691 GURL url(url_string); | 699 GURL url(url_string); |
| 692 if (!url_string.empty() && !url.is_valid()) { | 700 if (!url_string.empty() && !url.is_valid()) { |
| 693 error_ = keys::kInvalidUrlError; | 701 error_ = keys::kInvalidUrlError; |
| 694 return false; | 702 return false; |
| 695 } | 703 } |
| 696 | 704 |
| 697 const BookmarkNode* node = GetBookmarkNodeFromId(params->id); | 705 const BookmarkNode* node = GetBookmarkNodeFromId(params->id); |
| 698 if (!node) | 706 if (!CanBeModified(node)) |
| 699 return false; | 707 return false; |
| 700 | 708 |
| 701 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); | 709 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); |
| 702 if (model->is_permanent_node(node)) { | 710 if (model->is_permanent_node(node)) { |
| 703 error_ = keys::kModifySpecialError; | 711 error_ = keys::kModifySpecialError; |
| 704 return false; | 712 return false; |
| 705 } | 713 } |
| 706 if (has_title) | 714 if (has_title) |
| 707 model->SetTitle(node, title); | 715 model->SetTitle(node, title); |
| 708 if (!url.is_empty()) | 716 if (!url.is_empty()) |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 889 } | 897 } |
| 890 | 898 |
| 891 void BookmarksMoveFunction::GetQuotaLimitHeuristics( | 899 void BookmarksMoveFunction::GetQuotaLimitHeuristics( |
| 892 QuotaLimitHeuristics* heuristics) const { | 900 QuotaLimitHeuristics* heuristics) const { |
| 893 BookmarksQuotaLimitFactory::Build<BookmarksMoveFunction>(heuristics); | 901 BookmarksQuotaLimitFactory::Build<BookmarksMoveFunction>(heuristics); |
| 894 } | 902 } |
| 895 | 903 |
| 896 void BookmarksUpdateFunction::GetQuotaLimitHeuristics( | 904 void BookmarksUpdateFunction::GetQuotaLimitHeuristics( |
| 897 QuotaLimitHeuristics* heuristics) const { | 905 QuotaLimitHeuristics* heuristics) const { |
| 898 BookmarksQuotaLimitFactory::Build<BookmarksUpdateFunction>(heuristics); | 906 BookmarksQuotaLimitFactory::Build<BookmarksUpdateFunction>(heuristics); |
| 899 }; | 907 } |
| 900 | 908 |
| 901 void BookmarksCreateFunction::GetQuotaLimitHeuristics( | 909 void BookmarksCreateFunction::GetQuotaLimitHeuristics( |
| 902 QuotaLimitHeuristics* heuristics) const { | 910 QuotaLimitHeuristics* heuristics) const { |
| 903 BookmarksQuotaLimitFactory::BuildForCreate(heuristics, GetProfile()); | 911 BookmarksQuotaLimitFactory::BuildForCreate(heuristics, GetProfile()); |
| 904 } | 912 } |
| 905 | 913 |
| 906 BookmarksIOFunction::BookmarksIOFunction() {} | 914 BookmarksIOFunction::BookmarksIOFunction() {} |
| 907 | 915 |
| 908 BookmarksIOFunction::~BookmarksIOFunction() { | 916 BookmarksIOFunction::~BookmarksIOFunction() { |
| 909 // There may be pending file dialogs, we need to tell them that we've gone | 917 // There may be pending file dialogs, we need to tell them that we've gone |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1025 #if !defined(OS_ANDROID) | 1033 #if !defined(OS_ANDROID) |
| 1026 // Android does not have support for the standard exporter. | 1034 // Android does not have support for the standard exporter. |
| 1027 // TODO(jgreenwald): remove ifdef once extensions are no longer built on | 1035 // TODO(jgreenwald): remove ifdef once extensions are no longer built on |
| 1028 // Android. | 1036 // Android. |
| 1029 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL); | 1037 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL); |
| 1030 #endif | 1038 #endif |
| 1031 Release(); // Balanced in BookmarksIOFunction::SelectFile() | 1039 Release(); // Balanced in BookmarksIOFunction::SelectFile() |
| 1032 } | 1040 } |
| 1033 | 1041 |
| 1034 } // namespace extensions | 1042 } // namespace extensions |
| OLD | NEW |