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/bookmarks/base_bookmark_model_observer.h"
15#include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
16#include "chrome/browser/extensions/event_router.h"
17#include "chrome/browser/extensions/extension_function.h"
18#include "ui/shell_dialogs/select_file_dialog.h"
19
20class Profile;
21
22namespace base {
23class FilePath;
24class ListValue;
25}
26
27namespace extensions {
28
29// Observes BookmarkModel and then routes the notifications as events to
30// the extension system.
31class BookmarkEventRouter : public BookmarkModelObserver {
32 public:
33  BookmarkEventRouter(Profile* profile, BookmarkModel* model);
34  virtual ~BookmarkEventRouter();
35
36  // BookmarkModelObserver:
37  virtual void Loaded(BookmarkModel* model, bool ids_reassigned) OVERRIDE;
38  virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE;
39  virtual void BookmarkNodeMoved(BookmarkModel* model,
40                                 const BookmarkNode* old_parent,
41                                 int old_index,
42                                 const BookmarkNode* new_parent,
43                                 int new_index) OVERRIDE;
44  virtual void BookmarkNodeAdded(BookmarkModel* model,
45                                 const BookmarkNode* parent,
46                                 int index) OVERRIDE;
47  virtual void BookmarkNodeRemoved(BookmarkModel* model,
48                                   const BookmarkNode* parent,
49                                   int old_index,
50                                   const BookmarkNode* node) OVERRIDE;
51  virtual void BookmarkAllNodesRemoved(BookmarkModel* model) OVERRIDE;
52  virtual void BookmarkNodeChanged(BookmarkModel* model,
53                                   const BookmarkNode* node) OVERRIDE;
54  virtual void BookmarkNodeFaviconChanged(BookmarkModel* model,
55                                          const BookmarkNode* node) OVERRIDE;
56  virtual void BookmarkNodeChildrenReordered(BookmarkModel* model,
57                                             const BookmarkNode* node) OVERRIDE;
58  virtual void ExtensiveBookmarkChangesBeginning(BookmarkModel* model) OVERRIDE;
59  virtual void ExtensiveBookmarkChangesEnded(BookmarkModel* model) OVERRIDE;
60
61 private:
62  // Helper to actually dispatch an event to extension listeners.
63  void DispatchEvent(const char* event_name,
64                     scoped_ptr<base::ListValue> event_args);
65
66  Profile* profile_;
67  BookmarkModel* model_;
68
69  DISALLOW_COPY_AND_ASSIGN(BookmarkEventRouter);
70};
71
72class BookmarksAPI : public ProfileKeyedAPI,
73                     public EventRouter::Observer {
74 public:
75  explicit BookmarksAPI(Profile* profile);
76  virtual ~BookmarksAPI();
77
78  // BrowserContextKeyedService implementation.
79  virtual void Shutdown() OVERRIDE;
80
81  // ProfileKeyedAPI implementation.
82  static ProfileKeyedAPIFactory<BookmarksAPI>* GetFactoryInstance();
83
84  // EventRouter::Observer implementation.
85  virtual void OnListenerAdded(
86      const EventListenerInfo& details) OVERRIDE;
87
88 private:
89  friend class ProfileKeyedAPIFactory<BookmarksAPI>;
90
91  Profile* profile_;
92
93  // ProfileKeyedAPI implementation.
94  static const char* service_name() {
95    return "BookmarksAPI";
96  }
97  static const bool kServiceIsNULLWhileTesting = true;
98
99  // Created lazily upon OnListenerAdded.
100  scoped_ptr<BookmarkEventRouter> bookmark_event_router_;
101};
102
103class BookmarksFunction : public AsyncExtensionFunction,
104                          public BaseBookmarkModelObserver {
105 public:
106  // AsyncExtensionFunction:
107  virtual void Run() OVERRIDE;
108
109 protected:
110  virtual ~BookmarksFunction() {}
111
112  // Helper to get the bookmark id as int64 from the given string id.
113  // Sets error_ to an error string if the given id string can't be parsed
114  // as an int64. In case of error, doesn't change id and returns false.
115  bool GetBookmarkIdAsInt64(const std::string& id_string, int64* id);
116
117  // Helper that checks if bookmark editing is enabled. If it's not, this sets
118  // error_ to the appropriate error string.
119  bool EditBookmarksEnabled();
120
121 private:
122  // BaseBookmarkModelObserver:
123  virtual void BookmarkModelChanged() OVERRIDE;
124  virtual void Loaded(BookmarkModel* model, bool ids_reassigned) OVERRIDE;
125};
126
127class BookmarksGetFunction : public BookmarksFunction {
128 public:
129  DECLARE_EXTENSION_FUNCTION("bookmarks.get", BOOKMARKS_GET)
130
131 protected:
132  virtual ~BookmarksGetFunction() {}
133
134  // ExtensionFunction:
135  virtual bool RunImpl() OVERRIDE;
136};
137
138class BookmarksGetChildrenFunction : public BookmarksFunction {
139 public:
140  DECLARE_EXTENSION_FUNCTION("bookmarks.getChildren", BOOKMARKS_GETCHILDREN)
141
142 protected:
143  virtual ~BookmarksGetChildrenFunction() {}
144
145  // ExtensionFunction:
146  virtual bool RunImpl() OVERRIDE;
147};
148
149class BookmarksGetRecentFunction : public BookmarksFunction {
150 public:
151  DECLARE_EXTENSION_FUNCTION("bookmarks.getRecent", BOOKMARKS_GETRECENT)
152
153 protected:
154  virtual ~BookmarksGetRecentFunction() {}
155
156  // ExtensionFunction:
157  virtual bool RunImpl() OVERRIDE;
158};
159
160class BookmarksGetTreeFunction : public BookmarksFunction {
161 public:
162  DECLARE_EXTENSION_FUNCTION("bookmarks.getTree", BOOKMARKS_GETTREE)
163
164 protected:
165  virtual ~BookmarksGetTreeFunction() {}
166
167  // ExtensionFunction:
168  virtual bool RunImpl() OVERRIDE;
169};
170
171class BookmarksGetSubTreeFunction : public BookmarksFunction {
172 public:
173  DECLARE_EXTENSION_FUNCTION("bookmarks.getSubTree", BOOKMARKS_GETSUBTREE)
174
175 protected:
176  virtual ~BookmarksGetSubTreeFunction() {}
177
178  // ExtensionFunction:
179  virtual bool RunImpl() OVERRIDE;
180};
181
182class BookmarksSearchFunction : public BookmarksFunction {
183 public:
184  DECLARE_EXTENSION_FUNCTION("bookmarks.search", BOOKMARKS_SEARCH)
185
186 protected:
187  virtual ~BookmarksSearchFunction() {}
188
189  // ExtensionFunction:
190  virtual bool RunImpl() OVERRIDE;
191};
192
193class BookmarksRemoveFunction : public BookmarksFunction {
194 public:
195  DECLARE_EXTENSION_FUNCTION("bookmarks.remove", BOOKMARKS_REMOVE)
196
197  // Returns true on successful parse and sets invalid_id to true if conversion
198  // from id string to int64 failed.
199  static bool ExtractIds(const base::ListValue* args,
200                         std::list<int64>* ids,
201                         bool* invalid_id);
202  // ExtensionFunction:
203  virtual void GetQuotaLimitHeuristics(
204      QuotaLimitHeuristics* heuristics) const OVERRIDE;
205
206 protected:
207  virtual ~BookmarksRemoveFunction() {}
208
209  // ExtensionFunction:
210  virtual bool RunImpl() OVERRIDE;
211};
212
213class BookmarksRemoveTreeFunction : public BookmarksRemoveFunction {
214 public:
215  DECLARE_EXTENSION_FUNCTION("bookmarks.removeTree", BOOKMARKS_REMOVETREE)
216
217 protected:
218  virtual ~BookmarksRemoveTreeFunction() {}
219};
220
221class BookmarksCreateFunction : public BookmarksFunction {
222 public:
223  DECLARE_EXTENSION_FUNCTION("bookmarks.create", BOOKMARKS_CREATE)
224
225  // ExtensionFunction:
226  virtual void GetQuotaLimitHeuristics(
227      QuotaLimitHeuristics* heuristics) const OVERRIDE;
228
229 protected:
230  virtual ~BookmarksCreateFunction() {}
231
232  // ExtensionFunction:
233  virtual bool RunImpl() OVERRIDE;
234};
235
236class BookmarksMoveFunction : public BookmarksFunction {
237 public:
238  DECLARE_EXTENSION_FUNCTION("bookmarks.move", BOOKMARKS_MOVE)
239
240  static bool ExtractIds(const base::ListValue* args,
241                         std::list<int64>* ids,
242                         bool* invalid_id);
243
244  // ExtensionFunction:
245  virtual void GetQuotaLimitHeuristics(
246      QuotaLimitHeuristics* heuristics) const OVERRIDE;
247
248 protected:
249  virtual ~BookmarksMoveFunction() {}
250
251  // ExtensionFunction:
252  virtual bool RunImpl() OVERRIDE;
253};
254
255class BookmarksUpdateFunction : public BookmarksFunction {
256 public:
257  DECLARE_EXTENSION_FUNCTION("bookmarks.update", BOOKMARKS_UPDATE)
258
259  static bool ExtractIds(const base::ListValue* args,
260                         std::list<int64>* ids,
261                         bool* invalid_id);
262
263  // ExtensionFunction:
264  virtual void GetQuotaLimitHeuristics(
265      QuotaLimitHeuristics* heuristics) const OVERRIDE;
266
267 protected:
268  virtual ~BookmarksUpdateFunction() {}
269
270  // ExtensionFunction:
271  virtual bool RunImpl() OVERRIDE;
272};
273
274class BookmarksIOFunction : public BookmarksFunction,
275                            public ui::SelectFileDialog::Listener {
276 public:
277  BookmarksIOFunction();
278
279  virtual void FileSelected(const base::FilePath& path, int index, void* params) = 0;
280
281  // ui::SelectFileDialog::Listener:
282  virtual void MultiFilesSelected(const std::vector<base::FilePath>& files,
283                                  void* params) OVERRIDE;
284  virtual void FileSelectionCanceled(void* params) OVERRIDE;
285
286  void SelectFile(ui::SelectFileDialog::Type type);
287
288 protected:
289  virtual ~BookmarksIOFunction();
290
291 private:
292  void ShowSelectFileDialog(
293      ui::SelectFileDialog::Type type,
294      const base::FilePath& default_path);
295
296 protected:
297  scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
298};
299
300class BookmarksImportFunction : public BookmarksIOFunction {
301 public:
302  DECLARE_EXTENSION_FUNCTION("bookmarks.import", BOOKMARKS_IMPORT)
303
304  // BookmarkManagerIOFunction:
305  virtual void FileSelected(const base::FilePath& path,
306                            int index,
307                            void* params) OVERRIDE;
308
309 private:
310  virtual ~BookmarksImportFunction() {}
311
312  // ExtensionFunction:
313  virtual bool RunImpl() OVERRIDE;
314};
315
316class BookmarksExportFunction : public BookmarksIOFunction {
317 public:
318  DECLARE_EXTENSION_FUNCTION("bookmarks.export", BOOKMARKS_EXPORT)
319
320  // BookmarkManagerIOFunction:
321  virtual void FileSelected(const base::FilePath& path,
322                            int index,
323                            void* params) OVERRIDE;
324
325 private:
326  virtual ~BookmarksExportFunction() {}
327
328  // ExtensionFunction:
329  virtual bool RunImpl() OVERRIDE;
330};
331
332}  // namespace extensions
333
334#endif  // CHROME_BROWSER_EXTENSIONS_API_BOOKMARKS_BOOKMARKS_API_H_
335