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/task/cancelable_task_tracker.h"
13 #include "chrome/browser/extensions/chrome_extension_function.h"
14 #include "chrome/browser/history/history_notifications.h"
15 #include "chrome/browser/history/history_service.h"
16 #include "chrome/common/extensions/api/history.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "extensions/browser/browser_context_keyed_api_factory.h"
19 #include "extensions/browser/event_router.h"
25 namespace extensions {
27 // Observes History service and routes the notifications as events to the
29 class HistoryEventRouter : public content::NotificationObserver {
31 explicit HistoryEventRouter(Profile* profile);
32 virtual ~HistoryEventRouter();
35 // content::NotificationObserver::Observe.
36 virtual void Observe(int type,
37 const content::NotificationSource& source,
38 const content::NotificationDetails& details) OVERRIDE;
40 void HistoryUrlVisited(Profile* profile,
41 const history::URLVisitedDetails* details);
43 void HistoryUrlsRemoved(Profile* profile,
44 const history::URLsDeletedDetails* details);
46 void DispatchEvent(Profile* profile,
47 const std::string& event_name,
48 scoped_ptr<base::ListValue> event_args);
50 // Used for tracking registrations to history service notifications.
51 content::NotificationRegistrar registrar_;
53 DISALLOW_COPY_AND_ASSIGN(HistoryEventRouter);
56 class HistoryAPI : public BrowserContextKeyedAPI, public EventRouter::Observer {
58 explicit HistoryAPI(content::BrowserContext* context);
59 virtual ~HistoryAPI();
61 // KeyedService implementation.
62 virtual void Shutdown() OVERRIDE;
64 // BrowserContextKeyedAPI implementation.
65 static BrowserContextKeyedAPIFactory<HistoryAPI>* GetFactoryInstance();
67 // EventRouter::Observer implementation.
68 virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
71 friend class BrowserContextKeyedAPIFactory<HistoryAPI>;
73 content::BrowserContext* browser_context_;
75 // BrowserContextKeyedAPI implementation.
76 static const char* service_name() {
79 static const bool kServiceIsNULLWhileTesting = true;
81 // Created lazily upon OnListenerAdded.
82 scoped_ptr<HistoryEventRouter> history_event_router_;
86 void BrowserContextKeyedAPIFactory<HistoryAPI>::DeclareFactoryDependencies();
88 // Base class for history function APIs.
89 class HistoryFunction : public ChromeAsyncExtensionFunction {
91 virtual ~HistoryFunction() {}
93 bool ValidateUrl(const std::string& url_string, GURL* url);
94 bool VerifyDeleteAllowed();
95 base::Time GetTime(double ms_from_epoch);
98 // Base class for history funciton APIs which require async interaction with
99 // chrome services and the extension thread.
100 class HistoryFunctionWithCallback : public HistoryFunction {
102 HistoryFunctionWithCallback();
105 virtual ~HistoryFunctionWithCallback();
107 // ExtensionFunction:
108 virtual bool RunAsync() OVERRIDE;
110 // Return true if the async call was completed, false otherwise.
111 virtual bool RunAsyncImpl() = 0;
113 // Call this method to report the results of the async method to the caller.
114 // This method calls Release().
115 virtual void SendAsyncResponse();
117 // The consumer for the HistoryService callbacks.
118 CancelableRequestConsumer cancelable_consumer_;
119 base::CancelableTaskTracker task_tracker_;
122 // The actual call to SendResponse. This is required since the semantics for
123 // CancelableRequestConsumerT require it to be accessed after the call.
124 void SendResponseToCallback();
127 class HistoryGetVisitsFunction : public HistoryFunctionWithCallback {
129 DECLARE_EXTENSION_FUNCTION("history.getVisits", HISTORY_GETVISITS)
132 virtual ~HistoryGetVisitsFunction() {}
134 // HistoryFunctionWithCallback:
135 virtual bool RunAsyncImpl() OVERRIDE;
137 // Callback for the history function to provide results.
138 void QueryComplete(HistoryService::Handle request_service,
140 const history::URLRow* url_row,
141 history::VisitVector* visits);
144 class HistorySearchFunction : public HistoryFunctionWithCallback {
146 DECLARE_EXTENSION_FUNCTION("history.search", HISTORY_SEARCH)
149 virtual ~HistorySearchFunction() {}
151 // HistoryFunctionWithCallback:
152 virtual bool RunAsyncImpl() OVERRIDE;
154 // Callback for the history function to provide results.
155 void SearchComplete(HistoryService::Handle request_handle,
156 history::QueryResults* results);
159 class HistoryAddUrlFunction : public HistoryFunction {
161 DECLARE_EXTENSION_FUNCTION("history.addUrl", HISTORY_ADDURL)
164 virtual ~HistoryAddUrlFunction() {}
166 // HistoryFunctionWithCallback:
167 virtual bool RunAsync() OVERRIDE;
170 class HistoryDeleteAllFunction : public HistoryFunctionWithCallback {
172 DECLARE_EXTENSION_FUNCTION("history.deleteAll", HISTORY_DELETEALL)
175 virtual ~HistoryDeleteAllFunction() {}
177 // HistoryFunctionWithCallback:
178 virtual bool RunAsyncImpl() OVERRIDE;
180 // Callback for the history service to acknowledge deletion.
181 void DeleteComplete();
185 class HistoryDeleteUrlFunction : public HistoryFunction {
187 DECLARE_EXTENSION_FUNCTION("history.deleteUrl", HISTORY_DELETEURL)
190 virtual ~HistoryDeleteUrlFunction() {}
192 // HistoryFunctionWithCallback:
193 virtual bool RunAsync() OVERRIDE;
196 class HistoryDeleteRangeFunction : public HistoryFunctionWithCallback {
198 DECLARE_EXTENSION_FUNCTION("history.deleteRange", HISTORY_DELETERANGE)
201 virtual ~HistoryDeleteRangeFunction() {}
203 // HistoryFunctionWithCallback:
204 virtual bool RunAsyncImpl() OVERRIDE;
206 // Callback for the history service to acknowledge deletion.
207 void DeleteComplete();
210 } // namespace extensions
212 #endif // CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_