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