Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / extensions / api / bookmarks / bookmarks_api.h
blob9c3f082c714932bdf289bb07b29592c6a50c87f8
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 class ChromeBookmarkClient;
23 namespace base {
24 class FilePath;
25 class ListValue;
28 namespace content {
29 class BrowserContext;
32 namespace extensions {
34 namespace api {
35 namespace bookmarks {
36 struct CreateDetails;
40 // Observes BookmarkModel and then routes the notifications as events to
41 // the extension system.
42 class BookmarkEventRouter : public bookmarks::BookmarkModelObserver {
43 public:
44 explicit BookmarkEventRouter(Profile* profile);
45 ~BookmarkEventRouter() override;
47 // bookmarks::BookmarkModelObserver:
48 void BookmarkModelLoaded(bookmarks::BookmarkModel* model,
49 bool ids_reassigned) override;
50 void BookmarkModelBeingDeleted(bookmarks::BookmarkModel* model) override;
51 void BookmarkNodeMoved(bookmarks::BookmarkModel* model,
52 const bookmarks::BookmarkNode* old_parent,
53 int old_index,
54 const bookmarks::BookmarkNode* new_parent,
55 int new_index) override;
56 void BookmarkNodeAdded(bookmarks::BookmarkModel* model,
57 const bookmarks::BookmarkNode* parent,
58 int index) override;
59 void BookmarkNodeRemoved(bookmarks::BookmarkModel* model,
60 const bookmarks::BookmarkNode* parent,
61 int old_index,
62 const bookmarks::BookmarkNode* node,
63 const std::set<GURL>& removed_urls) override;
64 void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel* model,
65 const std::set<GURL>& removed_urls) override;
66 void BookmarkNodeChanged(bookmarks::BookmarkModel* model,
67 const bookmarks::BookmarkNode* node) override;
68 void BookmarkNodeFaviconChanged(bookmarks::BookmarkModel* model,
69 const bookmarks::BookmarkNode* node) override;
70 void BookmarkNodeChildrenReordered(
71 bookmarks::BookmarkModel* model,
72 const bookmarks::BookmarkNode* node) override;
73 void ExtensiveBookmarkChangesBeginning(
74 bookmarks::BookmarkModel* model) override;
75 void ExtensiveBookmarkChangesEnded(bookmarks::BookmarkModel* model) override;
77 private:
78 // Helper to actually dispatch an event to extension listeners.
79 void DispatchEvent(const std::string& event_name,
80 scoped_ptr<base::ListValue> event_args);
82 content::BrowserContext* browser_context_;
83 bookmarks::BookmarkModel* model_;
84 ChromeBookmarkClient* client_;
86 DISALLOW_COPY_AND_ASSIGN(BookmarkEventRouter);
89 class BookmarksAPI : public BrowserContextKeyedAPI,
90 public EventRouter::Observer {
91 public:
92 explicit BookmarksAPI(content::BrowserContext* context);
93 ~BookmarksAPI() override;
95 // KeyedService implementation.
96 void Shutdown() override;
98 // BrowserContextKeyedAPI implementation.
99 static BrowserContextKeyedAPIFactory<BookmarksAPI>* GetFactoryInstance();
101 // EventRouter::Observer implementation.
102 void OnListenerAdded(const EventListenerInfo& details) override;
104 private:
105 friend class BrowserContextKeyedAPIFactory<BookmarksAPI>;
107 content::BrowserContext* browser_context_;
109 // BrowserContextKeyedAPI implementation.
110 static const char* service_name() {
111 return "BookmarksAPI";
113 static const bool kServiceIsNULLWhileTesting = true;
115 // Created lazily upon OnListenerAdded.
116 scoped_ptr<BookmarkEventRouter> bookmark_event_router_;
119 class BookmarksFunction : public ChromeAsyncExtensionFunction,
120 public bookmarks::BaseBookmarkModelObserver {
121 public:
122 // AsyncExtensionFunction:
123 bool RunAsync() override;
125 protected:
126 ~BookmarksFunction() override {}
128 // RunAsync semantic equivalent called when the bookmarks are ready.
129 virtual bool RunOnReady() = 0;
131 // Helper to get the BookmarkModel.
132 bookmarks::BookmarkModel* GetBookmarkModel();
134 // Helper to get the ChromeBookmarkClient.
135 ChromeBookmarkClient* GetChromeBookmarkClient();
137 // Helper to get the bookmark id as int64 from the given string id.
138 // Sets error_ to an error string if the given id string can't be parsed
139 // as an int64. In case of error, doesn't change id and returns false.
140 bool GetBookmarkIdAsInt64(const std::string& id_string, int64* id);
142 // Helper to get the bookmark node from a given string id.
143 // If the given id can't be parsed or doesn't refer to a valid node, sets
144 // error_ and returns NULL.
145 const bookmarks::BookmarkNode* GetBookmarkNodeFromId(
146 const std::string& id_string);
148 // Helper to create a bookmark node from a CreateDetails object. If a node
149 // can't be created based on the given details, sets error_ and returns NULL.
150 const bookmarks::BookmarkNode* CreateBookmarkNode(
151 bookmarks::BookmarkModel* model,
152 const api::bookmarks::CreateDetails& details,
153 const bookmarks::BookmarkNode::MetaInfoMap* meta_info);
155 // Helper that checks if bookmark editing is enabled. If it's not, this sets
156 // error_ to the appropriate error string.
157 bool EditBookmarksEnabled();
159 // Helper that checks if |node| can be modified. Returns false if |node|
160 // is NULL, or a managed node, or the root node. In these cases the node
161 // can't be edited, can't have new child nodes appended, and its direct
162 // children can't be moved or reordered.
163 bool CanBeModified(const bookmarks::BookmarkNode* node);
165 private:
166 // bookmarks::BaseBookmarkModelObserver:
167 void BookmarkModelChanged() override;
168 void BookmarkModelLoaded(bookmarks::BookmarkModel* model,
169 bool ids_reassigned) override;
171 void RunAndSendResponse();
174 class BookmarksGetFunction : public BookmarksFunction {
175 public:
176 DECLARE_EXTENSION_FUNCTION("bookmarks.get", BOOKMARKS_GET)
178 protected:
179 ~BookmarksGetFunction() override {}
181 // BookmarksFunction:
182 bool RunOnReady() override;
185 class BookmarksGetChildrenFunction : public BookmarksFunction {
186 public:
187 DECLARE_EXTENSION_FUNCTION("bookmarks.getChildren", BOOKMARKS_GETCHILDREN)
189 protected:
190 ~BookmarksGetChildrenFunction() override {}
192 // BookmarksFunction:
193 bool RunOnReady() override;
196 class BookmarksGetRecentFunction : public BookmarksFunction {
197 public:
198 DECLARE_EXTENSION_FUNCTION("bookmarks.getRecent", BOOKMARKS_GETRECENT)
200 protected:
201 ~BookmarksGetRecentFunction() override {}
203 // BookmarksFunction:
204 bool RunOnReady() override;
207 class BookmarksGetTreeFunction : public BookmarksFunction {
208 public:
209 DECLARE_EXTENSION_FUNCTION("bookmarks.getTree", BOOKMARKS_GETTREE)
211 protected:
212 ~BookmarksGetTreeFunction() override {}
214 // BookmarksFunction:
215 bool RunOnReady() override;
218 class BookmarksGetSubTreeFunction : public BookmarksFunction {
219 public:
220 DECLARE_EXTENSION_FUNCTION("bookmarks.getSubTree", BOOKMARKS_GETSUBTREE)
222 protected:
223 ~BookmarksGetSubTreeFunction() override {}
225 // BookmarksFunction:
226 bool RunOnReady() override;
229 class BookmarksSearchFunction : public BookmarksFunction {
230 public:
231 DECLARE_EXTENSION_FUNCTION("bookmarks.search", BOOKMARKS_SEARCH)
233 protected:
234 ~BookmarksSearchFunction() override {}
236 // BookmarksFunction:
237 bool RunOnReady() override;
240 class BookmarksRemoveFunction : public BookmarksFunction {
241 public:
242 DECLARE_EXTENSION_FUNCTION("bookmarks.remove", BOOKMARKS_REMOVE)
244 // Returns true on successful parse and sets invalid_id to true if conversion
245 // from id string to int64 failed.
246 static bool ExtractIds(const base::ListValue* args,
247 std::list<int64>* ids,
248 bool* invalid_id);
250 protected:
251 ~BookmarksRemoveFunction() override {}
253 // BookmarksFunction:
254 bool RunOnReady() override;
257 class BookmarksRemoveTreeFunction : public BookmarksRemoveFunction {
258 public:
259 DECLARE_EXTENSION_FUNCTION("bookmarks.removeTree", BOOKMARKS_REMOVETREE)
261 protected:
262 ~BookmarksRemoveTreeFunction() override {}
265 class BookmarksCreateFunction : public BookmarksFunction {
266 public:
267 DECLARE_EXTENSION_FUNCTION("bookmarks.create", BOOKMARKS_CREATE)
269 protected:
270 ~BookmarksCreateFunction() override {}
272 // BookmarksFunction:
273 bool RunOnReady() override;
276 class BookmarksMoveFunction : public BookmarksFunction {
277 public:
278 DECLARE_EXTENSION_FUNCTION("bookmarks.move", BOOKMARKS_MOVE)
280 static bool ExtractIds(const base::ListValue* args,
281 std::list<int64>* ids,
282 bool* invalid_id);
284 protected:
285 ~BookmarksMoveFunction() override {}
287 // BookmarksFunction:
288 bool RunOnReady() override;
291 class BookmarksUpdateFunction : public BookmarksFunction {
292 public:
293 DECLARE_EXTENSION_FUNCTION("bookmarks.update", BOOKMARKS_UPDATE)
295 static bool ExtractIds(const base::ListValue* args,
296 std::list<int64>* ids,
297 bool* invalid_id);
299 protected:
300 ~BookmarksUpdateFunction() override {}
302 // BookmarksFunction:
303 bool RunOnReady() override;
306 class BookmarksIOFunction : public BookmarksFunction,
307 public ui::SelectFileDialog::Listener {
308 public:
309 BookmarksIOFunction();
311 void FileSelected(const base::FilePath& path,
312 int index,
313 void* params) override = 0;
315 // ui::SelectFileDialog::Listener:
316 void MultiFilesSelected(const std::vector<base::FilePath>& files,
317 void* params) override;
318 void FileSelectionCanceled(void* params) override;
320 void SelectFile(ui::SelectFileDialog::Type type);
322 protected:
323 ~BookmarksIOFunction() override;
325 private:
326 void ShowSelectFileDialog(
327 ui::SelectFileDialog::Type type,
328 const base::FilePath& default_path);
330 protected:
331 scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
334 class BookmarksImportFunction : public BookmarksIOFunction {
335 public:
336 DECLARE_EXTENSION_FUNCTION("bookmarks.import", BOOKMARKS_IMPORT)
338 // BookmarkManagerIOFunction:
339 void FileSelected(const base::FilePath& path,
340 int index,
341 void* params) override;
343 private:
344 ~BookmarksImportFunction() override {}
346 // BookmarksFunction:
347 bool RunOnReady() override;
350 class BookmarksExportFunction : public BookmarksIOFunction {
351 public:
352 DECLARE_EXTENSION_FUNCTION("bookmarks.export", BOOKMARKS_EXPORT)
354 // BookmarkManagerIOFunction:
355 void FileSelected(const base::FilePath& path,
356 int index,
357 void* params) override;
359 private:
360 ~BookmarksExportFunction() override {}
362 // BookmarksFunction:
363 bool RunOnReady() override;
366 } // namespace extensions
368 #endif // CHROME_BROWSER_EXTENSIONS_API_BOOKMARKS_BOOKMARKS_API_H_