summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_history_api.h
blob: d51c3f44211484a020b01193c834ca5f76774648 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_HISTORY_API_H_
#define CHROME_BROWSER_EXTENSIONS_EXTENSION_HISTORY_API_H_
#pragma once

#include <map>
#include <string>

#include "base/memory/singleton.h"
#include "chrome/browser/extensions/extension_function.h"
#include "chrome/browser/history/history.h"
#include "chrome/browser/history/history_notifications.h"
#include "content/common/notification_registrar.h"

// Observes History service and routes the notifications as events to the
// extension system.
class ExtensionHistoryEventRouter : public NotificationObserver {
 public:
  // Single instance of the event router.
  static ExtensionHistoryEventRouter* GetInstance();

  // Safe to call multiple times.
  void ObserveProfile(Profile* profile);

 private:
  friend struct DefaultSingletonTraits<ExtensionHistoryEventRouter>;

  ExtensionHistoryEventRouter();
  virtual ~ExtensionHistoryEventRouter();

  // NotificationObserver::Observe.
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details);

  void HistoryUrlVisited(Profile* profile,
                         const history::URLVisitedDetails* details);

  void HistoryUrlsRemoved(Profile* profile,
                          const history::URLsDeletedDetails* details);

  void DispatchEvent(Profile* profile,
                     const char* event_name,
                     const std::string& json_args);

  // Used for tracking registrations to history service notifications.
  NotificationRegistrar registrar_;

  // Registered profiles.
  typedef std::map<uintptr_t, Profile*> ProfileMap;
  ProfileMap profiles_;

  DISALLOW_COPY_AND_ASSIGN(ExtensionHistoryEventRouter);
};


// Base class for history function APIs.
class HistoryFunction : public AsyncExtensionFunction {
 public:
  virtual void Run();
  virtual bool RunImpl() = 0;

  bool GetUrlFromValue(Value* value, GURL* url);
  bool GetTimeFromValue(Value* value, base::Time* time);
};

// Base class for history funciton APIs which require async interaction with
// chrome services and the extension thread.
class HistoryFunctionWithCallback : public HistoryFunction {
 public:
  HistoryFunctionWithCallback();
  ~HistoryFunctionWithCallback();

  // Return true if the async call was completed, false otherwise.
  virtual bool RunAsyncImpl() = 0;

  // Call this method to report the results of the async method to the caller.
  // This method calls Release().
  virtual void SendAsyncResponse();

  // Override HistoryFunction::RunImpl.
  virtual bool RunImpl();

 protected:
  // The consumer for the HistoryService callbacks.
  CancelableRequestConsumer cancelable_consumer_;

 private:
  // The actual call to SendResponse.  This is required since the semantics for
  // CancelableRequestConsumerT require it to be accessed after the call.
  void SendResponseToCallback();
};

class GetVisitsHistoryFunction : public HistoryFunctionWithCallback {
 public:
  // Override HistoryFunction.
  virtual bool RunAsyncImpl();
  DECLARE_EXTENSION_FUNCTION_NAME("history.getVisits");

  // Callback for the history function to provide results.
  void QueryComplete(HistoryService::Handle request_service,
                     bool success,
                     const history::URLRow* url_row,
                     history::VisitVector* visits);
};

class SearchHistoryFunction : public HistoryFunctionWithCallback {
 public:
  virtual bool RunAsyncImpl();
  DECLARE_EXTENSION_FUNCTION_NAME("history.search");

  // Callback for the history function to provide results.
  void SearchComplete(HistoryService::Handle request_handle,
                      history::QueryResults* results);
};

class AddUrlHistoryFunction : public HistoryFunction {
 public:
  virtual bool RunImpl();
  DECLARE_EXTENSION_FUNCTION_NAME("history.addUrl");
};

class DeleteAllHistoryFunction : public HistoryFunctionWithCallback {
 public:
  virtual bool RunAsyncImpl();
  DECLARE_EXTENSION_FUNCTION_NAME("history.deleteAll");

  // Callback for the history service to acknowledge deletion.
  void DeleteComplete();
};


class DeleteUrlHistoryFunction : public HistoryFunction {
 public:
  virtual bool RunImpl();
  DECLARE_EXTENSION_FUNCTION_NAME("history.deleteUrl");
};

class DeleteRangeHistoryFunction : public HistoryFunctionWithCallback {
 public:
  virtual bool RunAsyncImpl();
  DECLARE_EXTENSION_FUNCTION_NAME("history.deleteRange");

  // Callback for the history service to acknowledge deletion.
  void DeleteComplete();
};

#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_HISTORY_API_H_