| 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 <algorithm> | 5 #include <algorithm> |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/api/bookmarks/bookmarks_api.h" | 7 #include "chrome/browser/extensions/api/bookmarks/bookmarks_api.h" |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/i18n/file_util_icu.h" | 11 #include "base/i18n/file_util_icu.h" |
| 12 #include "base/i18n/time_formatting.h" | 12 #include "base/i18n/time_formatting.h" |
| 13 #include "base/json/json_writer.h" | |
| 14 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 15 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 17 #include "base/prefs/pref_service.h" | 16 #include "base/prefs/pref_service.h" |
| 18 #include "base/rand_util.h" | 17 #include "base/rand_util.h" |
| 19 #include "base/sha1.h" | 18 #include "base/sha1.h" |
| 20 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
| 21 #include "base/strings/string16.h" | 20 #include "base/strings/string16.h" |
| 22 #include "base/strings/string_number_conversions.h" | 21 #include "base/strings/string_number_conversions.h" |
| 23 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 42 #include "chrome/common/pref_names.h" | 41 #include "chrome/common/pref_names.h" |
| 43 #include "components/bookmarks/browser/bookmark_model.h" | 42 #include "components/bookmarks/browser/bookmark_model.h" |
| 44 #include "components/bookmarks/browser/bookmark_utils.h" | 43 #include "components/bookmarks/browser/bookmark_utils.h" |
| 45 #include "components/user_prefs/user_prefs.h" | 44 #include "components/user_prefs/user_prefs.h" |
| 46 #include "content/public/browser/browser_context.h" | 45 #include "content/public/browser/browser_context.h" |
| 47 #include "content/public/browser/notification_service.h" | 46 #include "content/public/browser/notification_service.h" |
| 48 #include "content/public/browser/web_contents.h" | 47 #include "content/public/browser/web_contents.h" |
| 49 #include "extensions/browser/event_router.h" | 48 #include "extensions/browser/event_router.h" |
| 50 #include "extensions/browser/extension_function_dispatcher.h" | 49 #include "extensions/browser/extension_function_dispatcher.h" |
| 51 #include "extensions/browser/extension_registry.h" | 50 #include "extensions/browser/extension_registry.h" |
| 52 #include "extensions/browser/quota_service.h" | |
| 53 #include "extensions/common/permissions/permissions_data.h" | 51 #include "extensions/common/permissions/permissions_data.h" |
| 54 #include "grit/generated_resources.h" | 52 #include "grit/generated_resources.h" |
| 55 #include "ui/base/l10n/l10n_util.h" | 53 #include "ui/base/l10n/l10n_util.h" |
| 56 | 54 |
| 57 #if defined(OS_WIN) | 55 #if defined(OS_WIN) |
| 58 #include "ui/aura/remote_window_tree_host_win.h" | 56 #include "ui/aura/remote_window_tree_host_win.h" |
| 59 #endif | 57 #endif |
| 60 | 58 |
| 61 namespace extensions { | 59 namespace extensions { |
| 62 | 60 |
| 63 namespace keys = bookmark_api_constants; | 61 namespace keys = bookmark_api_constants; |
| 64 namespace bookmarks = api::bookmarks; | 62 namespace bookmarks = api::bookmarks; |
| 65 | 63 |
| 66 using base::TimeDelta; | |
| 67 using bookmarks::BookmarkTreeNode; | 64 using bookmarks::BookmarkTreeNode; |
| 68 using bookmarks::CreateDetails; | 65 using bookmarks::CreateDetails; |
| 69 using content::BrowserContext; | 66 using content::BrowserContext; |
| 70 using content::BrowserThread; | 67 using content::BrowserThread; |
| 71 using content::WebContents; | 68 using content::WebContents; |
| 72 | 69 |
| 73 typedef QuotaLimitHeuristic::Bucket Bucket; | |
| 74 typedef QuotaLimitHeuristic::Config Config; | |
| 75 typedef QuotaLimitHeuristic::BucketList BucketList; | |
| 76 typedef QuotaService::TimedLimit TimedLimit; | |
| 77 typedef QuotaService::SustainedLimit SustainedLimit; | |
| 78 typedef QuotaLimitHeuristic::BucketMapper BucketMapper; | |
| 79 | |
| 80 namespace { | 70 namespace { |
| 81 | 71 |
| 82 // Generates a default path (including a default filename) that will be | 72 // Generates a default path (including a default filename) that will be |
| 83 // used for pre-populating the "Export Bookmarks" file chooser dialog box. | 73 // used for pre-populating the "Export Bookmarks" file chooser dialog box. |
| 84 base::FilePath GetDefaultFilepathForBookmarkExport() { | 74 base::FilePath GetDefaultFilepathForBookmarkExport() { |
| 85 base::Time time = base::Time::Now(); | 75 base::Time time = base::Time::Now(); |
| 86 | 76 |
| 87 // Concatenate a date stamp to the filename. | 77 // Concatenate a date stamp to the filename. |
| 88 #if defined(OS_POSIX) | 78 #if defined(OS_POSIX) |
| 89 base::FilePath::StringType filename = | 79 base::FilePath::StringType filename = |
| (...skipping 707 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 797 if (!url.is_empty()) | 787 if (!url.is_empty()) |
| 798 model->SetURL(node, url); | 788 model->SetURL(node, url); |
| 799 | 789 |
| 800 scoped_ptr<BookmarkTreeNode> tree_node( | 790 scoped_ptr<BookmarkTreeNode> tree_node( |
| 801 bookmark_api_helpers::GetBookmarkTreeNode( | 791 bookmark_api_helpers::GetBookmarkTreeNode( |
| 802 GetChromeBookmarkClient(), node, false, false)); | 792 GetChromeBookmarkClient(), node, false, false)); |
| 803 results_ = bookmarks::Update::Results::Create(*tree_node); | 793 results_ = bookmarks::Update::Results::Create(*tree_node); |
| 804 return true; | 794 return true; |
| 805 } | 795 } |
| 806 | 796 |
| 807 // Mapper superclass for BookmarkFunctions. | |
| 808 template <typename BucketIdType> | |
| 809 class BookmarkBucketMapper : public BucketMapper { | |
| 810 public: | |
| 811 virtual ~BookmarkBucketMapper() { STLDeleteValues(&buckets_); } | |
| 812 protected: | |
| 813 Bucket* GetBucket(const BucketIdType& id) { | |
| 814 Bucket* b = buckets_[id]; | |
| 815 if (b == NULL) { | |
| 816 b = new Bucket(); | |
| 817 buckets_[id] = b; | |
| 818 } | |
| 819 return b; | |
| 820 } | |
| 821 private: | |
| 822 std::map<BucketIdType, Bucket*> buckets_; | |
| 823 }; | |
| 824 | |
| 825 // Mapper for 'bookmarks.create'. Maps "same input to bookmarks.create" to a | |
| 826 // unique bucket. | |
| 827 class CreateBookmarkBucketMapper : public BookmarkBucketMapper<std::string> { | |
| 828 public: | |
| 829 explicit CreateBookmarkBucketMapper(BrowserContext* context) | |
| 830 : browser_context_(context) {} | |
| 831 // TODO(tim): This should share code with BookmarksCreateFunction::RunOnReady, | |
| 832 // but I can't figure out a good way to do that with all the macros. | |
| 833 virtual void GetBucketsForArgs(const base::ListValue* args, | |
| 834 BucketList* buckets) OVERRIDE { | |
| 835 const base::DictionaryValue* json; | |
| 836 if (!args->GetDictionary(0, &json)) | |
| 837 return; | |
| 838 | |
| 839 std::string parent_id; | |
| 840 if (json->HasKey(keys::kParentIdKey)) { | |
| 841 if (!json->GetString(keys::kParentIdKey, &parent_id)) | |
| 842 return; | |
| 843 } | |
| 844 BookmarkModel* model = BookmarkModelFactory::GetForProfile( | |
| 845 Profile::FromBrowserContext(browser_context_)); | |
| 846 | |
| 847 int64 parent_id_int64; | |
| 848 base::StringToInt64(parent_id, &parent_id_int64); | |
| 849 const BookmarkNode* parent = | |
| 850 ::bookmarks::GetBookmarkNodeByID(model, parent_id_int64); | |
| 851 if (!parent) | |
| 852 return; | |
| 853 | |
| 854 std::string bucket_id = base::UTF16ToUTF8(parent->GetTitle()); | |
| 855 std::string title; | |
| 856 json->GetString(keys::kTitleKey, &title); | |
| 857 std::string url_string; | |
| 858 json->GetString(keys::kUrlKey, &url_string); | |
| 859 | |
| 860 bucket_id += title; | |
| 861 bucket_id += url_string; | |
| 862 // 20 bytes (SHA1 hash length) is very likely less than most of the | |
| 863 // |bucket_id| strings we construct here, so we hash it to save space. | |
| 864 buckets->push_back(GetBucket(base::SHA1HashString(bucket_id))); | |
| 865 } | |
| 866 | |
| 867 private: | |
| 868 BrowserContext* browser_context_; | |
| 869 }; | |
| 870 | |
| 871 // Mapper for 'bookmarks.remove'. | |
| 872 class RemoveBookmarksBucketMapper : public BookmarkBucketMapper<std::string> { | |
| 873 public: | |
| 874 explicit RemoveBookmarksBucketMapper(BrowserContext* context) | |
| 875 : browser_context_(context) {} | |
| 876 virtual void GetBucketsForArgs(const base::ListValue* args, | |
| 877 BucketList* buckets) OVERRIDE { | |
| 878 typedef std::list<int64> IdList; | |
| 879 IdList ids; | |
| 880 bool invalid_id = false; | |
| 881 if (!BookmarksRemoveFunction::ExtractIds(args, &ids, &invalid_id) || | |
| 882 invalid_id) { | |
| 883 return; | |
| 884 } | |
| 885 | |
| 886 for (IdList::iterator it = ids.begin(); it != ids.end(); ++it) { | |
| 887 BookmarkModel* model = BookmarkModelFactory::GetForProfile( | |
| 888 Profile::FromBrowserContext(browser_context_)); | |
| 889 const BookmarkNode* node = ::bookmarks::GetBookmarkNodeByID(model, *it); | |
| 890 if (!node || node->is_root()) | |
| 891 return; | |
| 892 | |
| 893 std::string bucket_id; | |
| 894 bucket_id += base::UTF16ToUTF8(node->parent()->GetTitle()); | |
| 895 bucket_id += base::UTF16ToUTF8(node->GetTitle()); | |
| 896 bucket_id += node->url().spec(); | |
| 897 buckets->push_back(GetBucket(base::SHA1HashString(bucket_id))); | |
| 898 } | |
| 899 } | |
| 900 | |
| 901 private: | |
| 902 BrowserContext* browser_context_; | |
| 903 }; | |
| 904 | |
| 905 // Mapper for any bookmark function accepting bookmark IDs as parameters, where | |
| 906 // a distinct ID corresponds to a single item in terms of quota limiting. This | |
| 907 // is inappropriate for bookmarks.remove, for example, since repeated removals | |
| 908 // of the same item will actually have a different ID each time. | |
| 909 template <class FunctionType> | |
| 910 class BookmarkIdMapper : public BookmarkBucketMapper<int64> { | |
| 911 public: | |
| 912 typedef std::list<int64> IdList; | |
| 913 virtual void GetBucketsForArgs(const base::ListValue* args, | |
| 914 BucketList* buckets) { | |
| 915 IdList ids; | |
| 916 bool invalid_id = false; | |
| 917 if (!FunctionType::ExtractIds(args, &ids, &invalid_id) || invalid_id) | |
| 918 return; | |
| 919 for (IdList::iterator it = ids.begin(); it != ids.end(); ++it) | |
| 920 buckets->push_back(GetBucket(*it)); | |
| 921 } | |
| 922 }; | |
| 923 | |
| 924 // Builds heuristics for all BookmarkFunctions using specialized BucketMappers. | |
| 925 class BookmarksQuotaLimitFactory { | |
| 926 public: | |
| 927 // For id-based bookmark functions. | |
| 928 template <class FunctionType> | |
| 929 static void Build(QuotaLimitHeuristics* heuristics) { | |
| 930 BuildWithMappers(heuristics, new BookmarkIdMapper<FunctionType>(), | |
| 931 new BookmarkIdMapper<FunctionType>()); | |
| 932 } | |
| 933 | |
| 934 // For bookmarks.create. | |
| 935 static void BuildForCreate(QuotaLimitHeuristics* heuristics, | |
| 936 BrowserContext* context) { | |
| 937 BuildWithMappers(heuristics, | |
| 938 new CreateBookmarkBucketMapper(context), | |
| 939 new CreateBookmarkBucketMapper(context)); | |
| 940 } | |
| 941 | |
| 942 // For bookmarks.remove. | |
| 943 static void BuildForRemove(QuotaLimitHeuristics* heuristics, | |
| 944 BrowserContext* context) { | |
| 945 BuildWithMappers(heuristics, | |
| 946 new RemoveBookmarksBucketMapper(context), | |
| 947 new RemoveBookmarksBucketMapper(context)); | |
| 948 } | |
| 949 | |
| 950 private: | |
| 951 static void BuildWithMappers(QuotaLimitHeuristics* heuristics, | |
| 952 BucketMapper* short_mapper, BucketMapper* long_mapper) { | |
| 953 const Config kSustainedLimitConfig = { | |
| 954 // See bookmarks.json for current value. | |
| 955 bookmarks::MAX_SUSTAINED_WRITE_OPERATIONS_PER_MINUTE, | |
| 956 TimeDelta::FromMinutes(1) | |
| 957 }; | |
| 958 heuristics->push_back(new SustainedLimit( | |
| 959 TimeDelta::FromMinutes(10), | |
| 960 kSustainedLimitConfig, | |
| 961 short_mapper, | |
| 962 "MAX_SUSTAINED_WRITE_OPERATIONS_PER_MINUTE")); | |
| 963 | |
| 964 const Config kTimedLimitConfig = { | |
| 965 // See bookmarks.json for current value. | |
| 966 bookmarks::MAX_WRITE_OPERATIONS_PER_HOUR, | |
| 967 TimeDelta::FromHours(1) | |
| 968 }; | |
| 969 heuristics->push_back(new TimedLimit( | |
| 970 kTimedLimitConfig, | |
| 971 long_mapper, | |
| 972 "MAX_WRITE_OPERATIONS_PER_HOUR")); | |
| 973 } | |
| 974 | |
| 975 DISALLOW_IMPLICIT_CONSTRUCTORS(BookmarksQuotaLimitFactory); | |
| 976 }; | |
| 977 | |
| 978 // And finally, building the individual heuristics for each function. | |
| 979 void BookmarksRemoveFunction::GetQuotaLimitHeuristics( | |
| 980 QuotaLimitHeuristics* heuristics) const { | |
| 981 BookmarksQuotaLimitFactory::BuildForRemove(heuristics, GetProfile()); | |
| 982 } | |
| 983 | |
| 984 void BookmarksMoveFunction::GetQuotaLimitHeuristics( | |
| 985 QuotaLimitHeuristics* heuristics) const { | |
| 986 BookmarksQuotaLimitFactory::Build<BookmarksMoveFunction>(heuristics); | |
| 987 } | |
| 988 | |
| 989 void BookmarksUpdateFunction::GetQuotaLimitHeuristics( | |
| 990 QuotaLimitHeuristics* heuristics) const { | |
| 991 BookmarksQuotaLimitFactory::Build<BookmarksUpdateFunction>(heuristics); | |
| 992 } | |
| 993 | |
| 994 void BookmarksCreateFunction::GetQuotaLimitHeuristics( | |
| 995 QuotaLimitHeuristics* heuristics) const { | |
| 996 BookmarksQuotaLimitFactory::BuildForCreate(heuristics, GetProfile()); | |
| 997 } | |
| 998 | |
| 999 BookmarksIOFunction::BookmarksIOFunction() {} | 797 BookmarksIOFunction::BookmarksIOFunction() {} |
| 1000 | 798 |
| 1001 BookmarksIOFunction::~BookmarksIOFunction() { | 799 BookmarksIOFunction::~BookmarksIOFunction() { |
| 1002 // There may be pending file dialogs, we need to tell them that we've gone | 800 // There may be pending file dialogs, we need to tell them that we've gone |
| 1003 // away so they don't try and call back to us. | 801 // away so they don't try and call back to us. |
| 1004 if (select_file_dialog_.get()) | 802 if (select_file_dialog_.get()) |
| 1005 select_file_dialog_->ListenerDestroyed(); | 803 select_file_dialog_->ListenerDestroyed(); |
| 1006 } | 804 } |
| 1007 | 805 |
| 1008 void BookmarksIOFunction::SelectFile(ui::SelectFileDialog::Type type) { | 806 void BookmarksIOFunction::SelectFile(ui::SelectFileDialog::Type type) { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1110 void BookmarksExportFunction::FileSelected(const base::FilePath& path, | 908 void BookmarksExportFunction::FileSelected(const base::FilePath& path, |
| 1111 int index, | 909 int index, |
| 1112 void* params) { | 910 void* params) { |
| 1113 // TODO(jgreenwald): remove ifdef once extensions are no longer built on | 911 // TODO(jgreenwald): remove ifdef once extensions are no longer built on |
| 1114 // Android. | 912 // Android. |
| 1115 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL); | 913 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL); |
| 1116 Release(); // Balanced in BookmarksIOFunction::SelectFile() | 914 Release(); // Balanced in BookmarksIOFunction::SelectFile() |
| 1117 } | 915 } |
| 1118 | 916 |
| 1119 } // namespace extensions | 917 } // namespace extensions |
| OLD | NEW |