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"
28 class ManagedBookmarkService
;
35 namespace extensions
{
43 // Observes BookmarkModel and then routes the notifications as events to
44 // the extension system.
45 class BookmarkEventRouter
: public bookmarks::BookmarkModelObserver
{
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
,
57 const bookmarks::BookmarkNode
* new_parent
,
58 int new_index
) override
;
59 void BookmarkNodeAdded(bookmarks::BookmarkModel
* model
,
60 const bookmarks::BookmarkNode
* parent
,
62 void BookmarkNodeRemoved(bookmarks::BookmarkModel
* model
,
63 const bookmarks::BookmarkNode
* parent
,
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
;
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
{
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
;
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
{
126 // AsyncExtensionFunction:
127 bool RunAsync() override
;
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
);
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
{
180 DECLARE_EXTENSION_FUNCTION("bookmarks.get", BOOKMARKS_GET
)
183 ~BookmarksGetFunction() override
{}
185 // BookmarksFunction:
186 bool RunOnReady() override
;
189 class BookmarksGetChildrenFunction
: public BookmarksFunction
{
191 DECLARE_EXTENSION_FUNCTION("bookmarks.getChildren", BOOKMARKS_GETCHILDREN
)
194 ~BookmarksGetChildrenFunction() override
{}
196 // BookmarksFunction:
197 bool RunOnReady() override
;
200 class BookmarksGetRecentFunction
: public BookmarksFunction
{
202 DECLARE_EXTENSION_FUNCTION("bookmarks.getRecent", BOOKMARKS_GETRECENT
)
205 ~BookmarksGetRecentFunction() override
{}
207 // BookmarksFunction:
208 bool RunOnReady() override
;
211 class BookmarksGetTreeFunction
: public BookmarksFunction
{
213 DECLARE_EXTENSION_FUNCTION("bookmarks.getTree", BOOKMARKS_GETTREE
)
216 ~BookmarksGetTreeFunction() override
{}
218 // BookmarksFunction:
219 bool RunOnReady() override
;
222 class BookmarksGetSubTreeFunction
: public BookmarksFunction
{
224 DECLARE_EXTENSION_FUNCTION("bookmarks.getSubTree", BOOKMARKS_GETSUBTREE
)
227 ~BookmarksGetSubTreeFunction() override
{}
229 // BookmarksFunction:
230 bool RunOnReady() override
;
233 class BookmarksSearchFunction
: public BookmarksFunction
{
235 DECLARE_EXTENSION_FUNCTION("bookmarks.search", BOOKMARKS_SEARCH
)
238 ~BookmarksSearchFunction() override
{}
240 // BookmarksFunction:
241 bool RunOnReady() override
;
244 class BookmarksRemoveFunction
: public BookmarksFunction
{
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
,
255 ~BookmarksRemoveFunction() override
{}
257 // BookmarksFunction:
258 bool RunOnReady() override
;
261 class BookmarksRemoveTreeFunction
: public BookmarksRemoveFunction
{
263 DECLARE_EXTENSION_FUNCTION("bookmarks.removeTree", BOOKMARKS_REMOVETREE
)
266 ~BookmarksRemoveTreeFunction() override
{}
269 class BookmarksCreateFunction
: public BookmarksFunction
{
271 DECLARE_EXTENSION_FUNCTION("bookmarks.create", BOOKMARKS_CREATE
)
274 ~BookmarksCreateFunction() override
{}
276 // BookmarksFunction:
277 bool RunOnReady() override
;
280 class BookmarksMoveFunction
: public BookmarksFunction
{
282 DECLARE_EXTENSION_FUNCTION("bookmarks.move", BOOKMARKS_MOVE
)
284 static bool ExtractIds(const base::ListValue
* args
,
285 std::list
<int64
>* ids
,
289 ~BookmarksMoveFunction() override
{}
291 // BookmarksFunction:
292 bool RunOnReady() override
;
295 class BookmarksUpdateFunction
: public BookmarksFunction
{
297 DECLARE_EXTENSION_FUNCTION("bookmarks.update", BOOKMARKS_UPDATE
)
299 static bool ExtractIds(const base::ListValue
* args
,
300 std::list
<int64
>* ids
,
304 ~BookmarksUpdateFunction() override
{}
306 // BookmarksFunction:
307 bool RunOnReady() override
;
310 class BookmarksIOFunction
: public BookmarksFunction
,
311 public ui::SelectFileDialog::Listener
{
313 BookmarksIOFunction();
315 void FileSelected(const base::FilePath
& path
,
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
);
327 ~BookmarksIOFunction() override
;
330 void ShowSelectFileDialog(
331 ui::SelectFileDialog::Type type
,
332 const base::FilePath
& default_path
);
335 scoped_refptr
<ui::SelectFileDialog
> select_file_dialog_
;
338 class BookmarksImportFunction
: public BookmarksIOFunction
{
340 DECLARE_EXTENSION_FUNCTION("bookmarks.import", BOOKMARKS_IMPORT
)
342 // BookmarkManagerIOFunction:
343 void FileSelected(const base::FilePath
& path
,
345 void* params
) override
;
348 ~BookmarksImportFunction() override
{}
350 // BookmarksFunction:
351 bool RunOnReady() override
;
354 class BookmarksExportFunction
: public BookmarksIOFunction
{
356 DECLARE_EXTENSION_FUNCTION("bookmarks.export", BOOKMARKS_EXPORT
)
358 // BookmarkManagerIOFunction:
359 void FileSelected(const base::FilePath
& path
,
361 void* params
) override
;
364 ~BookmarksExportFunction() override
{}
366 // BookmarksFunction:
367 bool RunOnReady() override
;
370 } // namespace extensions
372 #endif // CHROME_BROWSER_EXTENSIONS_API_BOOKMARKS_BOOKMARKS_API_H_