Home | History | Annotate | Download | only in bookmarks
      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/chrome_extension_function.h"
     17 #include "extensions/browser/event_router.h"
     18 #include "ui/shell_dialogs/select_file_dialog.h"
     19 
     20 class Profile;
     21 
     22 namespace base {
     23 class FilePath;
     24 class ListValue;
     25 }
     26 
     27 namespace extensions {
     28 
     29 // Observes BookmarkModel and then routes the notifications as events to
     30 // the extension system.
     31 class 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 std::string& 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 
     72 class 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 
    103 class BookmarksFunction : public ChromeAsyncExtensionFunction,
    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 to get the bookmark node from a given string id.
    118   // If the given id can't be parsed or doesn't refer to a valid node, sets
    119   // error_ and returns NULL.
    120   const BookmarkNode* GetBookmarkNodeFromId(const std::string& id_string);
    121 
    122   // Helper that checks if bookmark editing is enabled. If it's not, this sets
    123   // error_ to the appropriate error string.
    124   bool EditBookmarksEnabled();
    125 
    126  private:
    127   // BaseBookmarkModelObserver:
    128   virtual void BookmarkModelChanged() OVERRIDE;
    129   virtual void Loaded(BookmarkModel* model, bool ids_reassigned) OVERRIDE;
    130 };
    131 
    132 class BookmarksGetFunction : public BookmarksFunction {
    133  public:
    134   DECLARE_EXTENSION_FUNCTION("bookmarks.get", BOOKMARKS_GET)
    135 
    136  protected:
    137   virtual ~BookmarksGetFunction() {}
    138 
    139   // ExtensionFunction:
    140   virtual bool RunImpl() OVERRIDE;
    141 };
    142 
    143 class BookmarksGetChildrenFunction : public BookmarksFunction {
    144  public:
    145   DECLARE_EXTENSION_FUNCTION("bookmarks.getChildren", BOOKMARKS_GETCHILDREN)
    146 
    147  protected:
    148   virtual ~BookmarksGetChildrenFunction() {}
    149 
    150   // ExtensionFunction:
    151   virtual bool RunImpl() OVERRIDE;
    152 };
    153 
    154 class BookmarksGetRecentFunction : public BookmarksFunction {
    155  public:
    156   DECLARE_EXTENSION_FUNCTION("bookmarks.getRecent", BOOKMARKS_GETRECENT)
    157 
    158  protected:
    159   virtual ~BookmarksGetRecentFunction() {}
    160 
    161   // ExtensionFunction:
    162   virtual bool RunImpl() OVERRIDE;
    163 };
    164 
    165 class BookmarksGetTreeFunction : public BookmarksFunction {
    166  public:
    167   DECLARE_EXTENSION_FUNCTION("bookmarks.getTree", BOOKMARKS_GETTREE)
    168 
    169  protected:
    170   virtual ~BookmarksGetTreeFunction() {}
    171 
    172   // ExtensionFunction:
    173   virtual bool RunImpl() OVERRIDE;
    174 };
    175 
    176 class BookmarksGetSubTreeFunction : public BookmarksFunction {
    177  public:
    178   DECLARE_EXTENSION_FUNCTION("bookmarks.getSubTree", BOOKMARKS_GETSUBTREE)
    179 
    180  protected:
    181   virtual ~BookmarksGetSubTreeFunction() {}
    182 
    183   // ExtensionFunction:
    184   virtual bool RunImpl() OVERRIDE;
    185 };
    186 
    187 class BookmarksSearchFunction : public BookmarksFunction {
    188  public:
    189   DECLARE_EXTENSION_FUNCTION("bookmarks.search", BOOKMARKS_SEARCH)
    190 
    191  protected:
    192   virtual ~BookmarksSearchFunction() {}
    193 
    194   // ExtensionFunction:
    195   virtual bool RunImpl() OVERRIDE;
    196 };
    197 
    198 class BookmarksRemoveFunction : public BookmarksFunction {
    199  public:
    200   DECLARE_EXTENSION_FUNCTION("bookmarks.remove", BOOKMARKS_REMOVE)
    201 
    202   // Returns true on successful parse and sets invalid_id to true if conversion
    203   // from id string to int64 failed.
    204   static bool ExtractIds(const base::ListValue* args,
    205                          std::list<int64>* ids,
    206                          bool* invalid_id);
    207   // ExtensionFunction:
    208   virtual void GetQuotaLimitHeuristics(
    209       QuotaLimitHeuristics* heuristics) const OVERRIDE;
    210 
    211  protected:
    212   virtual ~BookmarksRemoveFunction() {}
    213 
    214   // ExtensionFunction:
    215   virtual bool RunImpl() OVERRIDE;
    216 };
    217 
    218 class BookmarksRemoveTreeFunction : public BookmarksRemoveFunction {
    219  public:
    220   DECLARE_EXTENSION_FUNCTION("bookmarks.removeTree", BOOKMARKS_REMOVETREE)
    221 
    222  protected:
    223   virtual ~BookmarksRemoveTreeFunction() {}
    224 };
    225 
    226 class BookmarksCreateFunction : public BookmarksFunction {
    227  public:
    228   DECLARE_EXTENSION_FUNCTION("bookmarks.create", BOOKMARKS_CREATE)
    229 
    230   // ExtensionFunction:
    231   virtual void GetQuotaLimitHeuristics(
    232       QuotaLimitHeuristics* heuristics) const OVERRIDE;
    233 
    234  protected:
    235   virtual ~BookmarksCreateFunction() {}
    236 
    237   // ExtensionFunction:
    238   virtual bool RunImpl() OVERRIDE;
    239 };
    240 
    241 class BookmarksMoveFunction : public BookmarksFunction {
    242  public:
    243   DECLARE_EXTENSION_FUNCTION("bookmarks.move", BOOKMARKS_MOVE)
    244 
    245   static bool ExtractIds(const base::ListValue* args,
    246                          std::list<int64>* ids,
    247                          bool* invalid_id);
    248 
    249   // ExtensionFunction:
    250   virtual void GetQuotaLimitHeuristics(
    251       QuotaLimitHeuristics* heuristics) const OVERRIDE;
    252 
    253  protected:
    254   virtual ~BookmarksMoveFunction() {}
    255 
    256   // ExtensionFunction:
    257   virtual bool RunImpl() OVERRIDE;
    258 };
    259 
    260 class BookmarksUpdateFunction : public BookmarksFunction {
    261  public:
    262   DECLARE_EXTENSION_FUNCTION("bookmarks.update", BOOKMARKS_UPDATE)
    263 
    264   static bool ExtractIds(const base::ListValue* args,
    265                          std::list<int64>* ids,
    266                          bool* invalid_id);
    267 
    268   // ExtensionFunction:
    269   virtual void GetQuotaLimitHeuristics(
    270       QuotaLimitHeuristics* heuristics) const OVERRIDE;
    271 
    272  protected:
    273   virtual ~BookmarksUpdateFunction() {}
    274 
    275   // ExtensionFunction:
    276   virtual bool RunImpl() OVERRIDE;
    277 };
    278 
    279 class BookmarksIOFunction : public BookmarksFunction,
    280                             public ui::SelectFileDialog::Listener {
    281  public:
    282   BookmarksIOFunction();
    283 
    284   virtual void FileSelected(const base::FilePath& path, int index, void* params) = 0;
    285 
    286   // ui::SelectFileDialog::Listener:
    287   virtual void MultiFilesSelected(const std::vector<base::FilePath>& files,
    288                                   void* params) OVERRIDE;
    289   virtual void FileSelectionCanceled(void* params) OVERRIDE;
    290 
    291   void SelectFile(ui::SelectFileDialog::Type type);
    292 
    293  protected:
    294   virtual ~BookmarksIOFunction();
    295 
    296  private:
    297   void ShowSelectFileDialog(
    298       ui::SelectFileDialog::Type type,
    299       const base::FilePath& default_path);
    300 
    301  protected:
    302   scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
    303 };
    304 
    305 class BookmarksImportFunction : public BookmarksIOFunction {
    306  public:
    307   DECLARE_EXTENSION_FUNCTION("bookmarks.import", BOOKMARKS_IMPORT)
    308 
    309   // BookmarkManagerIOFunction:
    310   virtual void FileSelected(const base::FilePath& path,
    311                             int index,
    312                             void* params) OVERRIDE;
    313 
    314  private:
    315   virtual ~BookmarksImportFunction() {}
    316 
    317   // ExtensionFunction:
    318   virtual bool RunImpl() OVERRIDE;
    319 };
    320 
    321 class BookmarksExportFunction : public BookmarksIOFunction {
    322  public:
    323   DECLARE_EXTENSION_FUNCTION("bookmarks.export", BOOKMARKS_EXPORT)
    324 
    325   // BookmarkManagerIOFunction:
    326   virtual void FileSelected(const base::FilePath& path,
    327                             int index,
    328                             void* params) OVERRIDE;
    329 
    330  private:
    331   virtual ~BookmarksExportFunction() {}
    332 
    333   // ExtensionFunction:
    334   virtual bool RunImpl() OVERRIDE;
    335 };
    336 
    337 }  // namespace extensions
    338 
    339 #endif  // CHROME_BROWSER_EXTENSIONS_API_BOOKMARKS_BOOKMARKS_API_H_
    340