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 #ifndef CHROME_BROWSER_HISTORY_HISTORY_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_HISTORY_HISTORY_SERVICE_H_ |
6 #define CHROME_BROWSER_HISTORY_HISTORY_SERVICE_H_ | 6 #define CHROME_BROWSER_HISTORY_HISTORY_SERVICE_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
19 #include "base/observer_list.h" | 19 #include "base/observer_list.h" |
20 #include "base/strings/string16.h" | 20 #include "base/strings/string16.h" |
21 #include "base/task/cancelable_task_tracker.h" | 21 #include "base/task/cancelable_task_tracker.h" |
22 #include "base/threading/thread_checker.h" | 22 #include "base/threading/thread_checker.h" |
23 #include "base/time/time.h" | 23 #include "base/time/time.h" |
24 #include "chrome/browser/common/cancelable_request.h" | |
25 #include "chrome/browser/history/delete_directive_handler.h" | 24 #include "chrome/browser/history/delete_directive_handler.h" |
26 #include "chrome/browser/history/typed_url_syncable_service.h" | 25 #include "chrome/browser/history/typed_url_syncable_service.h" |
27 #include "chrome/common/ref_counted_util.h" | 26 #include "chrome/common/ref_counted_util.h" |
28 #include "components/favicon_base/favicon_callback.h" | 27 #include "components/favicon_base/favicon_callback.h" |
29 #include "components/history/core/browser/history_client.h" | 28 #include "components/history/core/browser/history_client.h" |
30 #include "components/history/core/browser/keyword_id.h" | 29 #include "components/history/core/browser/keyword_id.h" |
31 #include "components/keyed_service/core/keyed_service.h" | 30 #include "components/keyed_service/core/keyed_service.h" |
32 #include "components/visitedlink/browser/visitedlink_delegate.h" | 31 #include "components/visitedlink/browser/visitedlink_delegate.h" |
33 #include "content/public/browser/download_manager_delegate.h" | 32 #include "content/public/browser/download_manager_delegate.h" |
34 #include "content/public/browser/notification_observer.h" | 33 #include "content/public/browser/notification_observer.h" |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 struct HistoryDetails; | 73 struct HistoryDetails; |
75 struct KeywordSearchTermVisit; | 74 struct KeywordSearchTermVisit; |
76 | 75 |
77 } // namespace history | 76 } // namespace history |
78 | 77 |
79 // The history service records page titles, and visit times, as well as | 78 // The history service records page titles, and visit times, as well as |
80 // (eventually) information about autocomplete. | 79 // (eventually) information about autocomplete. |
81 // | 80 // |
82 // This service is thread safe. Each request callback is invoked in the | 81 // This service is thread safe. Each request callback is invoked in the |
83 // thread that made the request. | 82 // thread that made the request. |
84 class HistoryService : public CancelableRequestProvider, | 83 class HistoryService : public content::NotificationObserver, |
85 public content::NotificationObserver, | |
86 public syncer::SyncableService, | 84 public syncer::SyncableService, |
87 public KeyedService, | 85 public KeyedService, |
88 public visitedlink::VisitedLinkDelegate { | 86 public visitedlink::VisitedLinkDelegate { |
89 public: | 87 public: |
90 // Miscellaneous commonly-used types. | 88 // Miscellaneous commonly-used types. |
91 typedef std::vector<PageUsageData*> PageUsageDataList; | 89 typedef std::vector<PageUsageData*> PageUsageDataList; |
92 | 90 |
93 // Must call Init after construction. The |history::HistoryClient| object | 91 // Must call Init after construction. The |history::HistoryClient| object |
94 // must be valid for the whole lifetime of |HistoryService|. | 92 // must be valid for the whole lifetime of |HistoryService|. |
95 explicit HistoryService(history::HistoryClient* client, Profile* profile); | 93 explicit HistoryService(history::HistoryClient* client, Profile* profile); |
(...skipping 635 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 void SetInMemoryBackend( | 729 void SetInMemoryBackend( |
732 scoped_ptr<history::InMemoryHistoryBackend> mem_backend); | 730 scoped_ptr<history::InMemoryHistoryBackend> mem_backend); |
733 | 731 |
734 // Called by our BackendDelegate when there is a problem reading the database. | 732 // Called by our BackendDelegate when there is a problem reading the database. |
735 void NotifyProfileError(sql::InitStatus init_status); | 733 void NotifyProfileError(sql::InitStatus init_status); |
736 | 734 |
737 // Call to schedule a given task for running on the history thread with the | 735 // Call to schedule a given task for running on the history thread with the |
738 // specified priority. The task will have ownership taken. | 736 // specified priority. The task will have ownership taken. |
739 void ScheduleTask(SchedulePriority priority, const base::Closure& task); | 737 void ScheduleTask(SchedulePriority priority, const base::Closure& task); |
740 | 738 |
741 // Schedule ------------------------------------------------------------------ | |
742 // | |
743 // Functions for scheduling operations on the history thread that have a | |
744 // handle and may be cancelable. For fire-and-forget operations, see | |
745 // ScheduleAndForget below. | |
746 | |
747 template<typename BackendFunc, class RequestType> | |
748 Handle Schedule(SchedulePriority priority, | |
749 BackendFunc func, // Function to call on the HistoryBackend. | |
750 CancelableRequestConsumerBase* consumer, | |
751 RequestType* request) { | |
752 DCHECK(thread_) << "History service being called after cleanup"; | |
753 DCHECK(thread_checker_.CalledOnValidThread()); | |
754 if (consumer) | |
755 AddRequest(request, consumer); | |
756 ScheduleTask(priority, | |
757 base::Bind(func, history_backend_.get(), | |
758 scoped_refptr<RequestType>(request))); | |
759 return request->handle(); | |
760 } | |
761 | |
762 template<typename BackendFunc, class RequestType, typename ArgA> | |
763 Handle Schedule(SchedulePriority priority, | |
764 BackendFunc func, // Function to call on the HistoryBackend. | |
765 CancelableRequestConsumerBase* consumer, | |
766 RequestType* request, | |
767 const ArgA& a) { | |
768 DCHECK(thread_) << "History service being called after cleanup"; | |
769 DCHECK(thread_checker_.CalledOnValidThread()); | |
770 if (consumer) | |
771 AddRequest(request, consumer); | |
772 ScheduleTask(priority, | |
773 base::Bind(func, history_backend_.get(), | |
774 scoped_refptr<RequestType>(request), a)); | |
775 return request->handle(); | |
776 } | |
777 | |
778 template<typename BackendFunc, | |
779 class RequestType, // Descendant of CancelableRequestBase. | |
780 typename ArgA, | |
781 typename ArgB> | |
782 Handle Schedule(SchedulePriority priority, | |
783 BackendFunc func, // Function to call on the HistoryBackend. | |
784 CancelableRequestConsumerBase* consumer, | |
785 RequestType* request, | |
786 const ArgA& a, | |
787 const ArgB& b) { | |
788 DCHECK(thread_) << "History service being called after cleanup"; | |
789 DCHECK(thread_checker_.CalledOnValidThread()); | |
790 if (consumer) | |
791 AddRequest(request, consumer); | |
792 ScheduleTask(priority, | |
793 base::Bind(func, history_backend_.get(), | |
794 scoped_refptr<RequestType>(request), a, b)); | |
795 return request->handle(); | |
796 } | |
797 | |
798 template<typename BackendFunc, | |
799 class RequestType, // Descendant of CancelableRequestBase. | |
800 typename ArgA, | |
801 typename ArgB, | |
802 typename ArgC> | |
803 Handle Schedule(SchedulePriority priority, | |
804 BackendFunc func, // Function to call on the HistoryBackend. | |
805 CancelableRequestConsumerBase* consumer, | |
806 RequestType* request, | |
807 const ArgA& a, | |
808 const ArgB& b, | |
809 const ArgC& c) { | |
810 DCHECK(thread_) << "History service being called after cleanup"; | |
811 DCHECK(thread_checker_.CalledOnValidThread()); | |
812 if (consumer) | |
813 AddRequest(request, consumer); | |
814 ScheduleTask(priority, | |
815 base::Bind(func, history_backend_.get(), | |
816 scoped_refptr<RequestType>(request), a, b, c)); | |
817 return request->handle(); | |
818 } | |
819 | |
820 template<typename BackendFunc, | |
821 class RequestType, // Descendant of CancelableRequestBase. | |
822 typename ArgA, | |
823 typename ArgB, | |
824 typename ArgC, | |
825 typename ArgD> | |
826 Handle Schedule(SchedulePriority priority, | |
827 BackendFunc func, // Function to call on the HistoryBackend. | |
828 CancelableRequestConsumerBase* consumer, | |
829 RequestType* request, | |
830 const ArgA& a, | |
831 const ArgB& b, | |
832 const ArgC& c, | |
833 const ArgD& d) { | |
834 DCHECK(thread_) << "History service being called after cleanup"; | |
835 DCHECK(thread_checker_.CalledOnValidThread()); | |
836 if (consumer) | |
837 AddRequest(request, consumer); | |
838 ScheduleTask(priority, | |
839 base::Bind(func, history_backend_.get(), | |
840 scoped_refptr<RequestType>(request), a, b, c, d)); | |
841 return request->handle(); | |
842 } | |
843 | |
844 // ScheduleAndForget --------------------------------------------------------- | 739 // ScheduleAndForget --------------------------------------------------------- |
845 // | 740 // |
846 // Functions for scheduling operations on the history thread that do not need | 741 // Functions for scheduling operations on the history thread that do not need |
847 // any callbacks and are not cancelable. | 742 // any callbacks and are not cancelable. |
848 | 743 |
849 template<typename BackendFunc> | 744 template<typename BackendFunc> |
850 void ScheduleAndForget(SchedulePriority priority, | 745 void ScheduleAndForget(SchedulePriority priority, |
851 BackendFunc func) { // Function to call on backend. | 746 BackendFunc func) { // Function to call on backend. |
852 DCHECK(thread_) << "History service being called after cleanup"; | 747 DCHECK(thread_) << "History service being called after cleanup"; |
853 DCHECK(thread_checker_.CalledOnValidThread()); | 748 DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
920 a, b, c, d, e)); | 815 a, b, c, d, e)); |
921 } | 816 } |
922 | 817 |
923 // All vended weak pointers are invalidated in Cleanup(). | 818 // All vended weak pointers are invalidated in Cleanup(). |
924 base::WeakPtrFactory<HistoryService> weak_ptr_factory_; | 819 base::WeakPtrFactory<HistoryService> weak_ptr_factory_; |
925 | 820 |
926 base::ThreadChecker thread_checker_; | 821 base::ThreadChecker thread_checker_; |
927 | 822 |
928 content::NotificationRegistrar registrar_; | 823 content::NotificationRegistrar registrar_; |
929 | 824 |
930 // Some void primitives require some internal processing in the main thread | |
931 // when done. We use this internal consumer for this purpose. | |
932 CancelableRequestConsumer internal_consumer_; | |
933 | |
934 // The thread used by the history service to run complicated operations. | 825 // The thread used by the history service to run complicated operations. |
935 // |thread_| is NULL once |Cleanup| is NULL. | 826 // |thread_| is NULL once |Cleanup| is NULL. |
936 base::Thread* thread_; | 827 base::Thread* thread_; |
937 | 828 |
938 // This class has most of the implementation and runs on the 'thread_'. | 829 // This class has most of the implementation and runs on the 'thread_'. |
939 // You MUST communicate with this class ONLY through the thread_'s | 830 // You MUST communicate with this class ONLY through the thread_'s |
940 // message_loop(). | 831 // message_loop(). |
941 // | 832 // |
942 // This pointer will be NULL once Cleanup() has been called, meaning no | 833 // This pointer will be NULL once Cleanup() has been called, meaning no |
943 // more calls should be made to the history thread. | 834 // more calls should be made to the history thread. |
(...skipping 30 matching lines...) Expand all Loading... |
974 scoped_ptr<history::InMemoryURLIndex> in_memory_url_index_; | 865 scoped_ptr<history::InMemoryURLIndex> in_memory_url_index_; |
975 | 866 |
976 ObserverList<history::VisitDatabaseObserver> visit_database_observers_; | 867 ObserverList<history::VisitDatabaseObserver> visit_database_observers_; |
977 | 868 |
978 history::DeleteDirectiveHandler delete_directive_handler_; | 869 history::DeleteDirectiveHandler delete_directive_handler_; |
979 | 870 |
980 DISALLOW_COPY_AND_ASSIGN(HistoryService); | 871 DISALLOW_COPY_AND_ASSIGN(HistoryService); |
981 }; | 872 }; |
982 | 873 |
983 #endif // CHROME_BROWSER_HISTORY_HISTORY_SERVICE_H_ | 874 #endif // CHROME_BROWSER_HISTORY_HISTORY_SERVICE_H_ |
OLD | NEW |