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" |
11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
15 #include "base/prefs/pref_service.h" | 15 #include "base/prefs/pref_service.h" |
16 #include "base/sha1.h" | 16 #include "base/sha1.h" |
17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
18 #include "base/strings/string16.h" | 18 #include "base/strings/string16.h" |
19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
23 #include "chrome/browser/bookmarks/bookmark_html_writer.h" | 23 #include "chrome/browser/bookmarks/bookmark_html_writer.h" |
24 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 24 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 25 #include "chrome/browser/bookmarks/chrome_bookmark_client.h" |
25 #include "chrome/browser/chrome_notification_types.h" | 26 #include "chrome/browser/chrome_notification_types.h" |
26 #include "chrome/browser/extensions/api/bookmarks/bookmark_api_constants.h" | 27 #include "chrome/browser/extensions/api/bookmarks/bookmark_api_constants.h" |
27 #include "chrome/browser/extensions/api/bookmarks/bookmark_api_helpers.h" | 28 #include "chrome/browser/extensions/api/bookmarks/bookmark_api_helpers.h" |
28 #include "chrome/browser/importer/external_process_importer_host.h" | 29 #include "chrome/browser/importer/external_process_importer_host.h" |
29 #include "chrome/browser/importer/importer_uma.h" | 30 #include "chrome/browser/importer/importer_uma.h" |
30 #include "chrome/browser/platform_util.h" | 31 #include "chrome/browser/platform_util.h" |
31 #include "chrome/browser/profiles/profile.h" | 32 #include "chrome/browser/profiles/profile.h" |
32 #include "chrome/browser/ui/chrome_select_file_policy.h" | 33 #include "chrome/browser/ui/chrome_select_file_policy.h" |
33 #include "chrome/browser/ui/host_desktop.h" | 34 #include "chrome/browser/ui/host_desktop.h" |
34 #include "chrome/common/chrome_paths.h" | 35 #include "chrome/common/chrome_paths.h" |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 if (success) { | 111 if (success) { |
111 content::NotificationService::current()->Notify( | 112 content::NotificationService::current()->Notify( |
112 chrome::NOTIFICATION_EXTENSION_BOOKMARKS_API_INVOKED, | 113 chrome::NOTIFICATION_EXTENSION_BOOKMARKS_API_INVOKED, |
113 content::Source<const Extension>(GetExtension()), | 114 content::Source<const Extension>(GetExtension()), |
114 content::Details<const BookmarksFunction>(this)); | 115 content::Details<const BookmarksFunction>(this)); |
115 } | 116 } |
116 SendResponse(success); | 117 SendResponse(success); |
117 return true; | 118 return true; |
118 } | 119 } |
119 | 120 |
| 121 ChromeBookmarkClient* BookmarksFunction::GetChromeBookmarkClient() { |
| 122 return BookmarkModelFactory::GetChromeBookmarkClientForProfile(GetProfile()); |
| 123 } |
| 124 |
120 bool BookmarksFunction::GetBookmarkIdAsInt64(const std::string& id_string, | 125 bool BookmarksFunction::GetBookmarkIdAsInt64(const std::string& id_string, |
121 int64* id) { | 126 int64* id) { |
122 if (base::StringToInt64(id_string, id)) | 127 if (base::StringToInt64(id_string, id)) |
123 return true; | 128 return true; |
124 | 129 |
125 error_ = keys::kInvalidIdError; | 130 error_ = keys::kInvalidIdError; |
126 return false; | 131 return false; |
127 } | 132 } |
128 | 133 |
129 const BookmarkNode* BookmarksFunction::GetBookmarkNodeFromId( | 134 const BookmarkNode* BookmarksFunction::GetBookmarkNodeFromId( |
(...skipping 17 matching lines...) Expand all Loading... |
147 int64 parentId; | 152 int64 parentId; |
148 | 153 |
149 if (!details.parent_id.get()) { | 154 if (!details.parent_id.get()) { |
150 // Optional, default to "other bookmarks". | 155 // Optional, default to "other bookmarks". |
151 parentId = model->other_node()->id(); | 156 parentId = model->other_node()->id(); |
152 } else { | 157 } else { |
153 if (!GetBookmarkIdAsInt64(*details.parent_id, &parentId)) | 158 if (!GetBookmarkIdAsInt64(*details.parent_id, &parentId)) |
154 return NULL; | 159 return NULL; |
155 } | 160 } |
156 const BookmarkNode* parent = GetBookmarkNodeByID(model, parentId); | 161 const BookmarkNode* parent = GetBookmarkNodeByID(model, parentId); |
157 if (!parent) { | 162 if (!CanBeModified(parent)) |
158 error_ = keys::kNoParentError; | |
159 return NULL; | 163 return NULL; |
160 } | |
161 if (parent->is_root()) { // Can't create children of the root. | |
162 error_ = keys::kModifySpecialError; | |
163 return NULL; | |
164 } | |
165 | 164 |
166 int index; | 165 int index; |
167 if (!details.index.get()) { // Optional (defaults to end). | 166 if (!details.index.get()) { // Optional (defaults to end). |
168 index = parent->child_count(); | 167 index = parent->child_count(); |
169 } else { | 168 } else { |
170 index = *details.index; | 169 index = *details.index; |
171 if (index > parent->child_count() || index < 0) { | 170 if (index > parent->child_count() || index < 0) { |
172 error_ = keys::kInvalidIndexError; | 171 error_ = keys::kInvalidIndexError; |
173 return NULL; | 172 return NULL; |
174 } | 173 } |
(...skipping 29 matching lines...) Expand all Loading... |
204 } | 203 } |
205 | 204 |
206 bool BookmarksFunction::EditBookmarksEnabled() { | 205 bool BookmarksFunction::EditBookmarksEnabled() { |
207 PrefService* prefs = user_prefs::UserPrefs::Get(GetProfile()); | 206 PrefService* prefs = user_prefs::UserPrefs::Get(GetProfile()); |
208 if (prefs->GetBoolean(prefs::kEditBookmarksEnabled)) | 207 if (prefs->GetBoolean(prefs::kEditBookmarksEnabled)) |
209 return true; | 208 return true; |
210 error_ = keys::kEditBookmarksDisabled; | 209 error_ = keys::kEditBookmarksDisabled; |
211 return false; | 210 return false; |
212 } | 211 } |
213 | 212 |
| 213 bool BookmarksFunction::CanBeModified(const BookmarkNode* node) { |
| 214 if (!node) { |
| 215 error_ = keys::kNoParentError; |
| 216 return false; |
| 217 } |
| 218 if (node->is_root()) { |
| 219 error_ = keys::kModifySpecialError; |
| 220 return false; |
| 221 } |
| 222 ChromeBookmarkClient* client = GetChromeBookmarkClient(); |
| 223 if (client->IsDescendantOfManagedNode(node)) { |
| 224 error_ = keys::kModifyManagedError; |
| 225 return false; |
| 226 } |
| 227 return true; |
| 228 } |
| 229 |
214 void BookmarksFunction::BookmarkModelChanged() { | 230 void BookmarksFunction::BookmarkModelChanged() { |
215 } | 231 } |
216 | 232 |
217 void BookmarksFunction::BookmarkModelLoaded(BookmarkModel* model, | 233 void BookmarksFunction::BookmarkModelLoaded(BookmarkModel* model, |
218 bool ids_reassigned) { | 234 bool ids_reassigned) { |
219 model->RemoveObserver(this); | 235 model->RemoveObserver(this); |
220 RunOnReady(); | 236 RunOnReady(); |
221 Release(); // Balanced in RunOnReady(). | 237 Release(); // Balanced in RunOnReady(). |
222 } | 238 } |
223 | 239 |
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
559 EXTENSION_FUNCTION_VALIDATE(params.get()); | 575 EXTENSION_FUNCTION_VALIDATE(params.get()); |
560 | 576 |
561 int64 id; | 577 int64 id; |
562 if (!GetBookmarkIdAsInt64(params->id, &id)) | 578 if (!GetBookmarkIdAsInt64(params->id, &id)) |
563 return false; | 579 return false; |
564 | 580 |
565 bool recursive = false; | 581 bool recursive = false; |
566 if (name() == BookmarksRemoveTreeFunction::function_name()) | 582 if (name() == BookmarksRemoveTreeFunction::function_name()) |
567 recursive = true; | 583 recursive = true; |
568 | 584 |
569 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); | 585 ChromeBookmarkClient* client = GetChromeBookmarkClient(); |
570 if (!bookmark_api_helpers::RemoveNode(model, id, recursive, &error_)) | 586 if (!bookmark_api_helpers::RemoveNode(client, id, recursive, &error_)) |
571 return false; | 587 return false; |
572 | 588 |
573 return true; | 589 return true; |
574 } | 590 } |
575 | 591 |
576 bool BookmarksCreateFunction::RunOnReady() { | 592 bool BookmarksCreateFunction::RunOnReady() { |
577 if (!EditBookmarksEnabled()) | 593 if (!EditBookmarksEnabled()) |
578 return false; | 594 return false; |
579 | 595 |
580 scoped_ptr<bookmarks::Create::Params> params( | 596 scoped_ptr<bookmarks::Create::Params> params( |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
623 if (!params->destination.parent_id.get()) { | 639 if (!params->destination.parent_id.get()) { |
624 // Optional, defaults to current parent. | 640 // Optional, defaults to current parent. |
625 parent = node->parent(); | 641 parent = node->parent(); |
626 } else { | 642 } else { |
627 int64 parentId; | 643 int64 parentId; |
628 if (!GetBookmarkIdAsInt64(*params->destination.parent_id, &parentId)) | 644 if (!GetBookmarkIdAsInt64(*params->destination.parent_id, &parentId)) |
629 return false; | 645 return false; |
630 | 646 |
631 parent = GetBookmarkNodeByID(model, parentId); | 647 parent = GetBookmarkNodeByID(model, parentId); |
632 } | 648 } |
633 if (!parent) { | 649 if (!CanBeModified(parent) || !CanBeModified(node)) |
634 error_ = keys::kNoParentError; | |
635 // TODO(erikkay) return an error message. | |
636 return false; | 650 return false; |
637 } | |
638 if (parent == model->root_node()) { | |
639 error_ = keys::kModifySpecialError; | |
640 return false; | |
641 } | |
642 | 651 |
643 int index; | 652 int index; |
644 if (params->destination.index.get()) { // Optional (defaults to end). | 653 if (params->destination.index.get()) { // Optional (defaults to end). |
645 index = *params->destination.index; | 654 index = *params->destination.index; |
646 if (index > parent->child_count() || index < 0) { | 655 if (index > parent->child_count() || index < 0) { |
647 error_ = keys::kInvalidIndexError; | 656 error_ = keys::kInvalidIndexError; |
648 return false; | 657 return false; |
649 } | 658 } |
650 } else { | 659 } else { |
651 index = parent->child_count(); | 660 index = parent->child_count(); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
688 std::string url_string; | 697 std::string url_string; |
689 if (params->changes.url.get()) | 698 if (params->changes.url.get()) |
690 url_string = *params->changes.url; | 699 url_string = *params->changes.url; |
691 GURL url(url_string); | 700 GURL url(url_string); |
692 if (!url_string.empty() && !url.is_valid()) { | 701 if (!url_string.empty() && !url.is_valid()) { |
693 error_ = keys::kInvalidUrlError; | 702 error_ = keys::kInvalidUrlError; |
694 return false; | 703 return false; |
695 } | 704 } |
696 | 705 |
697 const BookmarkNode* node = GetBookmarkNodeFromId(params->id); | 706 const BookmarkNode* node = GetBookmarkNodeFromId(params->id); |
698 if (!node) | 707 if (!CanBeModified(node)) |
699 return false; | 708 return false; |
700 | 709 |
701 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); | 710 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); |
702 if (model->is_permanent_node(node)) { | 711 if (model->is_permanent_node(node)) { |
703 error_ = keys::kModifySpecialError; | 712 error_ = keys::kModifySpecialError; |
704 return false; | 713 return false; |
705 } | 714 } |
706 if (has_title) | 715 if (has_title) |
707 model->SetTitle(node, title); | 716 model->SetTitle(node, title); |
708 if (!url.is_empty()) | 717 if (!url.is_empty()) |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
889 } | 898 } |
890 | 899 |
891 void BookmarksMoveFunction::GetQuotaLimitHeuristics( | 900 void BookmarksMoveFunction::GetQuotaLimitHeuristics( |
892 QuotaLimitHeuristics* heuristics) const { | 901 QuotaLimitHeuristics* heuristics) const { |
893 BookmarksQuotaLimitFactory::Build<BookmarksMoveFunction>(heuristics); | 902 BookmarksQuotaLimitFactory::Build<BookmarksMoveFunction>(heuristics); |
894 } | 903 } |
895 | 904 |
896 void BookmarksUpdateFunction::GetQuotaLimitHeuristics( | 905 void BookmarksUpdateFunction::GetQuotaLimitHeuristics( |
897 QuotaLimitHeuristics* heuristics) const { | 906 QuotaLimitHeuristics* heuristics) const { |
898 BookmarksQuotaLimitFactory::Build<BookmarksUpdateFunction>(heuristics); | 907 BookmarksQuotaLimitFactory::Build<BookmarksUpdateFunction>(heuristics); |
899 }; | 908 } |
900 | 909 |
901 void BookmarksCreateFunction::GetQuotaLimitHeuristics( | 910 void BookmarksCreateFunction::GetQuotaLimitHeuristics( |
902 QuotaLimitHeuristics* heuristics) const { | 911 QuotaLimitHeuristics* heuristics) const { |
903 BookmarksQuotaLimitFactory::BuildForCreate(heuristics, GetProfile()); | 912 BookmarksQuotaLimitFactory::BuildForCreate(heuristics, GetProfile()); |
904 } | 913 } |
905 | 914 |
906 BookmarksIOFunction::BookmarksIOFunction() {} | 915 BookmarksIOFunction::BookmarksIOFunction() {} |
907 | 916 |
908 BookmarksIOFunction::~BookmarksIOFunction() { | 917 BookmarksIOFunction::~BookmarksIOFunction() { |
909 // There may be pending file dialogs, we need to tell them that we've gone | 918 // 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) | 1034 #if !defined(OS_ANDROID) |
1026 // Android does not have support for the standard exporter. | 1035 // Android does not have support for the standard exporter. |
1027 // TODO(jgreenwald): remove ifdef once extensions are no longer built on | 1036 // TODO(jgreenwald): remove ifdef once extensions are no longer built on |
1028 // Android. | 1037 // Android. |
1029 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL); | 1038 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL); |
1030 #endif | 1039 #endif |
1031 Release(); // Balanced in BookmarksIOFunction::SelectFile() | 1040 Release(); // Balanced in BookmarksIOFunction::SelectFile() |
1032 } | 1041 } |
1033 | 1042 |
1034 } // namespace extensions | 1043 } // namespace extensions |
OLD | NEW |