/chrome/browser/extensions/api/history/history_api.h

http://github.com/chromium/chromium · C Header · 199 lines · 124 code · 50 blank · 25 comment · 0 complexity · e183617e0f3566a11ea544ca4ceee019 MD5 · raw file

  1. // Copyright (c) 2012 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. #ifndef CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_
  5. #define CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/compiler_specific.h"
  9. #include "base/macros.h"
  10. #include "base/scoped_observer.h"
  11. #include "base/task/cancelable_task_tracker.h"
  12. #include "chrome/common/extensions/api/history.h"
  13. #include "components/history/core/browser/history_service.h"
  14. #include "components/history/core/browser/history_service_observer.h"
  15. #include "extensions/browser/browser_context_keyed_api_factory.h"
  16. #include "extensions/browser/event_router.h"
  17. #include "extensions/browser/extension_function.h"
  18. class Profile;
  19. namespace base {
  20. class ListValue;
  21. }
  22. namespace extensions {
  23. // Observes History service and routes the notifications as events to the
  24. // extension system.
  25. class HistoryEventRouter : public history::HistoryServiceObserver {
  26. public:
  27. HistoryEventRouter(Profile* profile,
  28. history::HistoryService* history_service);
  29. ~HistoryEventRouter() override;
  30. private:
  31. // history::HistoryServiceObserver.
  32. void OnURLVisited(history::HistoryService* history_service,
  33. ui::PageTransition transition,
  34. const history::URLRow& row,
  35. const history::RedirectList& redirects,
  36. base::Time visit_time) override;
  37. void OnURLsDeleted(history::HistoryService* history_service,
  38. const history::DeletionInfo& deletion_info) override;
  39. void DispatchEvent(Profile* profile,
  40. events::HistogramValue histogram_value,
  41. const std::string& event_name,
  42. std::unique_ptr<base::ListValue> event_args);
  43. Profile* profile_;
  44. ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
  45. history_service_observer_{this};
  46. DISALLOW_COPY_AND_ASSIGN(HistoryEventRouter);
  47. };
  48. class HistoryAPI : public BrowserContextKeyedAPI, public EventRouter::Observer {
  49. public:
  50. explicit HistoryAPI(content::BrowserContext* context);
  51. ~HistoryAPI() override;
  52. // KeyedService implementation.
  53. void Shutdown() override;
  54. // BrowserContextKeyedAPI implementation.
  55. static BrowserContextKeyedAPIFactory<HistoryAPI>* GetFactoryInstance();
  56. // EventRouter::Observer implementation.
  57. void OnListenerAdded(const EventListenerInfo& details) override;
  58. private:
  59. friend class BrowserContextKeyedAPIFactory<HistoryAPI>;
  60. content::BrowserContext* browser_context_;
  61. // BrowserContextKeyedAPI implementation.
  62. static const char* service_name() {
  63. return "HistoryAPI";
  64. }
  65. static const bool kServiceIsNULLWhileTesting = true;
  66. // Created lazily upon OnListenerAdded.
  67. std::unique_ptr<HistoryEventRouter> history_event_router_;
  68. };
  69. template <>
  70. void BrowserContextKeyedAPIFactory<HistoryAPI>::DeclareFactoryDependencies();
  71. // Base class for history function APIs.
  72. class HistoryFunction : public ExtensionFunction {
  73. protected:
  74. ~HistoryFunction() override {}
  75. bool ValidateUrl(const std::string& url_string,
  76. GURL* url,
  77. std::string* error);
  78. bool VerifyDeleteAllowed(std::string* error);
  79. base::Time GetTime(double ms_from_epoch);
  80. Profile* GetProfile() const;
  81. };
  82. // Base class for history funciton APIs which require async interaction with
  83. // chrome services and the extension thread.
  84. class HistoryFunctionWithCallback : public HistoryFunction {
  85. public:
  86. HistoryFunctionWithCallback();
  87. protected:
  88. ~HistoryFunctionWithCallback() override;
  89. // The task tracker for the HistoryService callbacks.
  90. base::CancelableTaskTracker task_tracker_;
  91. };
  92. class HistoryGetVisitsFunction : public HistoryFunctionWithCallback {
  93. public:
  94. DECLARE_EXTENSION_FUNCTION("history.getVisits", HISTORY_GETVISITS)
  95. protected:
  96. ~HistoryGetVisitsFunction() override {}
  97. // ExtensionFunction:
  98. ResponseAction Run() override;
  99. // Callback for the history function to provide results.
  100. void QueryComplete(history::QueryURLResult result);
  101. };
  102. class HistorySearchFunction : public HistoryFunctionWithCallback {
  103. public:
  104. DECLARE_EXTENSION_FUNCTION("history.search", HISTORY_SEARCH)
  105. protected:
  106. ~HistorySearchFunction() override {}
  107. // ExtensionFunction:
  108. ResponseAction Run() override;
  109. // Callback for the history function to provide results.
  110. void SearchComplete(history::QueryResults results);
  111. };
  112. class HistoryAddUrlFunction : public HistoryFunction {
  113. public:
  114. DECLARE_EXTENSION_FUNCTION("history.addUrl", HISTORY_ADDURL)
  115. protected:
  116. ~HistoryAddUrlFunction() override {}
  117. // ExtensionFunction:
  118. ResponseAction Run() override;
  119. };
  120. class HistoryDeleteAllFunction : public HistoryFunctionWithCallback {
  121. public:
  122. DECLARE_EXTENSION_FUNCTION("history.deleteAll", HISTORY_DELETEALL)
  123. protected:
  124. ~HistoryDeleteAllFunction() override {}
  125. // ExtensionFunction:
  126. ResponseAction Run() override;
  127. // Callback for the history service to acknowledge deletion.
  128. void DeleteComplete();
  129. };
  130. class HistoryDeleteUrlFunction : public HistoryFunction {
  131. public:
  132. DECLARE_EXTENSION_FUNCTION("history.deleteUrl", HISTORY_DELETEURL)
  133. protected:
  134. ~HistoryDeleteUrlFunction() override {}
  135. // ExtensionFunction:
  136. ResponseAction Run() override;
  137. };
  138. class HistoryDeleteRangeFunction : public HistoryFunctionWithCallback {
  139. public:
  140. DECLARE_EXTENSION_FUNCTION("history.deleteRange", HISTORY_DELETERANGE)
  141. protected:
  142. ~HistoryDeleteRangeFunction() override {}
  143. // ExtensionFunction:
  144. ResponseAction Run() override;
  145. // Callback for the history service to acknowledge deletion.
  146. void DeleteComplete();
  147. };
  148. } // namespace extensions
  149. #endif // CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_