Update To 11.40.268.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   ~BookmarkEventRouter() override;
46
47   // BookmarkModelObserver:
48   void BookmarkModelLoaded(BookmarkModel* model, bool ids_reassigned) override;
49   void BookmarkModelBeingDeleted(BookmarkModel* model) override;
50   void BookmarkNodeMoved(BookmarkModel* model,
51                          const BookmarkNode* old_parent,
52                          int old_index,
53                          const BookmarkNode* new_parent,
54                          int new_index) override;
55   void BookmarkNodeAdded(BookmarkModel* model,
56                          const BookmarkNode* parent,
57                          int index) override;
58   void BookmarkNodeRemoved(BookmarkModel* model,
59                            const BookmarkNode* parent,
60                            int old_index,
61                            const BookmarkNode* node,
62                            const std::set<GURL>& removed_urls) override;
63   void BookmarkAllUserNodesRemoved(BookmarkModel* model,
64                                    const std::set<GURL>& removed_urls) override;
65   void BookmarkNodeChanged(BookmarkModel* model,
66                            const BookmarkNode* node) override;
67   void BookmarkNodeFaviconChanged(BookmarkModel* model,
68                                   const BookmarkNode* node) override;
69   void BookmarkNodeChildrenReordered(BookmarkModel* model,
70                                      const BookmarkNode* node) override;
71   void ExtensiveBookmarkChangesBeginning(BookmarkModel* model) override;
72   void ExtensiveBookmarkChangesEnded(BookmarkModel* model) override;
73
74  private:
75   // Helper to actually dispatch an event to extension listeners.
76   void DispatchEvent(const std::string& event_name,
77                      scoped_ptr<base::ListValue> event_args);
78
79   content::BrowserContext* browser_context_;
80   BookmarkModel* model_;
81   ChromeBookmarkClient* client_;
82
83   DISALLOW_COPY_AND_ASSIGN(BookmarkEventRouter);
84 };
85
86 class BookmarksAPI : public BrowserContextKeyedAPI,
87                      public EventRouter::Observer {
88  public:
89   explicit BookmarksAPI(content::BrowserContext* context);
90   ~BookmarksAPI() override;
91
92   // KeyedService implementation.
93   void Shutdown() override;
94
95   // BrowserContextKeyedAPI implementation.
96   static BrowserContextKeyedAPIFactory<BookmarksAPI>* GetFactoryInstance();
97
98   // EventRouter::Observer implementation.
99   void OnListenerAdded(const EventListenerInfo& details) override;
100
101  private:
102   friend class BrowserContextKeyedAPIFactory<BookmarksAPI>;
103
104   content::BrowserContext* browser_context_;
105
106   // BrowserContextKeyedAPI implementation.
107   static const char* service_name() {
108     return "BookmarksAPI";
109   }
110   static const bool kServiceIsNULLWhileTesting = true;
111
112   // Created lazily upon OnListenerAdded.
113   scoped_ptr<BookmarkEventRouter> bookmark_event_router_;
114 };
115
116 class BookmarksFunction : public ChromeAsyncExtensionFunction,
117                           public BaseBookmarkModelObserver {
118  public:
119   // AsyncExtensionFunction:
120   bool RunAsync() override;
121
122  protected:
123   ~BookmarksFunction() override {}
124
125   // RunAsync semantic equivalent called when the bookmarks are ready.
126   virtual bool RunOnReady() = 0;
127
128   // Helper to get the BookmarkModel.
129   BookmarkModel* GetBookmarkModel();
130
131   // Helper to get the ChromeBookmarkClient.
132   ChromeBookmarkClient* GetChromeBookmarkClient();
133
134   // Helper to get the bookmark id as int64 from the given string id.
135   // Sets error_ to an error string if the given id string can't be parsed
136   // as an int64. In case of error, doesn't change id and returns false.
137   bool GetBookmarkIdAsInt64(const std::string& id_string, int64* id);
138
139   // Helper to get the bookmark node from a given string id.
140   // If the given id can't be parsed or doesn't refer to a valid node, sets
141   // error_ and returns NULL.
142   const BookmarkNode* GetBookmarkNodeFromId(const std::string& id_string);
143
144   // Helper to create a bookmark node from a CreateDetails object. If a node
145   // can't be created based on the given details, sets error_ and returns NULL.
146   const BookmarkNode* CreateBookmarkNode(
147       BookmarkModel* model,
148       const api::bookmarks::CreateDetails& details,
149       const BookmarkNode::MetaInfoMap* meta_info);
150
151   // Helper that checks if bookmark editing is enabled. If it's not, this sets
152   // error_ to the appropriate error string.
153   bool EditBookmarksEnabled();
154
155   // Helper that checks if |node| can be modified. Returns false if |node|
156   // is NULL, or a managed node, or the root node. In these cases the node
157   // can't be edited, can't have new child nodes appended, and its direct
158   // children can't be moved or reordered.
159   bool CanBeModified(const BookmarkNode* node);
160
161  private:
162   // BaseBookmarkModelObserver:
163   void BookmarkModelChanged() override;
164   void BookmarkModelLoaded(BookmarkModel* model, bool ids_reassigned) override;
165
166   void RunAndSendResponse();
167 };
168
169 class BookmarksGetFunction : public BookmarksFunction {
170  public:
171   DECLARE_EXTENSION_FUNCTION("bookmarks.get", BOOKMARKS_GET)
172
173  protected:
174   ~BookmarksGetFunction() override {}
175
176   // BookmarksFunction:
177   bool RunOnReady() override;
178 };
179
180 class BookmarksGetChildrenFunction : public BookmarksFunction {
181  public:
182   DECLARE_EXTENSION_FUNCTION("bookmarks.getChildren", BOOKMARKS_GETCHILDREN)
183
184  protected:
185   ~BookmarksGetChildrenFunction() override {}
186
187   // BookmarksFunction:
188   bool RunOnReady() override;
189 };
190
191 class BookmarksGetRecentFunction : public BookmarksFunction {
192  public:
193   DECLARE_EXTENSION_FUNCTION("bookmarks.getRecent", BOOKMARKS_GETRECENT)
194
195  protected:
196   ~BookmarksGetRecentFunction() override {}
197
198   // BookmarksFunction:
199   bool RunOnReady() override;
200 };
201
202 class BookmarksGetTreeFunction : public BookmarksFunction {
203  public:
204   DECLARE_EXTENSION_FUNCTION("bookmarks.getTree", BOOKMARKS_GETTREE)
205
206  protected:
207   ~BookmarksGetTreeFunction() override {}
208
209   // BookmarksFunction:
210   bool RunOnReady() override;
211 };
212
213 class BookmarksGetSubTreeFunction : public BookmarksFunction {
214  public:
215   DECLARE_EXTENSION_FUNCTION("bookmarks.getSubTree", BOOKMARKS_GETSUBTREE)
216
217  protected:
218   ~BookmarksGetSubTreeFunction() override {}
219
220   // BookmarksFunction:
221   bool RunOnReady() override;
222 };
223
224 class BookmarksSearchFunction : public BookmarksFunction {
225  public:
226   DECLARE_EXTENSION_FUNCTION("bookmarks.search", BOOKMARKS_SEARCH)
227
228  protected:
229   ~BookmarksSearchFunction() override {}
230
231   // BookmarksFunction:
232   bool RunOnReady() override;
233 };
234
235 class BookmarksRemoveFunction : public BookmarksFunction {
236  public:
237   DECLARE_EXTENSION_FUNCTION("bookmarks.remove", BOOKMARKS_REMOVE)
238
239   // Returns true on successful parse and sets invalid_id to true if conversion
240   // from id string to int64 failed.
241   static bool ExtractIds(const base::ListValue* args,
242                          std::list<int64>* ids,
243                          bool* invalid_id);
244
245  protected:
246   ~BookmarksRemoveFunction() override {}
247
248   // BookmarksFunction:
249   bool RunOnReady() override;
250 };
251
252 class BookmarksRemoveTreeFunction : public BookmarksRemoveFunction {
253  public:
254   DECLARE_EXTENSION_FUNCTION("bookmarks.removeTree", BOOKMARKS_REMOVETREE)
255
256  protected:
257   ~BookmarksRemoveTreeFunction() override {}
258 };
259
260 class BookmarksCreateFunction : public BookmarksFunction {
261  public:
262   DECLARE_EXTENSION_FUNCTION("bookmarks.create", BOOKMARKS_CREATE)
263
264  protected:
265   ~BookmarksCreateFunction() override {}
266
267   // BookmarksFunction:
268   bool RunOnReady() override;
269 };
270
271 class BookmarksMoveFunction : public BookmarksFunction {
272  public:
273   DECLARE_EXTENSION_FUNCTION("bookmarks.move", BOOKMARKS_MOVE)
274
275   static bool ExtractIds(const base::ListValue* args,
276                          std::list<int64>* ids,
277                          bool* invalid_id);
278
279  protected:
280   ~BookmarksMoveFunction() override {}
281
282   // BookmarksFunction:
283   bool RunOnReady() override;
284 };
285
286 class BookmarksUpdateFunction : public BookmarksFunction {
287  public:
288   DECLARE_EXTENSION_FUNCTION("bookmarks.update", BOOKMARKS_UPDATE)
289
290   static bool ExtractIds(const base::ListValue* args,
291                          std::list<int64>* ids,
292                          bool* invalid_id);
293
294  protected:
295   ~BookmarksUpdateFunction() override {}
296
297   // BookmarksFunction:
298   bool RunOnReady() override;
299 };
300
301 class BookmarksIOFunction : public BookmarksFunction,
302                             public ui::SelectFileDialog::Listener {
303  public:
304   BookmarksIOFunction();
305
306   virtual void FileSelected(const base::FilePath& path, int index, void* params) = 0;
307
308   // ui::SelectFileDialog::Listener:
309   void MultiFilesSelected(const std::vector<base::FilePath>& files,
310                           void* params) override;
311   void FileSelectionCanceled(void* params) override;
312
313   void SelectFile(ui::SelectFileDialog::Type type);
314
315  protected:
316   ~BookmarksIOFunction() override;
317
318  private:
319   void ShowSelectFileDialog(
320       ui::SelectFileDialog::Type type,
321       const base::FilePath& default_path);
322
323  protected:
324   scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
325 };
326
327 class BookmarksImportFunction : public BookmarksIOFunction {
328  public:
329   DECLARE_EXTENSION_FUNCTION("bookmarks.import", BOOKMARKS_IMPORT)
330
331   // BookmarkManagerIOFunction:
332   void FileSelected(const base::FilePath& path,
333                     int index,
334                     void* params) override;
335
336  private:
337   ~BookmarksImportFunction() override {}
338
339   // BookmarksFunction:
340   bool RunOnReady() override;
341 };
342
343 class BookmarksExportFunction : public BookmarksIOFunction {
344  public:
345   DECLARE_EXTENSION_FUNCTION("bookmarks.export", BOOKMARKS_EXPORT)
346
347   // BookmarkManagerIOFunction:
348   void FileSelected(const base::FilePath& path,
349                     int index,
350                     void* params) override;
351
352  private:
353   ~BookmarksExportFunction() override {}
354
355   // BookmarksFunction:
356   bool RunOnReady() override;
357 };
358
359 }  // namespace extensions
360
361 #endif  // CHROME_BROWSER_EXTENSIONS_API_BOOKMARKS_BOOKMARKS_API_H_