Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2011 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_EXTENSION_HISTORY_API_H_
      6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_HISTORY_API_H_
      7 #pragma once
      8 
      9 #include <map>
     10 #include <string>
     11 
     12 #include "base/memory/singleton.h"
     13 #include "chrome/browser/extensions/extension_function.h"
     14 #include "chrome/browser/history/history.h"
     15 #include "chrome/browser/history/history_notifications.h"
     16 #include "content/common/notification_registrar.h"
     17 
     18 // Observes History service and routes the notifications as events to the
     19 // extension system.
     20 class ExtensionHistoryEventRouter : public NotificationObserver {
     21  public:
     22   // Single instance of the event router.
     23   static ExtensionHistoryEventRouter* GetInstance();
     24 
     25   // Safe to call multiple times.
     26   void ObserveProfile(Profile* profile);
     27 
     28  private:
     29   friend struct DefaultSingletonTraits<ExtensionHistoryEventRouter>;
     30 
     31   ExtensionHistoryEventRouter();
     32   virtual ~ExtensionHistoryEventRouter();
     33 
     34   // NotificationObserver::Observe.
     35   virtual void Observe(NotificationType type,
     36                        const NotificationSource& source,
     37                        const NotificationDetails& details);
     38 
     39   void HistoryUrlVisited(Profile* profile,
     40                          const history::URLVisitedDetails* details);
     41 
     42   void HistoryUrlsRemoved(Profile* profile,
     43                           const history::URLsDeletedDetails* details);
     44 
     45   void DispatchEvent(Profile* profile,
     46                      const char* event_name,
     47                      const std::string& json_args);
     48 
     49   // Used for tracking registrations to history service notifications.
     50   NotificationRegistrar registrar_;
     51 
     52   // Registered profiles.
     53   typedef std::map<uintptr_t, Profile*> ProfileMap;
     54   ProfileMap profiles_;
     55 
     56   DISALLOW_COPY_AND_ASSIGN(ExtensionHistoryEventRouter);
     57 };
     58 
     59 
     60 // Base class for history function APIs.
     61 class HistoryFunction : public AsyncExtensionFunction {
     62  public:
     63   virtual void Run();
     64   virtual bool RunImpl() = 0;
     65 
     66   bool GetUrlFromValue(Value* value, GURL* url);
     67   bool GetTimeFromValue(Value* value, base::Time* time);
     68 };
     69 
     70 // Base class for history funciton APIs which require async interaction with
     71 // chrome services and the extension thread.
     72 class HistoryFunctionWithCallback : public HistoryFunction {
     73  public:
     74   HistoryFunctionWithCallback();
     75   ~HistoryFunctionWithCallback();
     76 
     77   // Return true if the async call was completed, false otherwise.
     78   virtual bool RunAsyncImpl() = 0;
     79 
     80   // Call this method to report the results of the async method to the caller.
     81   // This method calls Release().
     82   virtual void SendAsyncResponse();
     83 
     84   // Override HistoryFunction::RunImpl.
     85   virtual bool RunImpl();
     86 
     87  protected:
     88   // The consumer for the HistoryService callbacks.
     89   CancelableRequestConsumer cancelable_consumer_;
     90 
     91  private:
     92   // The actual call to SendResponse.  This is required since the semantics for
     93   // CancelableRequestConsumerT require it to be accessed after the call.
     94   void SendResponseToCallback();
     95 };
     96 
     97 class GetVisitsHistoryFunction : public HistoryFunctionWithCallback {
     98  public:
     99   // Override HistoryFunction.
    100   virtual bool RunAsyncImpl();
    101   DECLARE_EXTENSION_FUNCTION_NAME("history.getVisits");
    102 
    103   // Callback for the history function to provide results.
    104   void QueryComplete(HistoryService::Handle request_service,
    105                      bool success,
    106                      const history::URLRow* url_row,
    107                      history::VisitVector* visits);
    108 };
    109 
    110 class SearchHistoryFunction : public HistoryFunctionWithCallback {
    111  public:
    112   virtual bool RunAsyncImpl();
    113   DECLARE_EXTENSION_FUNCTION_NAME("history.search");
    114 
    115   // Callback for the history function to provide results.
    116   void SearchComplete(HistoryService::Handle request_handle,
    117                       history::QueryResults* results);
    118 };
    119 
    120 class AddUrlHistoryFunction : public HistoryFunction {
    121  public:
    122   virtual bool RunImpl();
    123   DECLARE_EXTENSION_FUNCTION_NAME("history.addUrl");
    124 };
    125 
    126 class DeleteAllHistoryFunction : public HistoryFunctionWithCallback {
    127  public:
    128   virtual bool RunAsyncImpl();
    129   DECLARE_EXTENSION_FUNCTION_NAME("history.deleteAll");
    130 
    131   // Callback for the history service to acknowledge deletion.
    132   void DeleteComplete();
    133 };
    134 
    135 
    136 class DeleteUrlHistoryFunction : public HistoryFunction {
    137  public:
    138   virtual bool RunImpl();
    139   DECLARE_EXTENSION_FUNCTION_NAME("history.deleteUrl");
    140 };
    141 
    142 class DeleteRangeHistoryFunction : public HistoryFunctionWithCallback {
    143  public:
    144   virtual bool RunAsyncImpl();
    145   DECLARE_EXTENSION_FUNCTION_NAME("history.deleteRange");
    146 
    147   // Callback for the history service to acknowledge deletion.
    148   void DeleteComplete();
    149 };
    150 
    151 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_HISTORY_API_H_
    152