Home | History | Annotate | Download | only in history
      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/event_router.h"
     15 #include "chrome/browser/extensions/extension_function.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 char* 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 // Base class for history function APIs.
     88 class HistoryFunction : public AsyncExtensionFunction {
     89  protected:
     90   virtual ~HistoryFunction() {}
     91   virtual void Run() OVERRIDE;
     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 RunImpl() 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   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 HistoryGetMostVisitedFunction : public HistoryFunctionWithCallback {
    128  public:
    129   DECLARE_EXTENSION_FUNCTION("experimental.history.getMostVisited",
    130                              EXPERIMENTAL_HISTORY_GETMOSTVISITED)
    131 
    132  protected:
    133   virtual ~HistoryGetMostVisitedFunction() {}
    134 
    135   // HistoryFunctionWithCallback:
    136   virtual bool RunAsyncImpl() OVERRIDE;
    137 
    138   // Callback for the history function to provide results.
    139   void QueryComplete(CancelableRequestProvider::Handle handle,
    140                      const history::FilteredURLList& data);
    141 };
    142 
    143 class HistoryGetVisitsFunction : public HistoryFunctionWithCallback {
    144  public:
    145   DECLARE_EXTENSION_FUNCTION("history.getVisits", HISTORY_GETVISITS)
    146 
    147  protected:
    148   virtual ~HistoryGetVisitsFunction() {}
    149 
    150   // HistoryFunctionWithCallback:
    151   virtual bool RunAsyncImpl() OVERRIDE;
    152 
    153   // Callback for the history function to provide results.
    154   void QueryComplete(HistoryService::Handle request_service,
    155                      bool success,
    156                      const history::URLRow* url_row,
    157                      history::VisitVector* visits);
    158 };
    159 
    160 class HistorySearchFunction : public HistoryFunctionWithCallback {
    161  public:
    162   DECLARE_EXTENSION_FUNCTION("history.search", HISTORY_SEARCH)
    163 
    164  protected:
    165   virtual ~HistorySearchFunction() {}
    166 
    167   // HistoryFunctionWithCallback:
    168   virtual bool RunAsyncImpl() OVERRIDE;
    169 
    170   // Callback for the history function to provide results.
    171   void SearchComplete(HistoryService::Handle request_handle,
    172                       history::QueryResults* results);
    173 };
    174 
    175 class HistoryAddUrlFunction : public HistoryFunction {
    176  public:
    177   DECLARE_EXTENSION_FUNCTION("history.addUrl", HISTORY_ADDURL)
    178 
    179  protected:
    180   virtual ~HistoryAddUrlFunction() {}
    181 
    182   // HistoryFunctionWithCallback:
    183   virtual bool RunImpl() OVERRIDE;
    184 };
    185 
    186 class HistoryDeleteAllFunction : public HistoryFunctionWithCallback {
    187  public:
    188   DECLARE_EXTENSION_FUNCTION("history.deleteAll", HISTORY_DELETEALL)
    189 
    190  protected:
    191   virtual ~HistoryDeleteAllFunction() {}
    192 
    193   // HistoryFunctionWithCallback:
    194   virtual bool RunAsyncImpl() OVERRIDE;
    195 
    196   // Callback for the history service to acknowledge deletion.
    197   void DeleteComplete();
    198 };
    199 
    200 
    201 class HistoryDeleteUrlFunction : public HistoryFunction {
    202  public:
    203   DECLARE_EXTENSION_FUNCTION("history.deleteUrl", HISTORY_DELETEURL)
    204 
    205  protected:
    206   virtual ~HistoryDeleteUrlFunction() {}
    207 
    208   // HistoryFunctionWithCallback:
    209   virtual bool RunImpl() OVERRIDE;
    210 };
    211 
    212 class HistoryDeleteRangeFunction : public HistoryFunctionWithCallback {
    213  public:
    214   DECLARE_EXTENSION_FUNCTION("history.deleteRange", HISTORY_DELETERANGE)
    215 
    216  protected:
    217   virtual ~HistoryDeleteRangeFunction() {}
    218 
    219   // HistoryFunctionWithCallback:
    220   virtual bool RunAsyncImpl() OVERRIDE;
    221 
    222   // Callback for the history service to acknowledge deletion.
    223   void DeleteComplete();
    224 };
    225 
    226 }  // namespace extensions
    227 
    228 #endif  // CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_
    229