Upstream version 7.36.149.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/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"
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 BrowserContextKeyedAPI, public EventRouter::Observer {
57  public:
58   explicit HistoryAPI(content::BrowserContext* context);
59   virtual ~HistoryAPI();
60
61   // KeyedService implementation.
62   virtual void Shutdown() OVERRIDE;
63
64   // BrowserContextKeyedAPI implementation.
65   static BrowserContextKeyedAPIFactory<HistoryAPI>* GetFactoryInstance();
66
67   // EventRouter::Observer implementation.
68   virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
69
70  private:
71   friend class BrowserContextKeyedAPIFactory<HistoryAPI>;
72
73   content::BrowserContext* browser_context_;
74
75   // BrowserContextKeyedAPI implementation.
76   static const char* service_name() {
77     return "HistoryAPI";
78   }
79   static const bool kServiceIsNULLWhileTesting = true;
80
81   // Created lazily upon OnListenerAdded.
82   scoped_ptr<HistoryEventRouter> history_event_router_;
83 };
84
85 template <>
86 void BrowserContextKeyedAPIFactory<HistoryAPI>::DeclareFactoryDependencies();
87
88 // Base class for history function APIs.
89 class HistoryFunction : public ChromeAsyncExtensionFunction {
90  protected:
91   virtual ~HistoryFunction() {}
92
93   bool ValidateUrl(const std::string& url_string, GURL* url);
94   bool VerifyDeleteAllowed();
95   base::Time GetTime(double ms_from_epoch);
96 };
97
98 // Base class for history funciton APIs which require async interaction with
99 // chrome services and the extension thread.
100 class HistoryFunctionWithCallback : public HistoryFunction {
101  public:
102   HistoryFunctionWithCallback();
103
104  protected:
105   virtual ~HistoryFunctionWithCallback();
106
107   // ExtensionFunction:
108   virtual bool RunAsync() OVERRIDE;
109
110   // Return true if the async call was completed, false otherwise.
111   virtual bool RunAsyncImpl() = 0;
112
113   // Call this method to report the results of the async method to the caller.
114   // This method calls Release().
115   virtual void SendAsyncResponse();
116
117   // The consumer for the HistoryService callbacks.
118   CancelableRequestConsumer cancelable_consumer_;
119   base::CancelableTaskTracker task_tracker_;
120
121  private:
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();
125 };
126
127 class HistoryGetVisitsFunction : public HistoryFunctionWithCallback {
128  public:
129   DECLARE_EXTENSION_FUNCTION("history.getVisits", HISTORY_GETVISITS)
130
131  protected:
132   virtual ~HistoryGetVisitsFunction() {}
133
134   // HistoryFunctionWithCallback:
135   virtual bool RunAsyncImpl() OVERRIDE;
136
137   // Callback for the history function to provide results.
138   void QueryComplete(HistoryService::Handle request_service,
139                      bool success,
140                      const history::URLRow* url_row,
141                      history::VisitVector* visits);
142 };
143
144 class HistorySearchFunction : public HistoryFunctionWithCallback {
145  public:
146   DECLARE_EXTENSION_FUNCTION("history.search", HISTORY_SEARCH)
147
148  protected:
149   virtual ~HistorySearchFunction() {}
150
151   // HistoryFunctionWithCallback:
152   virtual bool RunAsyncImpl() OVERRIDE;
153
154   // Callback for the history function to provide results.
155   void SearchComplete(HistoryService::Handle request_handle,
156                       history::QueryResults* results);
157 };
158
159 class HistoryAddUrlFunction : public HistoryFunction {
160  public:
161   DECLARE_EXTENSION_FUNCTION("history.addUrl", HISTORY_ADDURL)
162
163  protected:
164   virtual ~HistoryAddUrlFunction() {}
165
166   // HistoryFunctionWithCallback:
167   virtual bool RunAsync() OVERRIDE;
168 };
169
170 class HistoryDeleteAllFunction : public HistoryFunctionWithCallback {
171  public:
172   DECLARE_EXTENSION_FUNCTION("history.deleteAll", HISTORY_DELETEALL)
173
174  protected:
175   virtual ~HistoryDeleteAllFunction() {}
176
177   // HistoryFunctionWithCallback:
178   virtual bool RunAsyncImpl() OVERRIDE;
179
180   // Callback for the history service to acknowledge deletion.
181   void DeleteComplete();
182 };
183
184
185 class HistoryDeleteUrlFunction : public HistoryFunction {
186  public:
187   DECLARE_EXTENSION_FUNCTION("history.deleteUrl", HISTORY_DELETEURL)
188
189  protected:
190   virtual ~HistoryDeleteUrlFunction() {}
191
192   // HistoryFunctionWithCallback:
193   virtual bool RunAsync() OVERRIDE;
194 };
195
196 class HistoryDeleteRangeFunction : public HistoryFunctionWithCallback {
197  public:
198   DECLARE_EXTENSION_FUNCTION("history.deleteRange", HISTORY_DELETERANGE)
199
200  protected:
201   virtual ~HistoryDeleteRangeFunction() {}
202
203   // HistoryFunctionWithCallback:
204   virtual bool RunAsyncImpl() OVERRIDE;
205
206   // Callback for the history service to acknowledge deletion.
207   void DeleteComplete();
208 };
209
210 }  // namespace extensions
211
212 #endif  // CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_