• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
service_name()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:
~HistoryFunction()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:
~HistoryGetVisitsFunction()132   virtual ~HistoryGetVisitsFunction() {}
133 
134   // HistoryFunctionWithCallback:
135   virtual bool RunAsyncImpl() OVERRIDE;
136 
137   // Callback for the history function to provide results.
138   void QueryComplete(bool success,
139                      const history::URLRow& url_row,
140                      const history::VisitVector& visits);
141 };
142 
143 class HistorySearchFunction : public HistoryFunctionWithCallback {
144  public:
145   DECLARE_EXTENSION_FUNCTION("history.search", HISTORY_SEARCH)
146 
147  protected:
~HistorySearchFunction()148   virtual ~HistorySearchFunction() {}
149 
150   // HistoryFunctionWithCallback:
151   virtual bool RunAsyncImpl() OVERRIDE;
152 
153   // Callback for the history function to provide results.
154   void SearchComplete(HistoryService::Handle request_handle,
155                       history::QueryResults* results);
156 };
157 
158 class HistoryAddUrlFunction : public HistoryFunction {
159  public:
160   DECLARE_EXTENSION_FUNCTION("history.addUrl", HISTORY_ADDURL)
161 
162  protected:
~HistoryAddUrlFunction()163   virtual ~HistoryAddUrlFunction() {}
164 
165   // HistoryFunctionWithCallback:
166   virtual bool RunAsync() OVERRIDE;
167 };
168 
169 class HistoryDeleteAllFunction : public HistoryFunctionWithCallback {
170  public:
171   DECLARE_EXTENSION_FUNCTION("history.deleteAll", HISTORY_DELETEALL)
172 
173  protected:
~HistoryDeleteAllFunction()174   virtual ~HistoryDeleteAllFunction() {}
175 
176   // HistoryFunctionWithCallback:
177   virtual bool RunAsyncImpl() OVERRIDE;
178 
179   // Callback for the history service to acknowledge deletion.
180   void DeleteComplete();
181 };
182 
183 
184 class HistoryDeleteUrlFunction : public HistoryFunction {
185  public:
186   DECLARE_EXTENSION_FUNCTION("history.deleteUrl", HISTORY_DELETEURL)
187 
188  protected:
~HistoryDeleteUrlFunction()189   virtual ~HistoryDeleteUrlFunction() {}
190 
191   // HistoryFunctionWithCallback:
192   virtual bool RunAsync() OVERRIDE;
193 };
194 
195 class HistoryDeleteRangeFunction : public HistoryFunctionWithCallback {
196  public:
197   DECLARE_EXTENSION_FUNCTION("history.deleteRange", HISTORY_DELETERANGE)
198 
199  protected:
~HistoryDeleteRangeFunction()200   virtual ~HistoryDeleteRangeFunction() {}
201 
202   // HistoryFunctionWithCallback:
203   virtual bool RunAsyncImpl() OVERRIDE;
204 
205   // Callback for the history service to acknowledge deletion.
206   void DeleteComplete();
207 };
208 
209 }  // namespace extensions
210 
211 #endif  // CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_
212