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
|
// Copyright 2012 The Chromium Authors
// 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/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "base/task/cancelable_task_tracker.h"
#include "base/values.h"
#include "chrome/common/extensions/api/history.h"
#include "components/history/core/browser/history_service.h"
#include "components/history/core/browser/history_service_observer.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_function.h"
class Profile;
namespace extensions {
// Observes History service and routes the notifications as events to the
// extension system.
class HistoryEventRouter : public history::HistoryServiceObserver {
public:
HistoryEventRouter(Profile* profile,
history::HistoryService* history_service);
HistoryEventRouter(const HistoryEventRouter&) = delete;
HistoryEventRouter& operator=(const HistoryEventRouter&) = delete;
~HistoryEventRouter() override;
private:
// history::HistoryServiceObserver.
void OnURLVisited(history::HistoryService* history_service,
const history::URLRow& url_row,
const history::VisitRow& new_visit) override;
void OnHistoryDeletions(history::HistoryService* history_service,
const history::DeletionInfo& deletion_info) override;
void DispatchEvent(Profile* profile,
events::HistogramValue histogram_value,
const std::string& event_name,
base::Value::List event_args);
raw_ptr<Profile> profile_;
base::ScopedObservation<history::HistoryService,
history::HistoryServiceObserver>
history_service_observation_{this};
};
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>;
raw_ptr<content::BrowserContext> browser_context_;
// BrowserContextKeyedAPI implementation.
static const char* service_name() {
return "HistoryAPI";
}
static const bool kServiceIsNULLWhileTesting = true;
// Created lazily upon OnListenerAdded.
std::unique_ptr<HistoryEventRouter> history_event_router_;
};
template <>
void BrowserContextKeyedAPIFactory<HistoryAPI>::DeclareFactoryDependencies();
// Base class for history function APIs.
class HistoryFunction : public ExtensionFunction {
protected:
~HistoryFunction() override = default;
bool ValidateUrl(const std::string& url_string,
GURL* url,
std::string* error);
bool VerifyDeleteAllowed(std::string* error);
base::Time GetTime(double ms_from_epoch);
Profile* GetProfile() const;
};
// 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;
// The task tracker for the HistoryService callbacks.
base::CancelableTaskTracker task_tracker_;
};
class HistoryGetVisitsFunction : public HistoryFunctionWithCallback {
public:
DECLARE_EXTENSION_FUNCTION("history.getVisits", HISTORY_GETVISITS)
protected:
~HistoryGetVisitsFunction() override = default;
// ExtensionFunction:
ResponseAction Run() override;
// Callback for the history function to provide results.
void QueryComplete(history::QueryURLResult result);
};
class HistorySearchFunction : public HistoryFunctionWithCallback {
public:
DECLARE_EXTENSION_FUNCTION("history.search", HISTORY_SEARCH)
protected:
~HistorySearchFunction() override = default;
// ExtensionFunction:
ResponseAction Run() 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 = default;
// ExtensionFunction:
ResponseAction Run() override;
};
class HistoryDeleteAllFunction : public HistoryFunctionWithCallback {
public:
DECLARE_EXTENSION_FUNCTION("history.deleteAll", HISTORY_DELETEALL)
protected:
~HistoryDeleteAllFunction() override = default;
// ExtensionFunction:
ResponseAction Run() 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 = default;
// ExtensionFunction:
ResponseAction Run() override;
};
class HistoryDeleteRangeFunction : public HistoryFunctionWithCallback {
public:
DECLARE_EXTENSION_FUNCTION("history.deleteRange", HISTORY_DELETERANGE)
protected:
~HistoryDeleteRangeFunction() override = default;
// ExtensionFunction:
ResponseAction Run() override;
// Callback for the history service to acknowledge deletion.
void DeleteComplete();
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_
|