Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / extensions / api / bookmarks / bookmarks_api.h
blobf25e7eaa74154c2d8629680917c4f78e4b4ce4b8
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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_BOOKMARKS_BOOKMARKS_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_BOOKMARKS_BOOKMARKS_API_H_
8 #include <list>
9 #include <string>
10 #include <vector>
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "chrome/browser/extensions/chrome_extension_function.h"
15 #include "components/bookmarks/browser/base_bookmark_model_observer.h"
16 #include "components/bookmarks/browser/bookmark_node.h"
17 #include "extensions/browser/browser_context_keyed_api_factory.h"
18 #include "extensions/browser/event_router.h"
19 #include "ui/shell_dialogs/select_file_dialog.h"
21 namespace base {
22 class FilePath;
23 class ListValue;
26 namespace bookmarks {
27 class BookmarkModel;
28 class ManagedBookmarkService;
31 namespace content {
32 class BrowserContext;
35 namespace extensions {
37 namespace api {
38 namespace bookmarks {
39 struct CreateDetails;
43 // Observes BookmarkModel and then routes the notifications as events to
44 // the extension system.
45 class BookmarkEventRouter : public bookmarks::BookmarkModelObserver {
46 public:
47 explicit BookmarkEventRouter(Profile* profile);
48 ~BookmarkEventRouter() override;
50 // bookmarks::BookmarkModelObserver:
51 void BookmarkModelLoaded(bookmarks::BookmarkModel* model,
52 bool ids_reassigned) override;
53 void BookmarkModelBeingDeleted(bookmarks::BookmarkModel* model) override;
54 void BookmarkNodeMoved(bookmarks::BookmarkModel* model,
55 const bookmarks::BookmarkNode* old_parent,
56 int old_index,
57 const bookmarks::BookmarkNode* new_parent,
58 int new_index) override;
59 void BookmarkNodeAdded(bookmarks::BookmarkModel* model,
60 const bookmarks::BookmarkNode* parent,
61 int index) override;
62 void BookmarkNodeRemoved(bookmarks::BookmarkModel* model,
63 const bookmarks::BookmarkNode* parent,
64 int old_index,
65 const bookmarks::BookmarkNode* node,
66 const std::set<GURL>& removed_urls) override;
67 void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel* model,
68 const std::set<GURL>& removed_urls) override;
69 void BookmarkNodeChanged(bookmarks::BookmarkModel* model,
70 const bookmarks::BookmarkNode* node) override;
71 void BookmarkNodeFaviconChanged(bookmarks::BookmarkModel* model,
72 const bookmarks::BookmarkNode* node) override;
73 void BookmarkNodeChildrenReordered(
74 bookmarks::BookmarkModel* model,
75 const bookmarks::BookmarkNode* node) override;
76 void ExtensiveBookmarkChangesBeginning(
77 bookmarks::BookmarkModel* model) override;
78 void ExtensiveBookmarkChangesEnded(bookmarks::BookmarkModel* model) override;
80 private:
81 // Helper to actually dispatch an event to extension listeners.
82 void DispatchEvent(events::HistogramValue histogram_value,
83 const std::string& event_name,
84 scoped_ptr<base::ListValue> event_args);
86 content::BrowserContext* browser_context_;
87 bookmarks::BookmarkModel* model_;
88 bookmarks::ManagedBookmarkService* managed_;
90 DISALLOW_COPY_AND_ASSIGN(BookmarkEventRouter);
93 class BookmarksAPI : public BrowserContextKeyedAPI,
94 public EventRouter::Observer {
95 public:
96 explicit BookmarksAPI(content::BrowserContext* context);
97 ~BookmarksAPI() override;
99 // KeyedService implementation.
100 void Shutdown() override;
102 // BrowserContextKeyedAPI implementation.
103 static BrowserContextKeyedAPIFactory<BookmarksAPI>* GetFactoryInstance();
105 // EventRouter::Observer implementation.
106 void OnListenerAdded(const EventListenerInfo& details) override;
108 private:
109 friend class BrowserContextKeyedAPIFactory<BookmarksAPI>;
111 content::BrowserContext* browser_context_;
113 // BrowserContextKeyedAPI implementation.
114 static const char* service_name() {
115 return "BookmarksAPI";
117 static const bool kServiceIsNULLWhileTesting = true;
119 // Created lazily upon OnListenerAdded.
120 scoped_ptr<BookmarkEventRouter> bookmark_event_router_;
123 class BookmarksFunction : public ChromeAsyncExtensionFunction,
124 public bookmarks::BaseBookmarkModelObserver {
125 public:
126 // AsyncExtensionFunction:
127 bool RunAsync() override;
129 protected:
130 ~BookmarksFunction() override {}
132 // RunAsync semantic equivalent called when the bookmarks are ready.
133 virtual bool RunOnReady() = 0;
135 // Helper to get the BookmarkModel.
136 bookmarks::BookmarkModel* GetBookmarkModel();
138 // Helper to get the ManagedBookmarkService.
139 bookmarks::ManagedBookmarkService* GetManagedBookmarkService();
141 // Helper to get the bookmark id as int64 from the given string id.
142 // Sets error_ to an error string if the given id string can't be parsed
143 // as an int64. In case of error, doesn't change id and returns false.
144 bool GetBookmarkIdAsInt64(const std::string& id_string, int64* id);
146 // Helper to get the bookmark node from a given string id.
147 // If the given id can't be parsed or doesn't refer to a valid node, sets
148 // error_ and returns NULL.
149 const bookmarks::BookmarkNode* GetBookmarkNodeFromId(
150 const std::string& id_string);
152 // Helper to create a bookmark node from a CreateDetails object. If a node
153 // can't be created based on the given details, sets error_ and returns NULL.
154 const bookmarks::BookmarkNode* CreateBookmarkNode(
155 bookmarks::BookmarkModel* model,
156 const api::bookmarks::CreateDetails& details,
157 const bookmarks::BookmarkNode::MetaInfoMap* meta_info);
159 // Helper that checks if bookmark editing is enabled. If it's not, this sets
160 // error_ to the appropriate error string.
161 bool EditBookmarksEnabled();
163 // Helper that checks if |node| can be modified. Returns false if |node|
164 // is NULL, or a managed node, or the root node. In these cases the node
165 // can't be edited, can't have new child nodes appended, and its direct
166 // children can't be moved or reordered.
167 bool CanBeModified(const bookmarks::BookmarkNode* node);
169 private:
170 // bookmarks::BaseBookmarkModelObserver:
171 void BookmarkModelChanged() override;
172 void BookmarkModelLoaded(bookmarks::BookmarkModel* model,
173 bool ids_reassigned) override;
175 void RunAndSendResponse();
178 class BookmarksGetFunction : public BookmarksFunction {
179 public:
180 DECLARE_EXTENSION_FUNCTION("bookmarks.get", BOOKMARKS_GET)
182 protected:
183 ~BookmarksGetFunction() override {}
185 // BookmarksFunction:
186 bool RunOnReady() override;
189 class BookmarksGetChildrenFunction : public BookmarksFunction {
190 public:
191 DECLARE_EXTENSION_FUNCTION("bookmarks.getChildren", BOOKMARKS_GETCHILDREN)
193 protected:
194 ~BookmarksGetChildrenFunction() override {}
196 // BookmarksFunction:
197 bool RunOnReady() override;
200 class BookmarksGetRecentFunction : public BookmarksFunction {
201 public:
202 DECLARE_EXTENSION_FUNCTION("bookmarks.getRecent", BOOKMARKS_GETRECENT)
204 protected:
205 ~BookmarksGetRecentFunction() override {}
207 // BookmarksFunction:
208 bool RunOnReady() override;
211 class BookmarksGetTreeFunction : public BookmarksFunction {
212 public:
213 DECLARE_EXTENSION_FUNCTION("bookmarks.getTree", BOOKMARKS_GETTREE)
215 protected:
216 ~BookmarksGetTreeFunction() override {}
218 // BookmarksFunction:
219 bool RunOnReady() override;
222 class BookmarksGetSubTreeFunction : public BookmarksFunction {
223 public:
224 DECLARE_EXTENSION_FUNCTION("bookmarks.getSubTree", BOOKMARKS_GETSUBTREE)
226 protected:
227 ~BookmarksGetSubTreeFunction() override {}
229 // BookmarksFunction:
230 bool RunOnReady() override;
233 class BookmarksSearchFunction : public BookmarksFunction {
234 public:
235 DECLARE_EXTENSION_FUNCTION("bookmarks.search", BOOKMARKS_SEARCH)
237 protected:
238 ~BookmarksSearchFunction() override {}
240 // BookmarksFunction:
241 bool RunOnReady() override;
244 class BookmarksRemoveFunction : public BookmarksFunction {
245 public:
246 DECLARE_EXTENSION_FUNCTION("bookmarks.remove", BOOKMARKS_REMOVE)
248 // Returns true on successful parse and sets invalid_id to true if conversion
249 // from id string to int64 failed.
250 static bool ExtractIds(const base::ListValue* args,
251 std::list<int64>* ids,
252 bool* invalid_id);
254 protected:
255 ~BookmarksRemoveFunction() override {}
257 // BookmarksFunction:
258 bool RunOnReady() override;
261 class BookmarksRemoveTreeFunction : public BookmarksRemoveFunction {
262 public:
263 DECLARE_EXTENSION_FUNCTION("bookmarks.removeTree", BOOKMARKS_REMOVETREE)
265 protected:
266 ~BookmarksRemoveTreeFunction() override {}
269 class BookmarksCreateFunction : public BookmarksFunction {
270 public:
271 DECLARE_EXTENSION_FUNCTION("bookmarks.create", BOOKMARKS_CREATE)
273 protected:
274 ~BookmarksCreateFunction() override {}
276 // BookmarksFunction:
277 bool RunOnReady() override;
280 class BookmarksMoveFunction : public BookmarksFunction {
281 public:
282 DECLARE_EXTENSION_FUNCTION("bookmarks.move", BOOKMARKS_MOVE)
284 static bool ExtractIds(const base::ListValue* args,
285 std::list<int64>* ids,
286 bool* invalid_id);
288 protected:
289 ~BookmarksMoveFunction() override {}
291 // BookmarksFunction:
292 bool RunOnReady() override;
295 class BookmarksUpdateFunction : public BookmarksFunction {
296 public:
297 DECLARE_EXTENSION_FUNCTION("bookmarks.update", BOOKMARKS_UPDATE)
299 static bool ExtractIds(const base::ListValue* args,
300 std::list<int64>* ids,
301 bool* invalid_id);
303 protected:
304 ~BookmarksUpdateFunction() override {}
306 // BookmarksFunction:
307 bool RunOnReady() override;
310 class BookmarksIOFunction : public BookmarksFunction,
311 public ui::SelectFileDialog::Listener {
312 public:
313 BookmarksIOFunction();
315 void FileSelected(const base::FilePath& path,
316 int index,
317 void* params) override = 0;
319 // ui::SelectFileDialog::Listener:
320 void MultiFilesSelected(const std::vector<base::FilePath>& files,
321 void* params) override;
322 void FileSelectionCanceled(void* params) override;
324 void SelectFile(ui::SelectFileDialog::Type type);
326 protected:
327 ~BookmarksIOFunction() override;
329 private:
330 void ShowSelectFileDialog(
331 ui::SelectFileDialog::Type type,
332 const base::FilePath& default_path);
334 protected:
335 scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
338 class BookmarksImportFunction : public BookmarksIOFunction {
339 public:
340 DECLARE_EXTENSION_FUNCTION("bookmarks.import", BOOKMARKS_IMPORT)
342 // BookmarkManagerIOFunction:
343 void FileSelected(const base::FilePath& path,
344 int index,
345 void* params) override;
347 private:
348 ~BookmarksImportFunction() override {}
350 // BookmarksFunction:
351 bool RunOnReady() override;
354 class BookmarksExportFunction : public BookmarksIOFunction {
355 public:
356 DECLARE_EXTENSION_FUNCTION("bookmarks.export", BOOKMARKS_EXPORT)
358 // BookmarkManagerIOFunction:
359 void FileSelected(const base::FilePath& path,
360 int index,
361 void* params) override;
363 private:
364 ~BookmarksExportFunction() override {}
366 // BookmarksFunction:
367 bool RunOnReady() override;
370 } // namespace extensions
372 #endif // CHROME_BROWSER_EXTENSIONS_API_BOOKMARKS_BOOKMARKS_API_H_