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