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 virtual ~BookmarkEventRouter();
47 // BookmarkModelObserver:
48 virtual void BookmarkModelLoaded(BookmarkModel* model,
49 bool ids_reassigned) OVERRIDE;
50 virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE;
51 virtual void BookmarkNodeMoved(BookmarkModel* model,
52 const BookmarkNode* old_parent,
54 const BookmarkNode* new_parent,
55 int new_index) OVERRIDE;
56 virtual void OnWillAddBookmarkNode(BookmarkModel* model,
57 BookmarkNode* node) OVERRIDE;
58 virtual void BookmarkNodeAdded(BookmarkModel* model,
59 const BookmarkNode* parent,
61 virtual void BookmarkNodeRemoved(BookmarkModel* model,
62 const BookmarkNode* parent,
64 const BookmarkNode* node,
65 const std::set<GURL>& removed_urls) OVERRIDE;
66 virtual void BookmarkAllUserNodesRemoved(
68 const std::set<GURL>& removed_urls) OVERRIDE;
69 virtual void BookmarkNodeChanged(BookmarkModel* model,
70 const BookmarkNode* node) OVERRIDE;
71 virtual void BookmarkNodeFaviconChanged(BookmarkModel* model,
72 const BookmarkNode* node) OVERRIDE;
73 virtual void BookmarkNodeChildrenReordered(BookmarkModel* model,
74 const BookmarkNode* node) OVERRIDE;
75 virtual void ExtensiveBookmarkChangesBeginning(BookmarkModel* model) OVERRIDE;
76 virtual void ExtensiveBookmarkChangesEnded(BookmarkModel* model) OVERRIDE;
79 // Helper to actually dispatch an event to extension listeners.
80 void DispatchEvent(const std::string& event_name,
81 scoped_ptr<base::ListValue> event_args);
83 content::BrowserContext* browser_context_;
84 BookmarkModel* model_;
85 ChromeBookmarkClient* client_;
87 DISALLOW_COPY_AND_ASSIGN(BookmarkEventRouter);
90 class BookmarksAPI : public BrowserContextKeyedAPI,
91 public EventRouter::Observer {
93 explicit BookmarksAPI(content::BrowserContext* context);
94 virtual ~BookmarksAPI();
96 // KeyedService implementation.
97 virtual void Shutdown() OVERRIDE;
99 // BrowserContextKeyedAPI implementation.
100 static BrowserContextKeyedAPIFactory<BookmarksAPI>* GetFactoryInstance();
102 // EventRouter::Observer implementation.
103 virtual void OnListenerAdded(
104 const EventListenerInfo& details) OVERRIDE;
107 friend class BrowserContextKeyedAPIFactory<BookmarksAPI>;
109 content::BrowserContext* browser_context_;
111 // BrowserContextKeyedAPI implementation.
112 static const char* service_name() {
113 return "BookmarksAPI";
115 static const bool kServiceIsNULLWhileTesting = true;
117 // Created lazily upon OnListenerAdded.
118 scoped_ptr<BookmarkEventRouter> bookmark_event_router_;
121 class BookmarksFunction : public ChromeAsyncExtensionFunction,
122 public BaseBookmarkModelObserver {
124 // AsyncExtensionFunction:
125 virtual bool RunAsync() OVERRIDE;
128 virtual ~BookmarksFunction() {}
130 // RunAsync semantic equivalent called when the bookmarks are ready.
131 virtual bool RunOnReady() = 0;
133 // Helper to get the BookmarkModel.
134 BookmarkModel* GetBookmarkModel();
136 // Helper to get the ChromeBookmarkClient.
137 ChromeBookmarkClient* GetChromeBookmarkClient();
139 // Helper to get the bookmark id as int64 from the given string id.
140 // Sets error_ to an error string if the given id string can't be parsed
141 // as an int64. In case of error, doesn't change id and returns false.
142 bool GetBookmarkIdAsInt64(const std::string& id_string, int64* id);
144 // Helper to get the bookmark node from a given string id.
145 // If the given id can't be parsed or doesn't refer to a valid node, sets
146 // error_ and returns NULL.
147 const BookmarkNode* GetBookmarkNodeFromId(const std::string& id_string);
149 // Helper to create a bookmark node from a CreateDetails object. If a node
150 // can't be created based on the given details, sets error_ and returns NULL.
151 const BookmarkNode* CreateBookmarkNode(
152 BookmarkModel* model,
153 const api::bookmarks::CreateDetails& details,
154 const BookmarkNode::MetaInfoMap* meta_info);
156 // Helper that checks if bookmark editing is enabled. If it's not, this sets
157 // error_ to the appropriate error string.
158 bool EditBookmarksEnabled();
160 // Helper that checks if |node| can be modified. Returns false if |node|
161 // is NULL, or a managed node, or the root node. In these cases the node
162 // can't be edited, can't have new child nodes appended, and its direct
163 // children can't be moved or reordered.
164 bool CanBeModified(const BookmarkNode* node);
167 // BaseBookmarkModelObserver:
168 virtual void BookmarkModelChanged() OVERRIDE;
169 virtual void BookmarkModelLoaded(BookmarkModel* model,
170 bool ids_reassigned) OVERRIDE;
173 class BookmarksGetFunction : public BookmarksFunction {
175 DECLARE_EXTENSION_FUNCTION("bookmarks.get", BOOKMARKS_GET)
178 virtual ~BookmarksGetFunction() {}
180 // BookmarksFunction:
181 virtual bool RunOnReady() OVERRIDE;
184 class BookmarksGetChildrenFunction : public BookmarksFunction {
186 DECLARE_EXTENSION_FUNCTION("bookmarks.getChildren", BOOKMARKS_GETCHILDREN)
189 virtual ~BookmarksGetChildrenFunction() {}
191 // BookmarksFunction:
192 virtual bool RunOnReady() OVERRIDE;
195 class BookmarksGetRecentFunction : public BookmarksFunction {
197 DECLARE_EXTENSION_FUNCTION("bookmarks.getRecent", BOOKMARKS_GETRECENT)
200 virtual ~BookmarksGetRecentFunction() {}
202 // BookmarksFunction:
203 virtual bool RunOnReady() OVERRIDE;
206 class BookmarksGetTreeFunction : public BookmarksFunction {
208 DECLARE_EXTENSION_FUNCTION("bookmarks.getTree", BOOKMARKS_GETTREE)
211 virtual ~BookmarksGetTreeFunction() {}
213 // BookmarksFunction:
214 virtual bool RunOnReady() OVERRIDE;
217 class BookmarksGetSubTreeFunction : public BookmarksFunction {
219 DECLARE_EXTENSION_FUNCTION("bookmarks.getSubTree", BOOKMARKS_GETSUBTREE)
222 virtual ~BookmarksGetSubTreeFunction() {}
224 // BookmarksFunction:
225 virtual bool RunOnReady() OVERRIDE;
228 class BookmarksSearchFunction : public BookmarksFunction {
230 DECLARE_EXTENSION_FUNCTION("bookmarks.search", BOOKMARKS_SEARCH)
233 virtual ~BookmarksSearchFunction() {}
235 // BookmarksFunction:
236 virtual bool RunOnReady() OVERRIDE;
239 class BookmarksRemoveFunction : public BookmarksFunction {
241 DECLARE_EXTENSION_FUNCTION("bookmarks.remove", BOOKMARKS_REMOVE)
243 // Returns true on successful parse and sets invalid_id to true if conversion
244 // from id string to int64 failed.
245 static bool ExtractIds(const base::ListValue* args,
246 std::list<int64>* ids,
250 virtual ~BookmarksRemoveFunction() {}
252 // BookmarksFunction:
253 virtual bool RunOnReady() OVERRIDE;
256 class BookmarksRemoveTreeFunction : public BookmarksRemoveFunction {
258 DECLARE_EXTENSION_FUNCTION("bookmarks.removeTree", BOOKMARKS_REMOVETREE)
261 virtual ~BookmarksRemoveTreeFunction() {}
264 class BookmarksCreateFunction : public BookmarksFunction {
266 DECLARE_EXTENSION_FUNCTION("bookmarks.create", BOOKMARKS_CREATE)
269 virtual ~BookmarksCreateFunction() {}
271 // BookmarksFunction:
272 virtual bool RunOnReady() OVERRIDE;
275 class BookmarksMoveFunction : public BookmarksFunction {
277 DECLARE_EXTENSION_FUNCTION("bookmarks.move", BOOKMARKS_MOVE)
279 static bool ExtractIds(const base::ListValue* args,
280 std::list<int64>* ids,
284 virtual ~BookmarksMoveFunction() {}
286 // BookmarksFunction:
287 virtual bool RunOnReady() OVERRIDE;
290 class BookmarksUpdateFunction : public BookmarksFunction {
292 DECLARE_EXTENSION_FUNCTION("bookmarks.update", BOOKMARKS_UPDATE)
294 static bool ExtractIds(const base::ListValue* args,
295 std::list<int64>* ids,
299 virtual ~BookmarksUpdateFunction() {}
301 // BookmarksFunction:
302 virtual bool RunOnReady() OVERRIDE;
305 class BookmarksIOFunction : public BookmarksFunction,
306 public ui::SelectFileDialog::Listener {
308 BookmarksIOFunction();
310 virtual void FileSelected(const base::FilePath& path, int index, void* params) = 0;
312 // ui::SelectFileDialog::Listener:
313 virtual void MultiFilesSelected(const std::vector<base::FilePath>& files,
314 void* params) OVERRIDE;
315 virtual void FileSelectionCanceled(void* params) OVERRIDE;
317 void SelectFile(ui::SelectFileDialog::Type type);
320 virtual ~BookmarksIOFunction();
323 void ShowSelectFileDialog(
324 ui::SelectFileDialog::Type type,
325 const base::FilePath& default_path);
328 scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
331 class BookmarksImportFunction : public BookmarksIOFunction {
333 DECLARE_EXTENSION_FUNCTION("bookmarks.import", BOOKMARKS_IMPORT)
335 // BookmarkManagerIOFunction:
336 virtual void FileSelected(const base::FilePath& path,
338 void* params) OVERRIDE;
341 virtual ~BookmarksImportFunction() {}
343 // BookmarksFunction:
344 virtual bool RunOnReady() OVERRIDE;
347 class BookmarksExportFunction : public BookmarksIOFunction {
349 DECLARE_EXTENSION_FUNCTION("bookmarks.export", BOOKMARKS_EXPORT)
351 // BookmarkManagerIOFunction:
352 virtual void FileSelected(const base::FilePath& path,
354 void* params) OVERRIDE;
357 virtual ~BookmarksExportFunction() {}
359 // BookmarksFunction:
360 virtual bool RunOnReady() OVERRIDE;
363 } // namespace extensions
365 #endif // CHROME_BROWSER_EXTENSIONS_API_BOOKMARKS_BOOKMARKS_API_H_