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_BOOKMARK_MANAGER_PRIVATE_BOOKMARK_MANAGER_PRIVATE_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_BOOKMARK_MANAGER_PRIVATE_BOOKMARK_MANAGER_PRIVATE_API_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "chrome/browser/extensions/api/bookmarks/bookmarks_api.h"
11 #include "chrome/browser/extensions/chrome_extension_function.h"
12 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
13 #include "chrome/browser/undo/bookmark_undo_service_factory.h"
14 #include "components/bookmarks/browser/base_bookmark_model_observer.h"
15 #include "components/bookmarks/browser/bookmark_node_data.h"
16 #include "components/undo/bookmark_undo_service.h"
17 #include "extensions/browser/browser_context_keyed_api_factory.h"
18 #include "extensions/browser/event_router.h"
23 struct BookmarkNodeData
;
31 namespace extensions
{
33 class BookmarkManagerPrivateEventRouter
34 : public bookmarks::BaseBookmarkModelObserver
{
36 BookmarkManagerPrivateEventRouter(content::BrowserContext
* browser_context
,
37 bookmarks::BookmarkModel
* bookmark_model
);
38 ~BookmarkManagerPrivateEventRouter() override
;
40 // bookmarks::BaseBookmarkModelObserver:
41 void BookmarkModelChanged() override
;
42 void BookmarkModelBeingDeleted(bookmarks::BookmarkModel
* model
) override
;
43 void OnWillChangeBookmarkMetaInfo(
44 bookmarks::BookmarkModel
* model
,
45 const bookmarks::BookmarkNode
* node
) override
;
46 void BookmarkMetaInfoChanged(bookmarks::BookmarkModel
* model
,
47 const bookmarks::BookmarkNode
* node
) override
;
50 // Helper to actually dispatch an event to extension listeners.
51 void DispatchEvent(events::HistogramValue histogram_value
,
52 const std::string
& event_name
,
53 scoped_ptr
<base::ListValue
> event_args
);
55 // Remembers the previous meta info of a node before it was changed.
56 bookmarks::BookmarkNode::MetaInfoMap prev_meta_info_
;
58 content::BrowserContext
* browser_context_
;
59 bookmarks::BookmarkModel
* bookmark_model_
;
62 class BookmarkManagerPrivateAPI
: public BrowserContextKeyedAPI
,
63 public EventRouter::Observer
{
65 explicit BookmarkManagerPrivateAPI(content::BrowserContext
* browser_context
);
66 ~BookmarkManagerPrivateAPI() override
;
68 // BrowserContextKeyedService implementation.
69 void Shutdown() override
;
71 // BrowserContextKeyedAPI implementation.
72 static BrowserContextKeyedAPIFactory
<BookmarkManagerPrivateAPI
>*
75 // EventRouter::Observer implementation.
76 void OnListenerAdded(const EventListenerInfo
& details
) override
;
79 friend class BrowserContextKeyedAPIFactory
<BookmarkManagerPrivateAPI
>;
81 // BrowserContextKeyedAPI implementation.
82 static const char* service_name() { return "BookmarkManagerPrivateAPI"; }
83 static const bool kServiceIsNULLWhileTesting
= true;
85 content::BrowserContext
* browser_context_
;
87 // Created lazily upon OnListenerAdded.
88 scoped_ptr
<BookmarkManagerPrivateEventRouter
> event_router_
;
91 // Class that handles the drag and drop related chrome.bookmarkManagerPrivate
92 // events. This class has one instance per bookmark manager tab.
93 class BookmarkManagerPrivateDragEventRouter
94 : public BookmarkTabHelper::BookmarkDrag
{
96 BookmarkManagerPrivateDragEventRouter(Profile
* profile
,
97 content::WebContents
* web_contents
);
98 ~BookmarkManagerPrivateDragEventRouter() override
;
100 // BookmarkTabHelper::BookmarkDrag interface
101 void OnDragEnter(const bookmarks::BookmarkNodeData
& data
) override
;
102 void OnDragOver(const bookmarks::BookmarkNodeData
& data
) override
;
103 void OnDragLeave(const bookmarks::BookmarkNodeData
& data
) override
;
104 void OnDrop(const bookmarks::BookmarkNodeData
& data
) override
;
106 // The bookmark drag and drop data. This gets set after a drop was done on
107 // the page. This returns NULL if no data is available.
108 const bookmarks::BookmarkNodeData
* GetBookmarkNodeData();
110 // Clears the drag and drop data.
111 void ClearBookmarkNodeData();
114 // Helper to actually dispatch an event to extension listeners.
115 void DispatchEvent(events::HistogramValue histogram_value
,
116 const std::string
& event_name
,
117 scoped_ptr
<base::ListValue
> args
);
120 content::WebContents
* web_contents_
;
121 bookmarks::BookmarkNodeData bookmark_drag_data_
;
123 DISALLOW_COPY_AND_ASSIGN(BookmarkManagerPrivateDragEventRouter
);
126 class ClipboardBookmarkManagerFunction
: public extensions::BookmarksFunction
{
128 ~ClipboardBookmarkManagerFunction() override
{}
130 bool CopyOrCut(bool cut
, const std::vector
<std::string
>& id_list
);
133 class BookmarkManagerPrivateCopyFunction
134 : public ClipboardBookmarkManagerFunction
{
136 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.copy",
137 BOOKMARKMANAGERPRIVATE_COPY
)
140 ~BookmarkManagerPrivateCopyFunction() override
{}
142 // ExtensionFunction:
143 bool RunOnReady() override
;
146 class BookmarkManagerPrivateCutFunction
147 : public ClipboardBookmarkManagerFunction
{
149 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.cut",
150 BOOKMARKMANAGERPRIVATE_CUT
)
153 ~BookmarkManagerPrivateCutFunction() override
{}
155 // ExtensionFunction:
156 bool RunOnReady() override
;
159 class BookmarkManagerPrivatePasteFunction
160 : public extensions::BookmarksFunction
{
162 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.paste",
163 BOOKMARKMANAGERPRIVATE_PASTE
)
166 ~BookmarkManagerPrivatePasteFunction() override
{}
168 // ExtensionFunction:
169 bool RunOnReady() override
;
172 class BookmarkManagerPrivateCanPasteFunction
173 : public extensions::BookmarksFunction
{
175 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.canPaste",
176 BOOKMARKMANAGERPRIVATE_CANPASTE
)
179 ~BookmarkManagerPrivateCanPasteFunction() override
{}
181 // ExtensionFunction:
182 bool RunOnReady() override
;
185 class BookmarkManagerPrivateSortChildrenFunction
186 : public extensions::BookmarksFunction
{
188 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.sortChildren",
189 BOOKMARKMANAGERPRIVATE_SORTCHILDREN
)
192 ~BookmarkManagerPrivateSortChildrenFunction() override
{}
194 // ExtensionFunction:
195 bool RunOnReady() override
;
198 class BookmarkManagerPrivateGetStringsFunction
: public AsyncExtensionFunction
{
200 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.getStrings",
201 BOOKMARKMANAGERPRIVATE_GETSTRINGS
)
204 ~BookmarkManagerPrivateGetStringsFunction() override
{}
206 // ExtensionFunction:
207 bool RunAsync() override
;
210 class BookmarkManagerPrivateStartDragFunction
211 : public extensions::BookmarksFunction
{
213 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.startDrag",
214 BOOKMARKMANAGERPRIVATE_STARTDRAG
)
217 ~BookmarkManagerPrivateStartDragFunction() override
{}
219 // ExtensionFunction:
220 bool RunOnReady() override
;
223 class BookmarkManagerPrivateDropFunction
224 : public extensions::BookmarksFunction
{
226 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.drop",
227 BOOKMARKMANAGERPRIVATE_DROP
)
230 ~BookmarkManagerPrivateDropFunction() override
{}
232 // ExtensionFunction:
233 bool RunOnReady() override
;
236 class BookmarkManagerPrivateGetSubtreeFunction
237 : public extensions::BookmarksFunction
{
239 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.getSubtree",
240 BOOKMARKMANAGERPRIVATE_GETSUBTREE
)
243 ~BookmarkManagerPrivateGetSubtreeFunction() override
{}
245 // ExtensionFunction:
246 bool RunOnReady() override
;
249 class BookmarkManagerPrivateCanEditFunction
250 : public extensions::BookmarksFunction
{
252 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.canEdit",
253 BOOKMARKMANAGERPRIVATE_CANEDIT
)
256 ~BookmarkManagerPrivateCanEditFunction() override
{}
258 // ExtensionFunction:
259 bool RunOnReady() override
;
262 class BookmarkManagerPrivateRecordLaunchFunction
263 : public extensions::BookmarksFunction
{
265 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.recordLaunch",
266 BOOKMARKMANAGERPRIVATE_RECORDLAUNCH
)
269 ~BookmarkManagerPrivateRecordLaunchFunction() override
{}
271 // ExtensionFunction:
272 bool RunOnReady() override
;
275 class BookmarkManagerPrivateCreateWithMetaInfoFunction
276 : public extensions::BookmarksFunction
{
278 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.createWithMetaInfo",
279 BOOKMARKMANAGERPRIVATE_CREATEWITHMETAINFO
)
282 ~BookmarkManagerPrivateCreateWithMetaInfoFunction() override
{}
284 // ExtensionFunction:
285 bool RunOnReady() override
;
288 class BookmarkManagerPrivateGetMetaInfoFunction
289 : public extensions::BookmarksFunction
{
291 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.getMetaInfo",
292 BOOKMARKMANAGERPRIVATE_GETMETAINFO
)
295 ~BookmarkManagerPrivateGetMetaInfoFunction() override
{}
297 // ExtensionFunction:
298 bool RunOnReady() override
;
301 class BookmarkManagerPrivateSetMetaInfoFunction
302 : public extensions::BookmarksFunction
{
304 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.setMetaInfo",
305 BOOKMARKMANAGERPRIVATE_SETMETAINFO
)
308 ~BookmarkManagerPrivateSetMetaInfoFunction() override
{}
310 // ExtensionFunction:
311 bool RunOnReady() override
;
314 class BookmarkManagerPrivateUpdateMetaInfoFunction
315 : public extensions::BookmarksFunction
{
317 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.updateMetaInfo",
318 BOOKMARKMANAGERPRIVATE_UPDATEMETAINFO
)
321 ~BookmarkManagerPrivateUpdateMetaInfoFunction() override
{}
323 // ExtensionFunction:
324 bool RunOnReady() override
;
327 class BookmarkManagerPrivateCanOpenNewWindowsFunction
328 : public extensions::BookmarksFunction
{
330 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.canOpenNewWindows",
331 BOOKMARKMANAGERPRIVATE_CANOPENNEWWINDOWS
)
334 ~BookmarkManagerPrivateCanOpenNewWindowsFunction() override
{}
336 // ExtensionFunction:
337 bool RunOnReady() override
;
340 class BookmarkManagerPrivateRemoveTreesFunction
341 : public extensions::BookmarksFunction
{
343 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.removeTrees",
344 BOOKMARKMANAGERPRIVATE_REMOVETREES
)
347 ~BookmarkManagerPrivateRemoveTreesFunction() override
{}
349 // ExtensionFunction:
350 bool RunOnReady() override
;
353 class BookmarkManagerPrivateUndoFunction
354 : public extensions::BookmarksFunction
{
356 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.undo",
357 BOOKMARKMANAGERPRIVATE_UNDO
)
360 ~BookmarkManagerPrivateUndoFunction() override
{}
362 // ExtensionFunction:
363 bool RunOnReady() override
;
366 class BookmarkManagerPrivateRedoFunction
367 : public extensions::BookmarksFunction
{
369 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.redo",
370 BOOKMARKMANAGERPRIVATE_REDO
)
373 ~BookmarkManagerPrivateRedoFunction() override
{}
375 // ExtensionFunction:
376 bool RunOnReady() override
;
379 class BookmarkManagerPrivateGetUndoInfoFunction
380 : public extensions::BookmarksFunction
{
382 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.getUndoInfo",
383 BOOKMARKMANAGERPRIVATE_UNDOINFO
)
386 ~BookmarkManagerPrivateGetUndoInfoFunction() override
{}
388 // ExtensionFunction:
389 bool RunOnReady() override
;
392 class BookmarkManagerPrivateGetRedoInfoFunction
393 : public extensions::BookmarksFunction
{
395 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.getRedoInfo",
396 BOOKMARKMANAGERPRIVATE_REDOINFO
)
399 ~BookmarkManagerPrivateGetRedoInfoFunction() override
{}
401 // ExtensionFunction:
402 bool RunOnReady() override
;
405 class BookmarkManagerPrivateSetVersionFunction
406 : public extensions::BookmarksFunction
{
408 DECLARE_EXTENSION_FUNCTION("bookmarkManagerPrivate.setVersion",
409 BOOKMARKMANAGERPRIVATE_SETVERSION
);
412 ~BookmarkManagerPrivateSetVersionFunction() override
{}
414 // ExtensionFunction:
415 bool RunOnReady() override
;
418 } // namespace extensions
420 #endif // CHROME_BROWSER_EXTENSIONS_API_BOOKMARK_MANAGER_PRIVATE_BOOKMARK_MANAGER_PRIVATE_API_H_