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