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

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

Issue 171813010: Move ProfileKeyedAPI implementations to take BrowserContext in the constructor (part 1). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 10 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 21 matching lines...) Expand all
32 #include "chrome/browser/importer/importer_uma.h" 32 #include "chrome/browser/importer/importer_uma.h"
33 #include "chrome/browser/platform_util.h" 33 #include "chrome/browser/platform_util.h"
34 #include "chrome/browser/profiles/profile.h" 34 #include "chrome/browser/profiles/profile.h"
35 #include "chrome/browser/ui/chrome_select_file_policy.h" 35 #include "chrome/browser/ui/chrome_select_file_policy.h"
36 #include "chrome/browser/ui/host_desktop.h" 36 #include "chrome/browser/ui/host_desktop.h"
37 #include "chrome/common/chrome_paths.h" 37 #include "chrome/common/chrome_paths.h"
38 #include "chrome/common/extensions/api/bookmarks.h" 38 #include "chrome/common/extensions/api/bookmarks.h"
39 #include "chrome/common/importer/importer_data_types.h" 39 #include "chrome/common/importer/importer_data_types.h"
40 #include "chrome/common/pref_names.h" 40 #include "chrome/common/pref_names.h"
41 #include "components/user_prefs/user_prefs.h" 41 #include "components/user_prefs/user_prefs.h"
42 #include "content/public/browser/browser_context.h"
42 #include "content/public/browser/notification_service.h" 43 #include "content/public/browser/notification_service.h"
43 #include "content/public/browser/web_contents.h" 44 #include "content/public/browser/web_contents.h"
44 #include "content/public/browser/web_contents_view.h" 45 #include "content/public/browser/web_contents_view.h"
45 #include "extensions/browser/event_router.h" 46 #include "extensions/browser/event_router.h"
46 #include "extensions/browser/extension_system.h" 47 #include "extensions/browser/extension_system.h"
47 #include "extensions/browser/quota_service.h" 48 #include "extensions/browser/quota_service.h"
48 #include "grit/generated_resources.h" 49 #include "grit/generated_resources.h"
49 #include "ui/base/l10n/l10n_util.h" 50 #include "ui/base/l10n/l10n_util.h"
50 51
51 #if defined(OS_WIN) 52 #if defined(OS_WIN)
52 #include "ui/aura/remote_window_tree_host_win.h" 53 #include "ui/aura/remote_window_tree_host_win.h"
53 #endif 54 #endif
54 55
55 namespace extensions { 56 namespace extensions {
56 57
57 namespace keys = bookmark_api_constants; 58 namespace keys = bookmark_api_constants;
58 namespace bookmarks = api::bookmarks; 59 namespace bookmarks = api::bookmarks;
59 60
60 using base::TimeDelta; 61 using base::TimeDelta;
61 using bookmarks::BookmarkTreeNode; 62 using bookmarks::BookmarkTreeNode;
63 using content::BrowserContext;
62 using content::BrowserThread; 64 using content::BrowserThread;
63 using content::WebContents; 65 using content::WebContents;
64 66
65 typedef QuotaLimitHeuristic::Bucket Bucket; 67 typedef QuotaLimitHeuristic::Bucket Bucket;
66 typedef QuotaLimitHeuristic::Config Config; 68 typedef QuotaLimitHeuristic::Config Config;
67 typedef QuotaLimitHeuristic::BucketList BucketList; 69 typedef QuotaLimitHeuristic::BucketList BucketList;
68 typedef QuotaService::TimedLimit TimedLimit; 70 typedef QuotaService::TimedLimit TimedLimit;
69 typedef QuotaService::SustainedLimit SustainedLimit; 71 typedef QuotaService::SustainedLimit SustainedLimit;
70 typedef QuotaLimitHeuristic::BucketMapper BucketMapper; 72 typedef QuotaLimitHeuristic::BucketMapper BucketMapper;
71 73
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 void BookmarksFunction::BookmarkModelChanged() { 151 void BookmarksFunction::BookmarkModelChanged() {
150 } 152 }
151 153
152 void BookmarksFunction::BookmarkModelLoaded(BookmarkModel* model, 154 void BookmarksFunction::BookmarkModelLoaded(BookmarkModel* model,
153 bool ids_reassigned) { 155 bool ids_reassigned) {
154 model->RemoveObserver(this); 156 model->RemoveObserver(this);
155 Run(); 157 Run();
156 Release(); // Balanced in Run(). 158 Release(); // Balanced in Run().
157 } 159 }
158 160
159 BookmarkEventRouter::BookmarkEventRouter(Profile* profile, BookmarkModel* model) 161 BookmarkEventRouter::BookmarkEventRouter(BrowserContext* context,
160 : profile_(profile), 162 BookmarkModel* model)
161 model_(model) { 163 : context_(context), model_(model) {
162 model_->AddObserver(this); 164 model_->AddObserver(this);
163 } 165 }
164 166
165 BookmarkEventRouter::~BookmarkEventRouter() { 167 BookmarkEventRouter::~BookmarkEventRouter() {
166 if (model_) { 168 if (model_) {
167 model_->RemoveObserver(this); 169 model_->RemoveObserver(this);
168 } 170 }
169 } 171 }
170 172
171 void BookmarkEventRouter::DispatchEvent( 173 void BookmarkEventRouter::DispatchEvent(
172 const std::string& event_name, 174 const std::string& event_name,
173 scoped_ptr<base::ListValue> event_args) { 175 scoped_ptr<base::ListValue> event_args) {
174 if (extensions::ExtensionSystem::Get(profile_)->event_router()) { 176 if (extensions::ExtensionSystem::Get(context_)->event_router()) {
175 extensions::ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent( 177 extensions::ExtensionSystem::Get(context_)->event_router()->BroadcastEvent(
176 make_scoped_ptr(new extensions::Event(event_name, event_args.Pass()))); 178 make_scoped_ptr(new extensions::Event(event_name, event_args.Pass())));
177 } 179 }
178 } 180 }
179 181
180 void BookmarkEventRouter::BookmarkModelLoaded(BookmarkModel* model, 182 void BookmarkEventRouter::BookmarkModelLoaded(BookmarkModel* model,
181 bool ids_reassigned) { 183 bool ids_reassigned) {
182 // TODO(erikkay): Perhaps we should send this event down to the extension 184 // TODO(erikkay): Perhaps we should send this event down to the extension
183 // so they know when it's safe to use the API? 185 // so they know when it's safe to use the API?
184 } 186 }
185 187
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 BookmarkModel* model) { 292 BookmarkModel* model) {
291 scoped_ptr<base::ListValue> args(new base::ListValue()); 293 scoped_ptr<base::ListValue> args(new base::ListValue());
292 DispatchEvent(bookmarks::OnImportBegan::kEventName, args.Pass()); 294 DispatchEvent(bookmarks::OnImportBegan::kEventName, args.Pass());
293 } 295 }
294 296
295 void BookmarkEventRouter::ExtensiveBookmarkChangesEnded(BookmarkModel* model) { 297 void BookmarkEventRouter::ExtensiveBookmarkChangesEnded(BookmarkModel* model) {
296 scoped_ptr<base::ListValue> args(new base::ListValue()); 298 scoped_ptr<base::ListValue> args(new base::ListValue());
297 DispatchEvent(bookmarks::OnImportEnded::kEventName, args.Pass()); 299 DispatchEvent(bookmarks::OnImportEnded::kEventName, args.Pass());
298 } 300 }
299 301
300 BookmarksAPI::BookmarksAPI(Profile* profile) : profile_(profile) { 302 BookmarksAPI::BookmarksAPI(BrowserContext* context) : context_(context) {
301 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( 303 EventRouter* event_router = ExtensionSystem::Get(context_)->event_router();
302 this, bookmarks::OnCreated::kEventName); 304 event_router->RegisterObserver(this, bookmarks::OnCreated::kEventName);
303 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( 305 event_router->RegisterObserver(this, bookmarks::OnRemoved::kEventName);
304 this, bookmarks::OnRemoved::kEventName); 306 event_router->RegisterObserver(this, bookmarks::OnChanged::kEventName);
305 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( 307 event_router->RegisterObserver(this, bookmarks::OnMoved::kEventName);
306 this, bookmarks::OnChanged::kEventName); 308 event_router->RegisterObserver(this,
307 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( 309 bookmarks::OnChildrenReordered::kEventName);
308 this, bookmarks::OnMoved::kEventName); 310 event_router->RegisterObserver(this, bookmarks::OnImportBegan::kEventName);
309 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( 311 event_router->RegisterObserver(this, bookmarks::OnImportEnded::kEventName);
310 this, bookmarks::OnChildrenReordered::kEventName);
311 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
312 this, bookmarks::OnImportBegan::kEventName);
313 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
314 this, bookmarks::OnImportEnded::kEventName);
315 } 312 }
316 313
317 BookmarksAPI::~BookmarksAPI() { 314 BookmarksAPI::~BookmarksAPI() {
318 } 315 }
319 316
320 void BookmarksAPI::Shutdown() { 317 void BookmarksAPI::Shutdown() {
321 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); 318 ExtensionSystem::Get(context_)->event_router()->UnregisterObserver(this);
322 } 319 }
323 320
324 static base::LazyInstance<ProfileKeyedAPIFactory<BookmarksAPI> > 321 static base::LazyInstance<ProfileKeyedAPIFactory<BookmarksAPI> >
325 g_factory = LAZY_INSTANCE_INITIALIZER; 322 g_factory = LAZY_INSTANCE_INITIALIZER;
326 323
327 // static 324 // static
328 ProfileKeyedAPIFactory<BookmarksAPI>* BookmarksAPI::GetFactoryInstance() { 325 ProfileKeyedAPIFactory<BookmarksAPI>* BookmarksAPI::GetFactoryInstance() {
329 return g_factory.Pointer(); 326 return g_factory.Pointer();
330 } 327 }
331 328
332 void BookmarksAPI::OnListenerAdded(const EventListenerInfo& details) { 329 void BookmarksAPI::OnListenerAdded(const EventListenerInfo& details) {
333 bookmark_event_router_.reset(new BookmarkEventRouter(profile_, 330 bookmark_event_router_.reset(
334 BookmarkModelFactory::GetForProfile(profile_))); 331 new BookmarkEventRouter(context_,
335 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); 332 BookmarkModelFactory::GetForProfile(
333 Profile::FromBrowserContext(context_))));
334 ExtensionSystem::Get(context_)->event_router()->UnregisterObserver(this);
336 } 335 }
337 336
338 bool BookmarksGetFunction::RunImpl() { 337 bool BookmarksGetFunction::RunImpl() {
339 scoped_ptr<bookmarks::Get::Params> params( 338 scoped_ptr<bookmarks::Get::Params> params(
340 bookmarks::Get::Params::Create(*args_)); 339 bookmarks::Get::Params::Create(*args_));
341 EXTENSION_FUNCTION_VALIDATE(params.get()); 340 EXTENSION_FUNCTION_VALIDATE(params.get());
342 341
343 std::vector<linked_ptr<BookmarkTreeNode> > nodes; 342 std::vector<linked_ptr<BookmarkTreeNode> > nodes;
344 if (params->id_or_id_list.as_strings) { 343 if (params->id_or_id_list.as_strings) {
345 std::vector<std::string>& ids = *params->id_or_id_list.as_strings; 344 std::vector<std::string>& ids = *params->id_or_id_list.as_strings;
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after
725 return b; 724 return b;
726 } 725 }
727 private: 726 private:
728 std::map<BucketIdType, Bucket*> buckets_; 727 std::map<BucketIdType, Bucket*> buckets_;
729 }; 728 };
730 729
731 // Mapper for 'bookmarks.create'. Maps "same input to bookmarks.create" to a 730 // Mapper for 'bookmarks.create'. Maps "same input to bookmarks.create" to a
732 // unique bucket. 731 // unique bucket.
733 class CreateBookmarkBucketMapper : public BookmarkBucketMapper<std::string> { 732 class CreateBookmarkBucketMapper : public BookmarkBucketMapper<std::string> {
734 public: 733 public:
735 explicit CreateBookmarkBucketMapper(Profile* profile) : profile_(profile) {} 734 explicit CreateBookmarkBucketMapper(BrowserContext* context)
735 : context_(context) {}
736 // TODO(tim): This should share code with BookmarksCreateFunction::RunImpl, 736 // TODO(tim): This should share code with BookmarksCreateFunction::RunImpl,
737 // but I can't figure out a good way to do that with all the macros. 737 // but I can't figure out a good way to do that with all the macros.
738 virtual void GetBucketsForArgs(const base::ListValue* args, 738 virtual void GetBucketsForArgs(const base::ListValue* args,
739 BucketList* buckets) OVERRIDE { 739 BucketList* buckets) OVERRIDE {
740 const base::DictionaryValue* json; 740 const base::DictionaryValue* json;
741 if (!args->GetDictionary(0, &json)) 741 if (!args->GetDictionary(0, &json))
742 return; 742 return;
743 743
744 std::string parent_id; 744 std::string parent_id;
745 if (json->HasKey(keys::kParentIdKey)) { 745 if (json->HasKey(keys::kParentIdKey)) {
746 if (!json->GetString(keys::kParentIdKey, &parent_id)) 746 if (!json->GetString(keys::kParentIdKey, &parent_id))
747 return; 747 return;
748 } 748 }
749 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile_); 749 BookmarkModel* model = BookmarkModelFactory::GetForProfile(
750 Profile::FromBrowserContext(context_));
750 751
751 int64 parent_id_int64; 752 int64 parent_id_int64;
752 base::StringToInt64(parent_id, &parent_id_int64); 753 base::StringToInt64(parent_id, &parent_id_int64);
753 const BookmarkNode* parent = model->GetNodeByID(parent_id_int64); 754 const BookmarkNode* parent = model->GetNodeByID(parent_id_int64);
754 if (!parent) 755 if (!parent)
755 return; 756 return;
756 757
757 std::string bucket_id = base::UTF16ToUTF8(parent->GetTitle()); 758 std::string bucket_id = base::UTF16ToUTF8(parent->GetTitle());
758 std::string title; 759 std::string title;
759 json->GetString(keys::kTitleKey, &title); 760 json->GetString(keys::kTitleKey, &title);
760 std::string url_string; 761 std::string url_string;
761 json->GetString(keys::kUrlKey, &url_string); 762 json->GetString(keys::kUrlKey, &url_string);
762 763
763 bucket_id += title; 764 bucket_id += title;
764 bucket_id += url_string; 765 bucket_id += url_string;
765 // 20 bytes (SHA1 hash length) is very likely less than most of the 766 // 20 bytes (SHA1 hash length) is very likely less than most of the
766 // |bucket_id| strings we construct here, so we hash it to save space. 767 // |bucket_id| strings we construct here, so we hash it to save space.
767 buckets->push_back(GetBucket(base::SHA1HashString(bucket_id))); 768 buckets->push_back(GetBucket(base::SHA1HashString(bucket_id)));
768 } 769 }
769 private: 770 private:
770 Profile* profile_; 771 BrowserContext* context_;
771 }; 772 };
772 773
773 // Mapper for 'bookmarks.remove'. 774 // Mapper for 'bookmarks.remove'.
774 class RemoveBookmarksBucketMapper : public BookmarkBucketMapper<std::string> { 775 class RemoveBookmarksBucketMapper : public BookmarkBucketMapper<std::string> {
775 public: 776 public:
776 explicit RemoveBookmarksBucketMapper(Profile* profile) : profile_(profile) {} 777 explicit RemoveBookmarksBucketMapper(BrowserContext* context)
778 : context_(context) {}
777 virtual void GetBucketsForArgs(const base::ListValue* args, 779 virtual void GetBucketsForArgs(const base::ListValue* args,
778 BucketList* buckets) OVERRIDE { 780 BucketList* buckets) OVERRIDE {
779 typedef std::list<int64> IdList; 781 typedef std::list<int64> IdList;
780 IdList ids; 782 IdList ids;
781 bool invalid_id = false; 783 bool invalid_id = false;
782 if (!BookmarksRemoveFunction::ExtractIds(args, &ids, &invalid_id) || 784 if (!BookmarksRemoveFunction::ExtractIds(args, &ids, &invalid_id) ||
783 invalid_id) { 785 invalid_id) {
784 return; 786 return;
785 } 787 }
786 788
787 for (IdList::iterator it = ids.begin(); it != ids.end(); ++it) { 789 for (IdList::iterator it = ids.begin(); it != ids.end(); ++it) {
788 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile_); 790 BookmarkModel* model = BookmarkModelFactory::GetForProfile(
791 Profile::FromBrowserContext(context_));
789 const BookmarkNode* node = model->GetNodeByID(*it); 792 const BookmarkNode* node = model->GetNodeByID(*it);
790 if (!node || node->is_root()) 793 if (!node || node->is_root())
791 return; 794 return;
792 795
793 std::string bucket_id; 796 std::string bucket_id;
794 bucket_id += base::UTF16ToUTF8(node->parent()->GetTitle()); 797 bucket_id += base::UTF16ToUTF8(node->parent()->GetTitle());
795 bucket_id += base::UTF16ToUTF8(node->GetTitle()); 798 bucket_id += base::UTF16ToUTF8(node->GetTitle());
796 bucket_id += node->url().spec(); 799 bucket_id += node->url().spec();
797 buckets->push_back(GetBucket(base::SHA1HashString(bucket_id))); 800 buckets->push_back(GetBucket(base::SHA1HashString(bucket_id)));
798 } 801 }
799 } 802 }
800 private: 803 private:
801 Profile* profile_; 804 BrowserContext* context_;
802 }; 805 };
803 806
804 // Mapper for any bookmark function accepting bookmark IDs as parameters, where 807 // Mapper for any bookmark function accepting bookmark IDs as parameters, where
805 // a distinct ID corresponds to a single item in terms of quota limiting. This 808 // a distinct ID corresponds to a single item in terms of quota limiting. This
806 // is inappropriate for bookmarks.remove, for example, since repeated removals 809 // is inappropriate for bookmarks.remove, for example, since repeated removals
807 // of the same item will actually have a different ID each time. 810 // of the same item will actually have a different ID each time.
808 template <class FunctionType> 811 template <class FunctionType>
809 class BookmarkIdMapper : public BookmarkBucketMapper<int64> { 812 class BookmarkIdMapper : public BookmarkBucketMapper<int64> {
810 public: 813 public:
811 typedef std::list<int64> IdList; 814 typedef std::list<int64> IdList;
(...skipping 13 matching lines...) Expand all
825 public: 828 public:
826 // For id-based bookmark functions. 829 // For id-based bookmark functions.
827 template <class FunctionType> 830 template <class FunctionType>
828 static void Build(QuotaLimitHeuristics* heuristics) { 831 static void Build(QuotaLimitHeuristics* heuristics) {
829 BuildWithMappers(heuristics, new BookmarkIdMapper<FunctionType>(), 832 BuildWithMappers(heuristics, new BookmarkIdMapper<FunctionType>(),
830 new BookmarkIdMapper<FunctionType>()); 833 new BookmarkIdMapper<FunctionType>());
831 } 834 }
832 835
833 // For bookmarks.create. 836 // For bookmarks.create.
834 static void BuildForCreate(QuotaLimitHeuristics* heuristics, 837 static void BuildForCreate(QuotaLimitHeuristics* heuristics,
835 Profile* profile) { 838 BrowserContext* context) {
836 BuildWithMappers(heuristics, new CreateBookmarkBucketMapper(profile), 839 BuildWithMappers(heuristics,
837 new CreateBookmarkBucketMapper(profile)); 840 new CreateBookmarkBucketMapper(context),
841 new CreateBookmarkBucketMapper(context));
838 } 842 }
839 843
840 // For bookmarks.remove. 844 // For bookmarks.remove.
841 static void BuildForRemove(QuotaLimitHeuristics* heuristics, 845 static void BuildForRemove(QuotaLimitHeuristics* heuristics,
842 Profile* profile) { 846 BrowserContext* context) {
843 BuildWithMappers(heuristics, new RemoveBookmarksBucketMapper(profile), 847 BuildWithMappers(heuristics,
844 new RemoveBookmarksBucketMapper(profile)); 848 new RemoveBookmarksBucketMapper(context),
849 new RemoveBookmarksBucketMapper(context));
845 } 850 }
846 851
847 private: 852 private:
848 static void BuildWithMappers(QuotaLimitHeuristics* heuristics, 853 static void BuildWithMappers(QuotaLimitHeuristics* heuristics,
849 BucketMapper* short_mapper, BucketMapper* long_mapper) { 854 BucketMapper* short_mapper, BucketMapper* long_mapper) {
850 const Config kSustainedLimitConfig = { 855 const Config kSustainedLimitConfig = {
851 // See bookmarks.json for current value. 856 // See bookmarks.json for current value.
852 bookmarks::MAX_SUSTAINED_WRITE_OPERATIONS_PER_MINUTE, 857 bookmarks::MAX_SUSTAINED_WRITE_OPERATIONS_PER_MINUTE,
853 TimeDelta::FromMinutes(1) 858 TimeDelta::FromMinutes(1)
854 }; 859 };
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
1015 #if !defined(OS_ANDROID) 1020 #if !defined(OS_ANDROID)
1016 // Android does not have support for the standard exporter. 1021 // Android does not have support for the standard exporter.
1017 // TODO(jgreenwald): remove ifdef once extensions are no longer built on 1022 // TODO(jgreenwald): remove ifdef once extensions are no longer built on
1018 // Android. 1023 // Android.
1019 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL); 1024 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL);
1020 #endif 1025 #endif
1021 Release(); // Balanced in BookmarksIOFunction::SelectFile() 1026 Release(); // Balanced in BookmarksIOFunction::SelectFile()
1022 } 1027 }
1023 1028
1024 } // namespace extensions 1029 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698