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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_
11 #include "base/compiler_specific.h"
12 #include "base/memory/linked_ptr.h"
13 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
14 #include "chrome/browser/extensions/chrome_extension_function.h"
15 #include "chrome/browser/extensions/event_router.h"
16 #include "chrome/browser/history/history_notifications.h"
17 #include "chrome/browser/history/history_service.h"
18 #include "chrome/common/cancelable_task_tracker.h"
19 #include "chrome/common/extensions/api/history.h"
20 #include "content/public/browser/notification_registrar.h"
26 namespace extensions {
28 // Observes History service and routes the notifications as events to the
30 class HistoryEventRouter : public content::NotificationObserver {
32 explicit HistoryEventRouter(Profile* profile);
33 virtual ~HistoryEventRouter();
36 // content::NotificationObserver::Observe.
37 virtual void Observe(int type,
38 const content::NotificationSource& source,
39 const content::NotificationDetails& details) OVERRIDE;
41 void HistoryUrlVisited(Profile* profile,
42 const history::URLVisitedDetails* details);
44 void HistoryUrlsRemoved(Profile* profile,
45 const history::URLsDeletedDetails* details);
47 void DispatchEvent(Profile* profile,
48 const std::string& event_name,
49 scoped_ptr<base::ListValue> event_args);
51 // Used for tracking registrations to history service notifications.
52 content::NotificationRegistrar registrar_;
54 DISALLOW_COPY_AND_ASSIGN(HistoryEventRouter);
57 class HistoryAPI : public ProfileKeyedAPI,
58 public EventRouter::Observer {
60 explicit HistoryAPI(Profile* profile);
61 virtual ~HistoryAPI();
63 // BrowserContextKeyedService implementation.
64 virtual void Shutdown() OVERRIDE;
66 // ProfileKeyedAPI implementation.
67 static ProfileKeyedAPIFactory<HistoryAPI>* GetFactoryInstance();
69 // EventRouter::Observer implementation.
70 virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
73 friend class ProfileKeyedAPIFactory<HistoryAPI>;
77 // ProfileKeyedAPI implementation.
78 static const char* service_name() {
81 static const bool kServiceIsNULLWhileTesting = true;
83 // Created lazily upon OnListenerAdded.
84 scoped_ptr<HistoryEventRouter> history_event_router_;
88 void ProfileKeyedAPIFactory<HistoryAPI>::DeclareFactoryDependencies();
90 // Base class for history function APIs.
91 class HistoryFunction : public ChromeAsyncExtensionFunction {
93 virtual ~HistoryFunction() {}
94 virtual void Run() OVERRIDE;
96 bool ValidateUrl(const std::string& url_string, GURL* url);
97 bool VerifyDeleteAllowed();
98 base::Time GetTime(double ms_from_epoch);
101 // Base class for history funciton APIs which require async interaction with
102 // chrome services and the extension thread.
103 class HistoryFunctionWithCallback : public HistoryFunction {
105 HistoryFunctionWithCallback();
108 virtual ~HistoryFunctionWithCallback();
110 // ExtensionFunction:
111 virtual bool RunImpl() OVERRIDE;
113 // Return true if the async call was completed, false otherwise.
114 virtual bool RunAsyncImpl() = 0;
116 // Call this method to report the results of the async method to the caller.
117 // This method calls Release().
118 virtual void SendAsyncResponse();
120 // The consumer for the HistoryService callbacks.
121 CancelableRequestConsumer cancelable_consumer_;
122 CancelableTaskTracker task_tracker_;
125 // The actual call to SendResponse. This is required since the semantics for
126 // CancelableRequestConsumerT require it to be accessed after the call.
127 void SendResponseToCallback();
130 class HistoryGetMostVisitedFunction : public HistoryFunctionWithCallback {
132 DECLARE_EXTENSION_FUNCTION("experimental.history.getMostVisited",
133 EXPERIMENTAL_HISTORY_GETMOSTVISITED)
136 virtual ~HistoryGetMostVisitedFunction() {}
138 // HistoryFunctionWithCallback:
139 virtual bool RunAsyncImpl() OVERRIDE;
141 // Callback for the history function to provide results.
142 void QueryComplete(CancelableRequestProvider::Handle handle,
143 const history::FilteredURLList& data);
146 class HistoryGetVisitsFunction : public HistoryFunctionWithCallback {
148 DECLARE_EXTENSION_FUNCTION("history.getVisits", HISTORY_GETVISITS)
151 virtual ~HistoryGetVisitsFunction() {}
153 // HistoryFunctionWithCallback:
154 virtual bool RunAsyncImpl() OVERRIDE;
156 // Callback for the history function to provide results.
157 void QueryComplete(HistoryService::Handle request_service,
159 const history::URLRow* url_row,
160 history::VisitVector* visits);
163 class HistorySearchFunction : public HistoryFunctionWithCallback {
165 DECLARE_EXTENSION_FUNCTION("history.search", HISTORY_SEARCH)
168 virtual ~HistorySearchFunction() {}
170 // HistoryFunctionWithCallback:
171 virtual bool RunAsyncImpl() OVERRIDE;
173 // Callback for the history function to provide results.
174 void SearchComplete(HistoryService::Handle request_handle,
175 history::QueryResults* results);
178 class HistoryAddUrlFunction : public HistoryFunction {
180 DECLARE_EXTENSION_FUNCTION("history.addUrl", HISTORY_ADDURL)
183 virtual ~HistoryAddUrlFunction() {}
185 // HistoryFunctionWithCallback:
186 virtual bool RunImpl() OVERRIDE;
189 class HistoryDeleteAllFunction : public HistoryFunctionWithCallback {
191 DECLARE_EXTENSION_FUNCTION("history.deleteAll", HISTORY_DELETEALL)
194 virtual ~HistoryDeleteAllFunction() {}
196 // HistoryFunctionWithCallback:
197 virtual bool RunAsyncImpl() OVERRIDE;
199 // Callback for the history service to acknowledge deletion.
200 void DeleteComplete();
204 class HistoryDeleteUrlFunction : public HistoryFunction {
206 DECLARE_EXTENSION_FUNCTION("history.deleteUrl", HISTORY_DELETEURL)
209 virtual ~HistoryDeleteUrlFunction() {}
211 // HistoryFunctionWithCallback:
212 virtual bool RunImpl() OVERRIDE;
215 class HistoryDeleteRangeFunction : public HistoryFunctionWithCallback {
217 DECLARE_EXTENSION_FUNCTION("history.deleteRange", HISTORY_DELETERANGE)
220 virtual ~HistoryDeleteRangeFunction() {}
222 // HistoryFunctionWithCallback:
223 virtual bool RunAsyncImpl() OVERRIDE;
225 // Callback for the history service to acknowledge deletion.
226 void DeleteComplete();
229 } // namespace extensions
231 #endif // CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_