OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 #pragma once | |
8 | |
9 #include <string> | |
10 | |
11 #include "base/compiler_specific.h" | |
12 #include "chrome/browser/extensions/extension_function.h" | |
13 #include "chrome/browser/history/history.h" | |
14 #include "chrome/browser/history/history_notifications.h" | |
15 #include "content/public/browser/notification_registrar.h" | |
16 | |
17 // Observes History service and routes the notifications as events to the | |
18 // extension system. | |
19 class ExtensionHistoryEventRouter : public content::NotificationObserver { | |
20 public: | |
21 explicit ExtensionHistoryEventRouter(); | |
22 virtual ~ExtensionHistoryEventRouter(); | |
23 | |
24 void ObserveProfile(Profile* profile); | |
25 | |
26 private: | |
27 // content::NotificationObserver::Observe. | |
28 virtual void Observe(int type, | |
29 const content::NotificationSource& source, | |
30 const content::NotificationDetails& details) OVERRIDE; | |
31 | |
32 void HistoryUrlVisited(Profile* profile, | |
33 const history::URLVisitedDetails* details); | |
34 | |
35 void HistoryUrlsRemoved(Profile* profile, | |
36 const history::URLsDeletedDetails* details); | |
37 | |
38 void DispatchEvent(Profile* profile, | |
39 const char* event_name, | |
40 const std::string& json_args); | |
41 | |
42 // Used for tracking registrations to history service notifications. | |
43 content::NotificationRegistrar registrar_; | |
44 | |
45 DISALLOW_COPY_AND_ASSIGN(ExtensionHistoryEventRouter); | |
46 }; | |
47 | |
48 | |
49 // Base class for history function APIs. | |
50 class HistoryFunction : public AsyncExtensionFunction { | |
51 public: | |
52 virtual void Run() OVERRIDE; | |
53 virtual bool RunImpl() = 0; | |
54 | |
55 bool GetUrlFromValue(base::Value* value, GURL* url); | |
56 bool GetTimeFromValue(base::Value* value, base::Time* time); | |
57 }; | |
58 | |
59 // Base class for history funciton APIs which require async interaction with | |
60 // chrome services and the extension thread. | |
61 class HistoryFunctionWithCallback : public HistoryFunction { | |
62 public: | |
63 HistoryFunctionWithCallback(); | |
64 virtual ~HistoryFunctionWithCallback(); | |
65 | |
66 // Return true if the async call was completed, false otherwise. | |
67 virtual bool RunAsyncImpl() = 0; | |
68 | |
69 // Call this method to report the results of the async method to the caller. | |
70 // This method calls Release(). | |
71 virtual void SendAsyncResponse(); | |
72 | |
73 // Override HistoryFunction::RunImpl. | |
74 virtual bool RunImpl() OVERRIDE; | |
75 | |
76 protected: | |
77 // The consumer for the HistoryService callbacks. | |
78 CancelableRequestConsumer cancelable_consumer_; | |
79 | |
80 private: | |
81 // The actual call to SendResponse. This is required since the semantics for | |
82 // CancelableRequestConsumerT require it to be accessed after the call. | |
83 void SendResponseToCallback(); | |
84 }; | |
85 | |
86 class GetVisitsHistoryFunction : public HistoryFunctionWithCallback { | |
87 public: | |
88 // Override HistoryFunction. | |
89 virtual bool RunAsyncImpl() OVERRIDE; | |
90 DECLARE_EXTENSION_FUNCTION_NAME("history.getVisits"); | |
91 | |
92 // Callback for the history function to provide results. | |
93 void QueryComplete(HistoryService::Handle request_service, | |
94 bool success, | |
95 const history::URLRow* url_row, | |
96 history::VisitVector* visits); | |
97 }; | |
98 | |
99 class SearchHistoryFunction : public HistoryFunctionWithCallback { | |
100 public: | |
101 virtual bool RunAsyncImpl() OVERRIDE; | |
102 DECLARE_EXTENSION_FUNCTION_NAME("history.search"); | |
103 | |
104 // Callback for the history function to provide results. | |
105 void SearchComplete(HistoryService::Handle request_handle, | |
106 history::QueryResults* results); | |
107 }; | |
108 | |
109 class AddUrlHistoryFunction : public HistoryFunction { | |
110 public: | |
111 virtual bool RunImpl() OVERRIDE; | |
112 DECLARE_EXTENSION_FUNCTION_NAME("history.addUrl"); | |
113 }; | |
114 | |
115 class DeleteAllHistoryFunction : public HistoryFunctionWithCallback { | |
116 public: | |
117 virtual bool RunAsyncImpl() OVERRIDE; | |
118 DECLARE_EXTENSION_FUNCTION_NAME("history.deleteAll"); | |
119 | |
120 // Callback for the history service to acknowledge deletion. | |
121 void DeleteComplete(); | |
122 }; | |
123 | |
124 | |
125 class DeleteUrlHistoryFunction : public HistoryFunction { | |
126 public: | |
127 virtual bool RunImpl() OVERRIDE; | |
128 DECLARE_EXTENSION_FUNCTION_NAME("history.deleteUrl"); | |
129 }; | |
130 | |
131 class DeleteRangeHistoryFunction : public HistoryFunctionWithCallback { | |
132 public: | |
133 virtual bool RunAsyncImpl() OVERRIDE; | |
134 DECLARE_EXTENSION_FUNCTION_NAME("history.deleteRange"); | |
135 | |
136 // Callback for the history service to acknowledge deletion. | |
137 void DeleteComplete(); | |
138 }; | |
139 | |
140 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_HISTORY_API_H_ | |
OLD | NEW |