- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / history / history_api.h
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.
4
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_
7
8 #include <string>
9 #include <vector>
10
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"
21
22 namespace base {
23 class ListValue;
24 }
25
26 namespace extensions {
27
28 // Observes History service and routes the notifications as events to the
29 // extension system.
30 class HistoryEventRouter : public content::NotificationObserver {
31  public:
32   explicit HistoryEventRouter(Profile* profile);
33   virtual ~HistoryEventRouter();
34
35  private:
36   // content::NotificationObserver::Observe.
37   virtual void Observe(int type,
38                        const content::NotificationSource& source,
39                        const content::NotificationDetails& details) OVERRIDE;
40
41   void HistoryUrlVisited(Profile* profile,
42                          const history::URLVisitedDetails* details);
43
44   void HistoryUrlsRemoved(Profile* profile,
45                           const history::URLsDeletedDetails* details);
46
47   void DispatchEvent(Profile* profile,
48                      const std::string& event_name,
49                      scoped_ptr<base::ListValue> event_args);
50
51   // Used for tracking registrations to history service notifications.
52   content::NotificationRegistrar registrar_;
53
54   DISALLOW_COPY_AND_ASSIGN(HistoryEventRouter);
55 };
56
57 class HistoryAPI : public ProfileKeyedAPI,
58                    public EventRouter::Observer {
59  public:
60   explicit HistoryAPI(Profile* profile);
61   virtual ~HistoryAPI();
62
63   // BrowserContextKeyedService implementation.
64   virtual void Shutdown() OVERRIDE;
65
66   // ProfileKeyedAPI implementation.
67   static ProfileKeyedAPIFactory<HistoryAPI>* GetFactoryInstance();
68
69   // EventRouter::Observer implementation.
70   virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
71
72  private:
73   friend class ProfileKeyedAPIFactory<HistoryAPI>;
74
75   Profile* profile_;
76
77   // ProfileKeyedAPI implementation.
78   static const char* service_name() {
79     return "HistoryAPI";
80   }
81   static const bool kServiceIsNULLWhileTesting = true;
82
83   // Created lazily upon OnListenerAdded.
84   scoped_ptr<HistoryEventRouter> history_event_router_;
85 };
86
87 template<>
88 void ProfileKeyedAPIFactory<HistoryAPI>::DeclareFactoryDependencies();
89
90 // Base class for history function APIs.
91 class HistoryFunction : public ChromeAsyncExtensionFunction {
92  protected:
93   virtual ~HistoryFunction() {}
94   virtual void Run() OVERRIDE;
95
96   bool ValidateUrl(const std::string& url_string, GURL* url);
97   bool VerifyDeleteAllowed();
98   base::Time GetTime(double ms_from_epoch);
99 };
100
101 // Base class for history funciton APIs which require async interaction with
102 // chrome services and the extension thread.
103 class HistoryFunctionWithCallback : public HistoryFunction {
104  public:
105   HistoryFunctionWithCallback();
106
107  protected:
108   virtual ~HistoryFunctionWithCallback();
109
110   // ExtensionFunction:
111   virtual bool RunImpl() OVERRIDE;
112
113   // Return true if the async call was completed, false otherwise.
114   virtual bool RunAsyncImpl() = 0;
115
116   // Call this method to report the results of the async method to the caller.
117   // This method calls Release().
118   virtual void SendAsyncResponse();
119
120   // The consumer for the HistoryService callbacks.
121   CancelableRequestConsumer cancelable_consumer_;
122   CancelableTaskTracker task_tracker_;
123
124  private:
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();
128 };
129
130 class HistoryGetMostVisitedFunction : public HistoryFunctionWithCallback {
131  public:
132   DECLARE_EXTENSION_FUNCTION("experimental.history.getMostVisited",
133                              EXPERIMENTAL_HISTORY_GETMOSTVISITED)
134
135  protected:
136   virtual ~HistoryGetMostVisitedFunction() {}
137
138   // HistoryFunctionWithCallback:
139   virtual bool RunAsyncImpl() OVERRIDE;
140
141   // Callback for the history function to provide results.
142   void QueryComplete(CancelableRequestProvider::Handle handle,
143                      const history::FilteredURLList& data);
144 };
145
146 class HistoryGetVisitsFunction : public HistoryFunctionWithCallback {
147  public:
148   DECLARE_EXTENSION_FUNCTION("history.getVisits", HISTORY_GETVISITS)
149
150  protected:
151   virtual ~HistoryGetVisitsFunction() {}
152
153   // HistoryFunctionWithCallback:
154   virtual bool RunAsyncImpl() OVERRIDE;
155
156   // Callback for the history function to provide results.
157   void QueryComplete(HistoryService::Handle request_service,
158                      bool success,
159                      const history::URLRow* url_row,
160                      history::VisitVector* visits);
161 };
162
163 class HistorySearchFunction : public HistoryFunctionWithCallback {
164  public:
165   DECLARE_EXTENSION_FUNCTION("history.search", HISTORY_SEARCH)
166
167  protected:
168   virtual ~HistorySearchFunction() {}
169
170   // HistoryFunctionWithCallback:
171   virtual bool RunAsyncImpl() OVERRIDE;
172
173   // Callback for the history function to provide results.
174   void SearchComplete(HistoryService::Handle request_handle,
175                       history::QueryResults* results);
176 };
177
178 class HistoryAddUrlFunction : public HistoryFunction {
179  public:
180   DECLARE_EXTENSION_FUNCTION("history.addUrl", HISTORY_ADDURL)
181
182  protected:
183   virtual ~HistoryAddUrlFunction() {}
184
185   // HistoryFunctionWithCallback:
186   virtual bool RunImpl() OVERRIDE;
187 };
188
189 class HistoryDeleteAllFunction : public HistoryFunctionWithCallback {
190  public:
191   DECLARE_EXTENSION_FUNCTION("history.deleteAll", HISTORY_DELETEALL)
192
193  protected:
194   virtual ~HistoryDeleteAllFunction() {}
195
196   // HistoryFunctionWithCallback:
197   virtual bool RunAsyncImpl() OVERRIDE;
198
199   // Callback for the history service to acknowledge deletion.
200   void DeleteComplete();
201 };
202
203
204 class HistoryDeleteUrlFunction : public HistoryFunction {
205  public:
206   DECLARE_EXTENSION_FUNCTION("history.deleteUrl", HISTORY_DELETEURL)
207
208  protected:
209   virtual ~HistoryDeleteUrlFunction() {}
210
211   // HistoryFunctionWithCallback:
212   virtual bool RunImpl() OVERRIDE;
213 };
214
215 class HistoryDeleteRangeFunction : public HistoryFunctionWithCallback {
216  public:
217   DECLARE_EXTENSION_FUNCTION("history.deleteRange", HISTORY_DELETERANGE)
218
219  protected:
220   virtual ~HistoryDeleteRangeFunction() {}
221
222   // HistoryFunctionWithCallback:
223   virtual bool RunAsyncImpl() OVERRIDE;
224
225   // Callback for the history service to acknowledge deletion.
226   void DeleteComplete();
227 };
228
229 }  // namespace extensions
230
231 #endif  // CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_