Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / bookmarks / bookmarks_api.h
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.
4
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_BOOKMARKS_BOOKMARKS_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_BOOKMARKS_BOOKMARKS_API_H_
7
8 #include <list>
9 #include <string>
10 #include <vector>
11
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"
20
21 class ChromeBookmarkClient;
22
23 namespace base {
24 class FilePath;
25 class ListValue;
26 }
27
28 namespace content {
29 class BrowserContext;
30 }
31
32 namespace extensions {
33
34 namespace api {
35 namespace bookmarks {
36 struct CreateDetails;
37 }
38 }
39
40 // Observes BookmarkModel and then routes the notifications as events to
41 // the extension system.
42 class BookmarkEventRouter : public BookmarkModelObserver {
43  public:
44   explicit BookmarkEventRouter(Profile* profile);
45   virtual ~BookmarkEventRouter();
46
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,
53                                  int old_index,
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,
60                                  int index) OVERRIDE;
61   virtual void BookmarkNodeRemoved(BookmarkModel* model,
62                                    const BookmarkNode* parent,
63                                    int old_index,
64                                    const BookmarkNode* node,
65                                    const std::set<GURL>& removed_urls) OVERRIDE;
66   virtual void BookmarkAllUserNodesRemoved(
67       BookmarkModel* model,
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;
77
78  private:
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);
82
83   content::BrowserContext* browser_context_;
84   BookmarkModel* model_;
85   ChromeBookmarkClient* client_;
86
87   DISALLOW_COPY_AND_ASSIGN(BookmarkEventRouter);
88 };
89
90 class BookmarksAPI : public BrowserContextKeyedAPI,
91                      public EventRouter::Observer {
92  public:
93   explicit BookmarksAPI(content::BrowserContext* context);
94   virtual ~BookmarksAPI();
95
96   // KeyedService implementation.
97   virtual void Shutdown() OVERRIDE;
98
99   // BrowserContextKeyedAPI implementation.
100   static BrowserContextKeyedAPIFactory<BookmarksAPI>* GetFactoryInstance();
101
102   // EventRouter::Observer implementation.
103   virtual void OnListenerAdded(
104       const EventListenerInfo& details) OVERRIDE;
105
106  private:
107   friend class BrowserContextKeyedAPIFactory<BookmarksAPI>;
108
109   content::BrowserContext* browser_context_;
110
111   // BrowserContextKeyedAPI implementation.
112   static const char* service_name() {
113     return "BookmarksAPI";
114   }
115   static const bool kServiceIsNULLWhileTesting = true;
116
117   // Created lazily upon OnListenerAdded.
118   scoped_ptr<BookmarkEventRouter> bookmark_event_router_;
119 };
120
121 class BookmarksFunction : public ChromeAsyncExtensionFunction,
122                           public BaseBookmarkModelObserver {
123  public:
124   // AsyncExtensionFunction:
125   virtual bool RunAsync() OVERRIDE;
126
127  protected:
128   virtual ~BookmarksFunction() {}
129
130   // RunAsync semantic equivalent called when the bookmarks are ready.
131   virtual bool RunOnReady() = 0;
132
133   // Helper to get the BookmarkModel.
134   BookmarkModel* GetBookmarkModel();
135
136   // Helper to get the ChromeBookmarkClient.
137   ChromeBookmarkClient* GetChromeBookmarkClient();
138
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);
143
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);
148
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);
155
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();
159
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);
165
166  private:
167   // BaseBookmarkModelObserver:
168   virtual void BookmarkModelChanged() OVERRIDE;
169   virtual void BookmarkModelLoaded(BookmarkModel* model,
170                                    bool ids_reassigned) OVERRIDE;
171 };
172
173 class BookmarksGetFunction : public BookmarksFunction {
174  public:
175   DECLARE_EXTENSION_FUNCTION("bookmarks.get", BOOKMARKS_GET)
176
177  protected:
178   virtual ~BookmarksGetFunction() {}
179
180   // BookmarksFunction:
181   virtual bool RunOnReady() OVERRIDE;
182 };
183
184 class BookmarksGetChildrenFunction : public BookmarksFunction {
185  public:
186   DECLARE_EXTENSION_FUNCTION("bookmarks.getChildren", BOOKMARKS_GETCHILDREN)
187
188  protected:
189   virtual ~BookmarksGetChildrenFunction() {}
190
191   // BookmarksFunction:
192   virtual bool RunOnReady() OVERRIDE;
193 };
194
195 class BookmarksGetRecentFunction : public BookmarksFunction {
196  public:
197   DECLARE_EXTENSION_FUNCTION("bookmarks.getRecent", BOOKMARKS_GETRECENT)
198
199  protected:
200   virtual ~BookmarksGetRecentFunction() {}
201
202   // BookmarksFunction:
203   virtual bool RunOnReady() OVERRIDE;
204 };
205
206 class BookmarksGetTreeFunction : public BookmarksFunction {
207  public:
208   DECLARE_EXTENSION_FUNCTION("bookmarks.getTree", BOOKMARKS_GETTREE)
209
210  protected:
211   virtual ~BookmarksGetTreeFunction() {}
212
213   // BookmarksFunction:
214   virtual bool RunOnReady() OVERRIDE;
215 };
216
217 class BookmarksGetSubTreeFunction : public BookmarksFunction {
218  public:
219   DECLARE_EXTENSION_FUNCTION("bookmarks.getSubTree", BOOKMARKS_GETSUBTREE)
220
221  protected:
222   virtual ~BookmarksGetSubTreeFunction() {}
223
224   // BookmarksFunction:
225   virtual bool RunOnReady() OVERRIDE;
226 };
227
228 class BookmarksSearchFunction : public BookmarksFunction {
229  public:
230   DECLARE_EXTENSION_FUNCTION("bookmarks.search", BOOKMARKS_SEARCH)
231
232  protected:
233   virtual ~BookmarksSearchFunction() {}
234
235   // BookmarksFunction:
236   virtual bool RunOnReady() OVERRIDE;
237 };
238
239 class BookmarksRemoveFunction : public BookmarksFunction {
240  public:
241   DECLARE_EXTENSION_FUNCTION("bookmarks.remove", BOOKMARKS_REMOVE)
242
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,
247                          bool* invalid_id);
248
249  protected:
250   virtual ~BookmarksRemoveFunction() {}
251
252   // BookmarksFunction:
253   virtual bool RunOnReady() OVERRIDE;
254 };
255
256 class BookmarksRemoveTreeFunction : public BookmarksRemoveFunction {
257  public:
258   DECLARE_EXTENSION_FUNCTION("bookmarks.removeTree", BOOKMARKS_REMOVETREE)
259
260  protected:
261   virtual ~BookmarksRemoveTreeFunction() {}
262 };
263
264 class BookmarksCreateFunction : public BookmarksFunction {
265  public:
266   DECLARE_EXTENSION_FUNCTION("bookmarks.create", BOOKMARKS_CREATE)
267
268  protected:
269   virtual ~BookmarksCreateFunction() {}
270
271   // BookmarksFunction:
272   virtual bool RunOnReady() OVERRIDE;
273 };
274
275 class BookmarksMoveFunction : public BookmarksFunction {
276  public:
277   DECLARE_EXTENSION_FUNCTION("bookmarks.move", BOOKMARKS_MOVE)
278
279   static bool ExtractIds(const base::ListValue* args,
280                          std::list<int64>* ids,
281                          bool* invalid_id);
282
283  protected:
284   virtual ~BookmarksMoveFunction() {}
285
286   // BookmarksFunction:
287   virtual bool RunOnReady() OVERRIDE;
288 };
289
290 class BookmarksUpdateFunction : public BookmarksFunction {
291  public:
292   DECLARE_EXTENSION_FUNCTION("bookmarks.update", BOOKMARKS_UPDATE)
293
294   static bool ExtractIds(const base::ListValue* args,
295                          std::list<int64>* ids,
296                          bool* invalid_id);
297
298  protected:
299   virtual ~BookmarksUpdateFunction() {}
300
301   // BookmarksFunction:
302   virtual bool RunOnReady() OVERRIDE;
303 };
304
305 class BookmarksIOFunction : public BookmarksFunction,
306                             public ui::SelectFileDialog::Listener {
307  public:
308   BookmarksIOFunction();
309
310   virtual void FileSelected(const base::FilePath& path, int index, void* params) = 0;
311
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;
316
317   void SelectFile(ui::SelectFileDialog::Type type);
318
319  protected:
320   virtual ~BookmarksIOFunction();
321
322  private:
323   void ShowSelectFileDialog(
324       ui::SelectFileDialog::Type type,
325       const base::FilePath& default_path);
326
327  protected:
328   scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
329 };
330
331 class BookmarksImportFunction : public BookmarksIOFunction {
332  public:
333   DECLARE_EXTENSION_FUNCTION("bookmarks.import", BOOKMARKS_IMPORT)
334
335   // BookmarkManagerIOFunction:
336   virtual void FileSelected(const base::FilePath& path,
337                             int index,
338                             void* params) OVERRIDE;
339
340  private:
341   virtual ~BookmarksImportFunction() {}
342
343   // BookmarksFunction:
344   virtual bool RunOnReady() OVERRIDE;
345 };
346
347 class BookmarksExportFunction : public BookmarksIOFunction {
348  public:
349   DECLARE_EXTENSION_FUNCTION("bookmarks.export", BOOKMARKS_EXPORT)
350
351   // BookmarkManagerIOFunction:
352   virtual void FileSelected(const base::FilePath& path,
353                             int index,
354                             void* params) OVERRIDE;
355
356  private:
357   virtual ~BookmarksExportFunction() {}
358
359   // BookmarksFunction:
360   virtual bool RunOnReady() OVERRIDE;
361 };
362
363 }  // namespace extensions
364
365 #endif  // CHROME_BROWSER_EXTENSIONS_API_BOOKMARKS_BOOKMARKS_API_H_