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/api/profile_keyed_api_factory.h"
14 #include "chrome/browser/extensions/chrome_extension_function.h"
15 #include "chrome/browser/history/history_notifications.h"
16 #include "chrome/browser/history/history_service.h"
17 #include "chrome/common/extensions/api/history.h"
18 #include "content/public/browser/notification_registrar.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 ProfileKeyedAPI,
57 public EventRouter::Observer {
59 explicit HistoryAPI(Profile* profile);
60 virtual ~HistoryAPI();
62 // BrowserContextKeyedService implementation.
63 virtual void Shutdown() OVERRIDE;
65 // ProfileKeyedAPI implementation.
66 static ProfileKeyedAPIFactory<HistoryAPI>* GetFactoryInstance();
68 // EventRouter::Observer implementation.
69 virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
72 friend class ProfileKeyedAPIFactory<HistoryAPI>;
76 // ProfileKeyedAPI implementation.
77 static const char* service_name() {
80 static const bool kServiceIsNULLWhileTesting = true;
82 // Created lazily upon OnListenerAdded.
83 scoped_ptr<HistoryEventRouter> history_event_router_;
87 void ProfileKeyedAPIFactory<HistoryAPI>::DeclareFactoryDependencies();
89 // Base class for history function APIs.
90 class HistoryFunction : public ChromeAsyncExtensionFunction {
92 virtual ~HistoryFunction() {}
93 virtual void Run() OVERRIDE;
95 bool ValidateUrl(const std::string& url_string, GURL* url);
96 bool VerifyDeleteAllowed();
97 base::Time GetTime(double ms_from_epoch);
100 // Base class for history funciton APIs which require async interaction with
101 // chrome services and the extension thread.
102 class HistoryFunctionWithCallback : public HistoryFunction {
104 HistoryFunctionWithCallback();
107 virtual ~HistoryFunctionWithCallback();
109 // ExtensionFunction:
110 virtual bool RunImpl() OVERRIDE;
112 // Return true if the async call was completed, false otherwise.
113 virtual bool RunAsyncImpl() = 0;
115 // Call this method to report the results of the async method to the caller.
116 // This method calls Release().
117 virtual void SendAsyncResponse();
119 // The consumer for the HistoryService callbacks.
120 CancelableRequestConsumer cancelable_consumer_;
121 base::CancelableTaskTracker task_tracker_;
124 // The actual call to SendResponse. This is required since the semantics for
125 // CancelableRequestConsumerT require it to be accessed after the call.
126 void SendResponseToCallback();
129 class HistoryGetVisitsFunction : public HistoryFunctionWithCallback {
131 DECLARE_EXTENSION_FUNCTION("history.getVisits", HISTORY_GETVISITS)
134 virtual ~HistoryGetVisitsFunction() {}
136 // HistoryFunctionWithCallback:
137 virtual bool RunAsyncImpl() OVERRIDE;
139 // Callback for the history function to provide results.
140 void QueryComplete(HistoryService::Handle request_service,
142 const history::URLRow* url_row,
143 history::VisitVector* visits);
146 class HistorySearchFunction : public HistoryFunctionWithCallback {
148 DECLARE_EXTENSION_FUNCTION("history.search", HISTORY_SEARCH)
151 virtual ~HistorySearchFunction() {}
153 // HistoryFunctionWithCallback:
154 virtual bool RunAsyncImpl() OVERRIDE;
156 // Callback for the history function to provide results.
157 void SearchComplete(HistoryService::Handle request_handle,
158 history::QueryResults* results);
161 class HistoryAddUrlFunction : public HistoryFunction {
163 DECLARE_EXTENSION_FUNCTION("history.addUrl", HISTORY_ADDURL)
166 virtual ~HistoryAddUrlFunction() {}
168 // HistoryFunctionWithCallback:
169 virtual bool RunImpl() OVERRIDE;
172 class HistoryDeleteAllFunction : public HistoryFunctionWithCallback {
174 DECLARE_EXTENSION_FUNCTION("history.deleteAll", HISTORY_DELETEALL)
177 virtual ~HistoryDeleteAllFunction() {}
179 // HistoryFunctionWithCallback:
180 virtual bool RunAsyncImpl() OVERRIDE;
182 // Callback for the history service to acknowledge deletion.
183 void DeleteComplete();
187 class HistoryDeleteUrlFunction : public HistoryFunction {
189 DECLARE_EXTENSION_FUNCTION("history.deleteUrl", HISTORY_DELETEURL)
192 virtual ~HistoryDeleteUrlFunction() {}
194 // HistoryFunctionWithCallback:
195 virtual bool RunImpl() OVERRIDE;
198 class HistoryDeleteRangeFunction : public HistoryFunctionWithCallback {
200 DECLARE_EXTENSION_FUNCTION("history.deleteRange", HISTORY_DELETERANGE)
203 virtual ~HistoryDeleteRangeFunction() {}
205 // HistoryFunctionWithCallback:
206 virtual bool RunAsyncImpl() OVERRIDE;
208 // Callback for the history service to acknowledge deletion.
209 void DeleteComplete();
212 } // namespace extensions
214 #endif // CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_