• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
service_name()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:
~BookmarksFunction()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:
~BookmarksGetFunction()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:
~BookmarksGetChildrenFunction()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:
~BookmarksGetRecentFunction()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:
~BookmarksGetTreeFunction()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:
~BookmarksGetSubTreeFunction()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:
~BookmarksSearchFunction()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   // BookmarksFunction:
249   virtual void GetQuotaLimitHeuristics(
250       QuotaLimitHeuristics* heuristics) const OVERRIDE;
251 
252  protected:
~BookmarksRemoveFunction()253   virtual ~BookmarksRemoveFunction() {}
254 
255   // BookmarksFunction:
256   virtual bool RunOnReady() OVERRIDE;
257 };
258 
259 class BookmarksRemoveTreeFunction : public BookmarksRemoveFunction {
260  public:
261   DECLARE_EXTENSION_FUNCTION("bookmarks.removeTree", BOOKMARKS_REMOVETREE)
262 
263  protected:
~BookmarksRemoveTreeFunction()264   virtual ~BookmarksRemoveTreeFunction() {}
265 };
266 
267 class BookmarksCreateFunction : public BookmarksFunction {
268  public:
269   DECLARE_EXTENSION_FUNCTION("bookmarks.create", BOOKMARKS_CREATE)
270 
271   // BookmarksFunction:
272   virtual void GetQuotaLimitHeuristics(
273       QuotaLimitHeuristics* heuristics) const OVERRIDE;
274 
275  protected:
~BookmarksCreateFunction()276   virtual ~BookmarksCreateFunction() {}
277 
278   // BookmarksFunction:
279   virtual bool RunOnReady() OVERRIDE;
280 };
281 
282 class BookmarksMoveFunction : public BookmarksFunction {
283  public:
284   DECLARE_EXTENSION_FUNCTION("bookmarks.move", BOOKMARKS_MOVE)
285 
286   static bool ExtractIds(const base::ListValue* args,
287                          std::list<int64>* ids,
288                          bool* invalid_id);
289 
290   // BookmarksFunction:
291   virtual void GetQuotaLimitHeuristics(
292       QuotaLimitHeuristics* heuristics) const OVERRIDE;
293 
294  protected:
~BookmarksMoveFunction()295   virtual ~BookmarksMoveFunction() {}
296 
297   // BookmarksFunction:
298   virtual bool RunOnReady() OVERRIDE;
299 };
300 
301 class BookmarksUpdateFunction : public BookmarksFunction {
302  public:
303   DECLARE_EXTENSION_FUNCTION("bookmarks.update", BOOKMARKS_UPDATE)
304 
305   static bool ExtractIds(const base::ListValue* args,
306                          std::list<int64>* ids,
307                          bool* invalid_id);
308 
309   // BookmarksFunction:
310   virtual void GetQuotaLimitHeuristics(
311       QuotaLimitHeuristics* heuristics) const OVERRIDE;
312 
313  protected:
~BookmarksUpdateFunction()314   virtual ~BookmarksUpdateFunction() {}
315 
316   // BookmarksFunction:
317   virtual bool RunOnReady() OVERRIDE;
318 };
319 
320 class BookmarksIOFunction : public BookmarksFunction,
321                             public ui::SelectFileDialog::Listener {
322  public:
323   BookmarksIOFunction();
324 
325   virtual void FileSelected(const base::FilePath& path, int index, void* params) = 0;
326 
327   // ui::SelectFileDialog::Listener:
328   virtual void MultiFilesSelected(const std::vector<base::FilePath>& files,
329                                   void* params) OVERRIDE;
330   virtual void FileSelectionCanceled(void* params) OVERRIDE;
331 
332   void SelectFile(ui::SelectFileDialog::Type type);
333 
334  protected:
335   virtual ~BookmarksIOFunction();
336 
337  private:
338   void ShowSelectFileDialog(
339       ui::SelectFileDialog::Type type,
340       const base::FilePath& default_path);
341 
342  protected:
343   scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
344 };
345 
346 class BookmarksImportFunction : public BookmarksIOFunction {
347  public:
348   DECLARE_EXTENSION_FUNCTION("bookmarks.import", BOOKMARKS_IMPORT)
349 
350   // BookmarkManagerIOFunction:
351   virtual void FileSelected(const base::FilePath& path,
352                             int index,
353                             void* params) OVERRIDE;
354 
355  private:
~BookmarksImportFunction()356   virtual ~BookmarksImportFunction() {}
357 
358   // BookmarksFunction:
359   virtual bool RunOnReady() OVERRIDE;
360 };
361 
362 class BookmarksExportFunction : public BookmarksIOFunction {
363  public:
364   DECLARE_EXTENSION_FUNCTION("bookmarks.export", BOOKMARKS_EXPORT)
365 
366   // BookmarkManagerIOFunction:
367   virtual void FileSelected(const base::FilePath& path,
368                             int index,
369                             void* params) OVERRIDE;
370 
371  private:
~BookmarksExportFunction()372   virtual ~BookmarksExportFunction() {}
373 
374   // BookmarksFunction:
375   virtual bool RunOnReady() OVERRIDE;
376 };
377 
378 }  // namespace extensions
379 
380 #endif  // CHROME_BROWSER_EXTENSIONS_API_BOOKMARKS_BOOKMARKS_API_H_
381