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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
// Copyright (c) 2012 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_API_HISTORY_HISTORY_API_H_
#define CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_
#include <string>
#include <vector>
#include "base/compiler_specific.h"
#include "base/scoped_observer.h"
#include "base/task/cancelable_task_tracker.h"
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "chrome/browser/history/history_notifications.h"
#include "chrome/browser/history/history_service.h"
#include "chrome/common/extensions/api/history.h"
#include "components/history/core/browser/history_service_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/browser/event_router.h"
namespace base {
class ListValue;
}
namespace extensions {
// Observes History service and routes the notifications as events to the
// extension system.
class HistoryEventRouter : public content::NotificationObserver,
public history::HistoryServiceObserver {
public:
HistoryEventRouter(Profile* profile, HistoryService* history_service);
~HistoryEventRouter() override;
private:
// content::NotificationObserver::Observe.
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override;
// history::HistoryServiceObserver.
void OnURLVisited(HistoryService* history_service,
ui::PageTransition transition,
const history::URLRow& row,
const history::RedirectList& redirects,
base::Time visit_time) override;
void HistoryUrlsRemoved(Profile* profile,
const history::URLsDeletedDetails* details);
void DispatchEvent(Profile* profile,
const std::string& event_name,
scoped_ptr<base::ListValue> event_args);
// Used for tracking registrations to history service notifications.
content::NotificationRegistrar registrar_;
Profile* profile_;
ScopedObserver<HistoryService, HistoryServiceObserver>
history_service_observer_;
DISALLOW_COPY_AND_ASSIGN(HistoryEventRouter);
};
class HistoryAPI : public BrowserContextKeyedAPI, public EventRouter::Observer {
public:
explicit HistoryAPI(content::BrowserContext* context);
~HistoryAPI() override;
// KeyedService implementation.
void Shutdown() override;
// BrowserContextKeyedAPI implementation.
static BrowserContextKeyedAPIFactory<HistoryAPI>* GetFactoryInstance();
// EventRouter::Observer implementation.
void OnListenerAdded(const EventListenerInfo& details) override;
private:
friend class BrowserContextKeyedAPIFactory<HistoryAPI>;
content::BrowserContext* browser_context_;
// BrowserContextKeyedAPI implementation.
static const char* service_name() {
return "HistoryAPI";
}
static const bool kServiceIsNULLWhileTesting = true;
// Created lazily upon OnListenerAdded.
scoped_ptr<HistoryEventRouter> history_event_router_;
};
template <>
void BrowserContextKeyedAPIFactory<HistoryAPI>::DeclareFactoryDependencies();
// Base class for history function APIs.
class HistoryFunction : public ChromeAsyncExtensionFunction {
protected:
~HistoryFunction() override {}
bool ValidateUrl(const std::string& url_string, GURL* url);
bool VerifyDeleteAllowed();
base::Time GetTime(double ms_from_epoch);
};
// Base class for history funciton APIs which require async interaction with
// chrome services and the extension thread.
class HistoryFunctionWithCallback : public HistoryFunction {
public:
HistoryFunctionWithCallback();
protected:
~HistoryFunctionWithCallback() override;
// ExtensionFunction:
bool RunAsync() override;
// 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();
// The task tracker for the HistoryService callbacks.
base::CancelableTaskTracker task_tracker_;
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 HistoryGetVisitsFunction : public HistoryFunctionWithCallback {
public:
DECLARE_EXTENSION_FUNCTION("history.getVisits", HISTORY_GETVISITS)
protected:
~HistoryGetVisitsFunction() override {}
// HistoryFunctionWithCallback:
bool RunAsyncImpl() override;
// Callback for the history function to provide results.
void QueryComplete(bool success,
const history::URLRow& url_row,
const history::VisitVector& visits);
};
class HistorySearchFunction : public HistoryFunctionWithCallback {
public:
DECLARE_EXTENSION_FUNCTION("history.search", HISTORY_SEARCH)
protected:
~HistorySearchFunction() override {}
// HistoryFunctionWithCallback:
bool RunAsyncImpl() override;
// Callback for the history function to provide results.
void SearchComplete(history::QueryResults* results);
};
class HistoryAddUrlFunction : public HistoryFunction {
public:
DECLARE_EXTENSION_FUNCTION("history.addUrl", HISTORY_ADDURL)
protected:
~HistoryAddUrlFunction() override {}
// HistoryFunctionWithCallback:
bool RunAsync() override;
};
class HistoryDeleteAllFunction : public HistoryFunctionWithCallback {
public:
DECLARE_EXTENSION_FUNCTION("history.deleteAll", HISTORY_DELETEALL)
protected:
~HistoryDeleteAllFunction() override {}
// HistoryFunctionWithCallback:
bool RunAsyncImpl() override;
// Callback for the history service to acknowledge deletion.
void DeleteComplete();
};
class HistoryDeleteUrlFunction : public HistoryFunction {
public:
DECLARE_EXTENSION_FUNCTION("history.deleteUrl", HISTORY_DELETEURL)
protected:
~HistoryDeleteUrlFunction() override {}
// HistoryFunctionWithCallback:
bool RunAsync() override;
};
class HistoryDeleteRangeFunction : public HistoryFunctionWithCallback {
public:
DECLARE_EXTENSION_FUNCTION("history.deleteRange", HISTORY_DELETERANGE)
protected:
~HistoryDeleteRangeFunction() override {}
// HistoryFunctionWithCallback:
bool RunAsyncImpl() override;
// Callback for the history service to acknowledge deletion.
void DeleteComplete();
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_
|