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_
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 class ChromeBookmarkClient;
32 namespace extensions {
40 // Observes BookmarkModel and then routes the notifications as events to
41 // the extension system.
42 class BookmarkEventRouter : public BookmarkModelObserver {
44 explicit BookmarkEventRouter(Profile* profile);
45 ~BookmarkEventRouter() override;
47 // BookmarkModelObserver:
48 void BookmarkModelLoaded(BookmarkModel* model, bool ids_reassigned) override;
49 void BookmarkModelBeingDeleted(BookmarkModel* model) override;
50 void BookmarkNodeMoved(BookmarkModel* model,
51 const BookmarkNode* old_parent,
53 const BookmarkNode* new_parent,
54 int new_index) override;
55 void BookmarkNodeAdded(BookmarkModel* model,
56 const BookmarkNode* parent,
58 void BookmarkNodeRemoved(BookmarkModel* model,
59 const BookmarkNode* parent,
61 const BookmarkNode* node,
62 const std::set<GURL>& removed_urls) override;
63 void BookmarkAllUserNodesRemoved(BookmarkModel* model,
64 const std::set<GURL>& removed_urls) override;
65 void BookmarkNodeChanged(BookmarkModel* model,
66 const BookmarkNode* node) override;
67 void BookmarkNodeFaviconChanged(BookmarkModel* model,
68 const BookmarkNode* node) override;
69 void BookmarkNodeChildrenReordered(BookmarkModel* model,
70 const BookmarkNode* node) override;
71 void ExtensiveBookmarkChangesBeginning(BookmarkModel* model) override;
72 void ExtensiveBookmarkChangesEnded(BookmarkModel* model) override;
75 // Helper to actually dispatch an event to extension listeners.
76 void DispatchEvent(const std::string& event_name,
77 scoped_ptr<base::ListValue> event_args);
79 content::BrowserContext* browser_context_;
80 BookmarkModel* model_;
81 ChromeBookmarkClient* client_;
83 DISALLOW_COPY_AND_ASSIGN(BookmarkEventRouter);
86 class BookmarksAPI : public BrowserContextKeyedAPI,
87 public EventRouter::Observer {
89 explicit BookmarksAPI(content::BrowserContext* context);
90 ~BookmarksAPI() override;
92 // KeyedService implementation.
93 void Shutdown() override;
95 // BrowserContextKeyedAPI implementation.
96 static BrowserContextKeyedAPIFactory<BookmarksAPI>* GetFactoryInstance();
98 // EventRouter::Observer implementation.
99 void OnListenerAdded(const EventListenerInfo& details) override;
102 friend class BrowserContextKeyedAPIFactory<BookmarksAPI>;
104 content::BrowserContext* browser_context_;
106 // BrowserContextKeyedAPI implementation.
107 static const char* service_name() {
108 return "BookmarksAPI";
110 static const bool kServiceIsNULLWhileTesting = true;
112 // Created lazily upon OnListenerAdded.
113 scoped_ptr<BookmarkEventRouter> bookmark_event_router_;
116 class BookmarksFunction : public ChromeAsyncExtensionFunction,
117 public BaseBookmarkModelObserver {
119 // AsyncExtensionFunction:
120 bool RunAsync() override;
123 ~BookmarksFunction() override {}
125 // RunAsync semantic equivalent called when the bookmarks are ready.
126 virtual bool RunOnReady() = 0;
128 // Helper to get the BookmarkModel.
129 BookmarkModel* GetBookmarkModel();
131 // Helper to get the ChromeBookmarkClient.
132 ChromeBookmarkClient* GetChromeBookmarkClient();
134 // Helper to get the bookmark id as int64 from the given string id.
135 // Sets error_ to an error string if the given id string can't be parsed
136 // as an int64. In case of error, doesn't change id and returns false.
137 bool GetBookmarkIdAsInt64(const std::string& id_string, int64* id);
139 // Helper to get the bookmark node from a given string id.
140 // If the given id can't be parsed or doesn't refer to a valid node, sets
141 // error_ and returns NULL.
142 const BookmarkNode* GetBookmarkNodeFromId(const std::string& id_string);
144 // Helper to create a bookmark node from a CreateDetails object. If a node
145 // can't be created based on the given details, sets error_ and returns NULL.
146 const BookmarkNode* CreateBookmarkNode(
147 BookmarkModel* model,
148 const api::bookmarks::CreateDetails& details,
149 const BookmarkNode::MetaInfoMap* meta_info);
151 // Helper that checks if bookmark editing is enabled. If it's not, this sets
152 // error_ to the appropriate error string.
153 bool EditBookmarksEnabled();
155 // Helper that checks if |node| can be modified. Returns false if |node|
156 // is NULL, or a managed node, or the root node. In these cases the node
157 // can't be edited, can't have new child nodes appended, and its direct
158 // children can't be moved or reordered.
159 bool CanBeModified(const BookmarkNode* node);
162 // BaseBookmarkModelObserver:
163 void BookmarkModelChanged() override;
164 void BookmarkModelLoaded(BookmarkModel* model, bool ids_reassigned) override;
166 void RunAndSendResponse();
169 class BookmarksGetFunction : public BookmarksFunction {
171 DECLARE_EXTENSION_FUNCTION("bookmarks.get", BOOKMARKS_GET)
174 ~BookmarksGetFunction() override {}
176 // BookmarksFunction:
177 bool RunOnReady() override;
180 class BookmarksGetChildrenFunction : public BookmarksFunction {
182 DECLARE_EXTENSION_FUNCTION("bookmarks.getChildren", BOOKMARKS_GETCHILDREN)
185 ~BookmarksGetChildrenFunction() override {}
187 // BookmarksFunction:
188 bool RunOnReady() override;
191 class BookmarksGetRecentFunction : public BookmarksFunction {
193 DECLARE_EXTENSION_FUNCTION("bookmarks.getRecent", BOOKMARKS_GETRECENT)
196 ~BookmarksGetRecentFunction() override {}
198 // BookmarksFunction:
199 bool RunOnReady() override;
202 class BookmarksGetTreeFunction : public BookmarksFunction {
204 DECLARE_EXTENSION_FUNCTION("bookmarks.getTree", BOOKMARKS_GETTREE)
207 ~BookmarksGetTreeFunction() override {}
209 // BookmarksFunction:
210 bool RunOnReady() override;
213 class BookmarksGetSubTreeFunction : public BookmarksFunction {
215 DECLARE_EXTENSION_FUNCTION("bookmarks.getSubTree", BOOKMARKS_GETSUBTREE)
218 ~BookmarksGetSubTreeFunction() override {}
220 // BookmarksFunction:
221 bool RunOnReady() override;
224 class BookmarksSearchFunction : public BookmarksFunction {
226 DECLARE_EXTENSION_FUNCTION("bookmarks.search", BOOKMARKS_SEARCH)
229 ~BookmarksSearchFunction() override {}
231 // BookmarksFunction:
232 bool RunOnReady() override;
235 class BookmarksRemoveFunction : public BookmarksFunction {
237 DECLARE_EXTENSION_FUNCTION("bookmarks.remove", BOOKMARKS_REMOVE)
239 // Returns true on successful parse and sets invalid_id to true if conversion
240 // from id string to int64 failed.
241 static bool ExtractIds(const base::ListValue* args,
242 std::list<int64>* ids,
246 ~BookmarksRemoveFunction() override {}
248 // BookmarksFunction:
249 bool RunOnReady() override;
252 class BookmarksRemoveTreeFunction : public BookmarksRemoveFunction {
254 DECLARE_EXTENSION_FUNCTION("bookmarks.removeTree", BOOKMARKS_REMOVETREE)
257 ~BookmarksRemoveTreeFunction() override {}
260 class BookmarksCreateFunction : public BookmarksFunction {
262 DECLARE_EXTENSION_FUNCTION("bookmarks.create", BOOKMARKS_CREATE)
265 ~BookmarksCreateFunction() override {}
267 // BookmarksFunction:
268 bool RunOnReady() override;
271 class BookmarksMoveFunction : public BookmarksFunction {
273 DECLARE_EXTENSION_FUNCTION("bookmarks.move", BOOKMARKS_MOVE)
275 static bool ExtractIds(const base::ListValue* args,
276 std::list<int64>* ids,
280 ~BookmarksMoveFunction() override {}
282 // BookmarksFunction:
283 bool RunOnReady() override;
286 class BookmarksUpdateFunction : public BookmarksFunction {
288 DECLARE_EXTENSION_FUNCTION("bookmarks.update", BOOKMARKS_UPDATE)
290 static bool ExtractIds(const base::ListValue* args,
291 std::list<int64>* ids,
295 ~BookmarksUpdateFunction() override {}
297 // BookmarksFunction:
298 bool RunOnReady() override;
301 class BookmarksIOFunction : public BookmarksFunction,
302 public ui::SelectFileDialog::Listener {
304 BookmarksIOFunction();
306 virtual void FileSelected(const base::FilePath& path, int index, void* params) = 0;
308 // ui::SelectFileDialog::Listener:
309 void MultiFilesSelected(const std::vector<base::FilePath>& files,
310 void* params) override;
311 void FileSelectionCanceled(void* params) override;
313 void SelectFile(ui::SelectFileDialog::Type type);
316 ~BookmarksIOFunction() override;
319 void ShowSelectFileDialog(
320 ui::SelectFileDialog::Type type,
321 const base::FilePath& default_path);
324 scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
327 class BookmarksImportFunction : public BookmarksIOFunction {
329 DECLARE_EXTENSION_FUNCTION("bookmarks.import", BOOKMARKS_IMPORT)
331 // BookmarkManagerIOFunction:
332 void FileSelected(const base::FilePath& path,
334 void* params) override;
337 ~BookmarksImportFunction() override {}
339 // BookmarksFunction:
340 bool RunOnReady() override;
343 class BookmarksExportFunction : public BookmarksIOFunction {
345 DECLARE_EXTENSION_FUNCTION("bookmarks.export", BOOKMARKS_EXPORT)
347 // BookmarkManagerIOFunction:
348 void FileSelected(const base::FilePath& path,
350 void* params) override;
353 ~BookmarksExportFunction() override {}
355 // BookmarksFunction:
356 bool RunOnReady() override;
359 } // namespace extensions
361 #endif // CHROME_BROWSER_EXTENSIONS_API_BOOKMARKS_BOOKMARKS_API_H_