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 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_ 6 #define CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/compiler_specific.h" 12 #include "base/task/cancelable_task_tracker.h" 13 #include "chrome/browser/extensions/chrome_extension_function.h" 14 #include "chrome/browser/history/history_notifications.h" 15 #include "chrome/browser/history/history_service.h" 16 #include "chrome/common/extensions/api/history.h" 17 #include "content/public/browser/notification_registrar.h" 18 #include "extensions/browser/browser_context_keyed_api_factory.h" 19 #include "extensions/browser/event_router.h" 20 21 namespace base { 22 class ListValue; 23 } 24 25 namespace extensions { 26 27 // Observes History service and routes the notifications as events to the 28 // extension system. 29 class HistoryEventRouter : public content::NotificationObserver { 30 public: 31 explicit HistoryEventRouter(Profile* profile); 32 virtual ~HistoryEventRouter(); 33 34 private: 35 // content::NotificationObserver::Observe. 36 virtual void Observe(int type, 37 const content::NotificationSource& source, 38 const content::NotificationDetails& details) OVERRIDE; 39 40 void HistoryUrlVisited(Profile* profile, 41 const history::URLVisitedDetails* details); 42 43 void HistoryUrlsRemoved(Profile* profile, 44 const history::URLsDeletedDetails* details); 45 46 void DispatchEvent(Profile* profile, 47 const std::string& event_name, 48 scoped_ptr<base::ListValue> event_args); 49 50 // Used for tracking registrations to history service notifications. 51 content::NotificationRegistrar registrar_; 52 53 DISALLOW_COPY_AND_ASSIGN(HistoryEventRouter); 54 }; 55 56 class HistoryAPI : public BrowserContextKeyedAPI, public EventRouter::Observer { 57 public: 58 explicit HistoryAPI(content::BrowserContext* context); 59 virtual ~HistoryAPI(); 60 61 // KeyedService implementation. 62 virtual void Shutdown() OVERRIDE; 63 64 // BrowserContextKeyedAPI implementation. 65 static BrowserContextKeyedAPIFactory<HistoryAPI>* GetFactoryInstance(); 66 67 // EventRouter::Observer implementation. 68 virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE; 69 70 private: 71 friend class BrowserContextKeyedAPIFactory<HistoryAPI>; 72 73 content::BrowserContext* browser_context_; 74 75 // BrowserContextKeyedAPI implementation. 76 static const char* service_name() { 77 return "HistoryAPI"; 78 } 79 static const bool kServiceIsNULLWhileTesting = true; 80 81 // Created lazily upon OnListenerAdded. 82 scoped_ptr<HistoryEventRouter> history_event_router_; 83 }; 84 85 template <> 86 void BrowserContextKeyedAPIFactory<HistoryAPI>::DeclareFactoryDependencies(); 87 88 // Base class for history function APIs. 89 class HistoryFunction : public ChromeAsyncExtensionFunction { 90 protected: 91 virtual ~HistoryFunction() {} 92 93 bool ValidateUrl(const std::string& url_string, GURL* url); 94 bool VerifyDeleteAllowed(); 95 base::Time GetTime(double ms_from_epoch); 96 }; 97 98 // Base class for history funciton APIs which require async interaction with 99 // chrome services and the extension thread. 100 class HistoryFunctionWithCallback : public HistoryFunction { 101 public: 102 HistoryFunctionWithCallback(); 103 104 protected: 105 virtual ~HistoryFunctionWithCallback(); 106 107 // ExtensionFunction: 108 virtual bool RunAsync() OVERRIDE; 109 110 // Return true if the async call was completed, false otherwise. 111 virtual bool RunAsyncImpl() = 0; 112 113 // Call this method to report the results of the async method to the caller. 114 // This method calls Release(). 115 virtual void SendAsyncResponse(); 116 117 // The task tracker for the HistoryService callbacks. 118 base::CancelableTaskTracker task_tracker_; 119 120 private: 121 // The actual call to SendResponse. This is required since the semantics for 122 // CancelableRequestConsumerT require it to be accessed after the call. 123 void SendResponseToCallback(); 124 }; 125 126 class HistoryGetVisitsFunction : public HistoryFunctionWithCallback { 127 public: 128 DECLARE_EXTENSION_FUNCTION("history.getVisits", HISTORY_GETVISITS) 129 130 protected: 131 virtual ~HistoryGetVisitsFunction() {} 132 133 // HistoryFunctionWithCallback: 134 virtual bool RunAsyncImpl() OVERRIDE; 135 136 // Callback for the history function to provide results. 137 void QueryComplete(bool success, 138 const history::URLRow& url_row, 139 const history::VisitVector& visits); 140 }; 141 142 class HistorySearchFunction : public HistoryFunctionWithCallback { 143 public: 144 DECLARE_EXTENSION_FUNCTION("history.search", HISTORY_SEARCH) 145 146 protected: 147 virtual ~HistorySearchFunction() {} 148 149 // HistoryFunctionWithCallback: 150 virtual bool RunAsyncImpl() OVERRIDE; 151 152 // Callback for the history function to provide results. 153 void SearchComplete(history::QueryResults* results); 154 }; 155 156 class HistoryAddUrlFunction : public HistoryFunction { 157 public: 158 DECLARE_EXTENSION_FUNCTION("history.addUrl", HISTORY_ADDURL) 159 160 protected: 161 virtual ~HistoryAddUrlFunction() {} 162 163 // HistoryFunctionWithCallback: 164 virtual bool RunAsync() OVERRIDE; 165 }; 166 167 class HistoryDeleteAllFunction : public HistoryFunctionWithCallback { 168 public: 169 DECLARE_EXTENSION_FUNCTION("history.deleteAll", HISTORY_DELETEALL) 170 171 protected: 172 virtual ~HistoryDeleteAllFunction() {} 173 174 // HistoryFunctionWithCallback: 175 virtual bool RunAsyncImpl() OVERRIDE; 176 177 // Callback for the history service to acknowledge deletion. 178 void DeleteComplete(); 179 }; 180 181 182 class HistoryDeleteUrlFunction : public HistoryFunction { 183 public: 184 DECLARE_EXTENSION_FUNCTION("history.deleteUrl", HISTORY_DELETEURL) 185 186 protected: 187 virtual ~HistoryDeleteUrlFunction() {} 188 189 // HistoryFunctionWithCallback: 190 virtual bool RunAsync() OVERRIDE; 191 }; 192 193 class HistoryDeleteRangeFunction : public HistoryFunctionWithCallback { 194 public: 195 DECLARE_EXTENSION_FUNCTION("history.deleteRange", HISTORY_DELETERANGE) 196 197 protected: 198 virtual ~HistoryDeleteRangeFunction() {} 199 200 // HistoryFunctionWithCallback: 201 virtual bool RunAsyncImpl() OVERRIDE; 202 203 // Callback for the history service to acknowledge deletion. 204 void DeleteComplete(); 205 }; 206 207 } // namespace extensions 208 209 #endif // CHROME_BROWSER_EXTENSIONS_API_HISTORY_HISTORY_API_H_ 210