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_BOOKMARKS_MODULE_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_BOOKMARKS_MODULE_H_ | |
7 #pragma once | |
8 | |
9 #include <list> | |
10 #include <set> | |
11 #include <string> | |
12 | |
13 #include "base/compiler_specific.h" | |
14 #include "base/memory/ref_counted.h" | |
15 #include "chrome/browser/bookmarks/bookmark_model_observer.h" | |
16 #include "chrome/browser/extensions/extension_function.h" | |
17 #include "chrome/browser/ui/shell_dialogs.h" | |
18 #include "content/public/browser/notification_observer.h" | |
19 #include "content/public/browser/notification_registrar.h" | |
20 | |
21 class FilePath; | |
22 | |
23 namespace base { | |
24 class ListValue; | |
25 } | |
26 | |
27 // Observes BookmarkModel and then routes the notifications as events to | |
28 // the extension system. | |
29 class ExtensionBookmarkEventRouter : public BookmarkModelObserver { | |
30 public: | |
31 explicit ExtensionBookmarkEventRouter(BookmarkModel* model); | |
32 virtual ~ExtensionBookmarkEventRouter(); | |
33 | |
34 void Init(); | |
35 | |
36 // BookmarkModelObserver: | |
37 virtual void Loaded(BookmarkModel* model, bool ids_reassigned) OVERRIDE; | |
38 virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE; | |
39 virtual void BookmarkNodeMoved(BookmarkModel* model, | |
40 const BookmarkNode* old_parent, | |
41 int old_index, | |
42 const BookmarkNode* new_parent, | |
43 int new_index) OVERRIDE; | |
44 virtual void BookmarkNodeAdded(BookmarkModel* model, | |
45 const BookmarkNode* parent, | |
46 int index) OVERRIDE; | |
47 virtual void BookmarkNodeRemoved(BookmarkModel* model, | |
48 const BookmarkNode* parent, | |
49 int old_index, | |
50 const BookmarkNode* node) OVERRIDE; | |
51 virtual void BookmarkNodeChanged(BookmarkModel* model, | |
52 const BookmarkNode* node) OVERRIDE; | |
53 virtual void BookmarkNodeFaviconChanged(BookmarkModel* model, | |
54 const BookmarkNode* node) OVERRIDE; | |
55 virtual void BookmarkNodeChildrenReordered(BookmarkModel* model, | |
56 const BookmarkNode* node) OVERRIDE; | |
57 virtual void BookmarkImportBeginning(BookmarkModel* model) OVERRIDE; | |
58 virtual void BookmarkImportEnding(BookmarkModel* model) OVERRIDE; | |
59 | |
60 private: | |
61 // Helper to actually dispatch an event to extension listeners. | |
62 void DispatchEvent(Profile* profile, | |
63 const char* event_name, | |
64 const std::string& json_args); | |
65 | |
66 BookmarkModel* model_; | |
67 | |
68 DISALLOW_COPY_AND_ASSIGN(ExtensionBookmarkEventRouter); | |
69 }; | |
70 | |
71 class BookmarksFunction : public AsyncExtensionFunction, | |
72 public content::NotificationObserver { | |
73 public: | |
74 // AsyncExtensionFunction: | |
75 virtual void Run() OVERRIDE; | |
76 | |
77 virtual bool RunImpl() = 0; | |
78 | |
79 protected: | |
80 // Helper to get the bookmark id as int64 from the given string id. | |
81 // Sets error_ to an error string if the given id string can't be parsed | |
82 // as an int64. In case of error, doesn't change id and returns false. | |
83 bool GetBookmarkIdAsInt64(const std::string& id_string, int64* id); | |
84 | |
85 // Helper that checks if bookmark editing is enabled. If it's not, this sets | |
86 // error_ to the appropriate error string. | |
87 bool EditBookmarksEnabled(); | |
88 | |
89 private: | |
90 // content::NotificationObserver: | |
91 virtual void Observe(int type, | |
92 const content::NotificationSource& source, | |
93 const content::NotificationDetails& details) OVERRIDE; | |
94 | |
95 content::NotificationRegistrar registrar_; | |
96 }; | |
97 | |
98 class GetBookmarksFunction : public BookmarksFunction { | |
99 public: | |
100 virtual bool RunImpl() OVERRIDE; | |
101 | |
102 private: | |
103 DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.get") | |
104 }; | |
105 | |
106 class GetBookmarkChildrenFunction : public BookmarksFunction { | |
107 public: | |
108 virtual bool RunImpl() OVERRIDE; | |
109 | |
110 private: | |
111 DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.getChildren") | |
112 }; | |
113 | |
114 class GetBookmarkRecentFunction : public BookmarksFunction { | |
115 public: | |
116 virtual bool RunImpl() OVERRIDE; | |
117 | |
118 private: | |
119 DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.getRecent") | |
120 }; | |
121 | |
122 class GetBookmarkTreeFunction : public BookmarksFunction { | |
123 public: | |
124 virtual bool RunImpl() OVERRIDE; | |
125 | |
126 private: | |
127 DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.getTree") | |
128 }; | |
129 | |
130 class GetBookmarkSubTreeFunction : public BookmarksFunction { | |
131 public: | |
132 virtual bool RunImpl() OVERRIDE; | |
133 | |
134 private: | |
135 DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.getSubTree") | |
136 }; | |
137 | |
138 class SearchBookmarksFunction : public BookmarksFunction { | |
139 public: | |
140 virtual bool RunImpl() OVERRIDE; | |
141 | |
142 private: | |
143 DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.search") | |
144 }; | |
145 | |
146 class RemoveBookmarkFunction : public BookmarksFunction { | |
147 public: | |
148 // Returns true on successful parse and sets invalid_id to true if conversion | |
149 // from id string to int64 failed. | |
150 static bool ExtractIds(const base::ListValue* args, std::list<int64>* ids, | |
151 bool* invalid_id); | |
152 // BookmarksFunction: | |
153 virtual bool RunImpl() OVERRIDE; | |
154 virtual void GetQuotaLimitHeuristics( | |
155 std::list<QuotaLimitHeuristic*>* heuristics) const; | |
156 | |
157 private: | |
158 DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.remove") | |
159 }; | |
160 | |
161 class RemoveTreeBookmarkFunction : public RemoveBookmarkFunction { | |
162 DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.removeTree") | |
163 }; | |
164 | |
165 class CreateBookmarkFunction : public BookmarksFunction { | |
166 public: | |
167 virtual void GetQuotaLimitHeuristics( | |
168 std::list<QuotaLimitHeuristic*>* heuristics) const; | |
169 // BookmarksFunction: | |
170 virtual bool RunImpl() OVERRIDE; | |
171 | |
172 private: | |
173 DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.create") | |
174 }; | |
175 | |
176 class MoveBookmarkFunction : public BookmarksFunction { | |
177 public: | |
178 static bool ExtractIds(const base::ListValue* args, std::list<int64>* ids, | |
179 bool* invalid_id); | |
180 virtual void GetQuotaLimitHeuristics( | |
181 std::list<QuotaLimitHeuristic*>* heuristics) const; | |
182 // BookmarksFunction: | |
183 virtual bool RunImpl() OVERRIDE; | |
184 | |
185 private: | |
186 DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.move") | |
187 }; | |
188 | |
189 class UpdateBookmarkFunction : public BookmarksFunction { | |
190 public: | |
191 static bool ExtractIds(const base::ListValue* args, std::list<int64>* ids, | |
192 bool* invalid_id); | |
193 virtual void GetQuotaLimitHeuristics( | |
194 std::list<QuotaLimitHeuristic*>* heuristics) const; | |
195 virtual bool RunImpl(); | |
196 private: | |
197 DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.update") | |
198 }; | |
199 | |
200 class BookmarksIOFunction : public BookmarksFunction, | |
201 public SelectFileDialog::Listener { | |
202 public: | |
203 BookmarksIOFunction(); | |
204 virtual ~BookmarksIOFunction(); | |
205 | |
206 virtual void FileSelected(const FilePath& path, int index, void* params) = 0; | |
207 | |
208 // SelectFileDialog::Listener: | |
209 virtual void MultiFilesSelected(const std::vector<FilePath>& files, | |
210 void* params) OVERRIDE; | |
211 virtual void FileSelectionCanceled(void* params) OVERRIDE; | |
212 | |
213 void SelectFile(SelectFileDialog::Type type); | |
214 | |
215 private: | |
216 void ShowSelectFileDialog(SelectFileDialog::Type type, FilePath default_path); | |
217 | |
218 protected: | |
219 scoped_refptr<SelectFileDialog> select_file_dialog_; | |
220 }; | |
221 | |
222 class ImportBookmarksFunction : public BookmarksIOFunction { | |
223 public: | |
224 // BookmarkManagerIOFunction: | |
225 virtual bool RunImpl() OVERRIDE; | |
226 virtual void FileSelected(const FilePath& path, int index, void* params) | |
227 OVERRIDE; | |
228 | |
229 private: | |
230 DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.import"); | |
231 }; | |
232 | |
233 class ExportBookmarksFunction : public BookmarksIOFunction { | |
234 public: | |
235 // BookmarkManagerIOFunction: | |
236 virtual bool RunImpl() OVERRIDE; | |
237 virtual void FileSelected(const FilePath& path, int index, void* params) | |
238 OVERRIDE; | |
239 | |
240 private: | |
241 DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.export"); | |
242 }; | |
243 | |
244 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_BOOKMARKS_MODULE_H_ | |
OLD | NEW |