OLD | NEW |
(Empty) | |
| 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. |
| 19 class 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. |
| 60 class 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. |
| 71 class 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 SendReposne. This is required since the semantics for |
| 89 // CancelableRequestConsumerT require it to be accessed after the call. |
| 90 void SendResponseToCallback(); |
| 91 |
| 92 friend class NewRunnableMethod; |
| 93 }; |
| 94 |
| 95 class GetVisitsHistoryFunction : public HistoryFunctionWithCallback { |
| 96 public: |
| 97 // Override HistoryFunction. |
| 98 virtual bool RunAsyncImpl(); |
| 99 DECLARE_EXTENSION_FUNCTION_NAME("experimental.history.getVisits"); |
| 100 |
| 101 // Callback for the history function to provide results. |
| 102 void QueryComplete(HistoryService::Handle request_service, |
| 103 bool success, |
| 104 const history::URLRow* url_row, |
| 105 history::VisitVector* visits); |
| 106 }; |
| 107 |
| 108 class SearchHistoryFunction : public HistoryFunctionWithCallback { |
| 109 public: |
| 110 virtual bool RunAsyncImpl(); |
| 111 DECLARE_EXTENSION_FUNCTION_NAME("experimental.history.search"); |
| 112 |
| 113 // Callback for the history function to provide results. |
| 114 void SearchComplete(HistoryService::Handle request_handle, |
| 115 history::QueryResults* results); |
| 116 }; |
| 117 |
| 118 class AddUrlHistoryFunction : public HistoryFunction { |
| 119 public: |
| 120 virtual bool RunImpl(); |
| 121 DECLARE_EXTENSION_FUNCTION_NAME("experimental.history.addUrl"); |
| 122 }; |
| 123 |
| 124 class DeleteAllHistoryFunction : public HistoryFunctionWithCallback { |
| 125 public: |
| 126 virtual bool RunAsyncImpl(); |
| 127 DECLARE_EXTENSION_FUNCTION_NAME("experimental.history.deleteAll"); |
| 128 |
| 129 // Callback for the history service to acknowledge deletion. |
| 130 void DeleteComplete(); |
| 131 }; |
| 132 |
| 133 |
| 134 class DeleteUrlHistoryFunction : public HistoryFunction { |
| 135 public: |
| 136 virtual bool RunImpl(); |
| 137 DECLARE_EXTENSION_FUNCTION_NAME("experimental.history.deleteUrl"); |
| 138 }; |
| 139 |
| 140 class DeleteRangeHistoryFunction : public HistoryFunctionWithCallback { |
| 141 public: |
| 142 virtual bool RunAsyncImpl(); |
| 143 DECLARE_EXTENSION_FUNCTION_NAME("experimental.history.deleteRange"); |
| 144 |
| 145 // Callback for the history service to acknowledge deletion. |
| 146 void DeleteComplete(); |
| 147 }; |
| 148 |
| 149 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_HISTORY_API_H_ |
OLD | NEW |