Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2011 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_EXTENSION_BOOKMARKS_MODULE_H_
      6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_BOOKMARKS_MODULE_H_
      7 #pragma once
      8 
      9 #include <list>
     10 #include <set>
     11 #include <string>
     12 
     13 #include "base/compiler_specific.h"
     14 #include "base/memory/ref_counted.h"
     15 #include "base/memory/singleton.h"
     16 #include "chrome/browser/bookmarks/bookmark_model_observer.h"
     17 #include "chrome/browser/extensions/extension_function.h"
     18 #include "chrome/browser/ui/shell_dialogs.h"
     19 #include "content/common/notification_observer.h"
     20 #include "content/common/notification_registrar.h"
     21 
     22 class FilePath;
     23 
     24 // Observes BookmarkModel and then routes the notifications as events to
     25 // the extension system.
     26 class ExtensionBookmarkEventRouter : public BookmarkModelObserver {
     27  public:
     28   static ExtensionBookmarkEventRouter* GetInstance();
     29   virtual ~ExtensionBookmarkEventRouter();
     30 
     31   // Call this for each model to observe.  Safe to call multiple times per
     32   // model.
     33   void Observe(BookmarkModel* model);
     34 
     35   // BookmarkModelObserver:
     36   virtual void Loaded(BookmarkModel* model) OVERRIDE;
     37   virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE {}
     38   virtual void BookmarkNodeMoved(BookmarkModel* model,
     39                                  const BookmarkNode* old_parent,
     40                                  int old_index,
     41                                  const BookmarkNode* new_parent,
     42                                  int new_index) OVERRIDE;
     43   virtual void BookmarkNodeAdded(BookmarkModel* model,
     44                                  const BookmarkNode* parent,
     45                                  int index) OVERRIDE;
     46   virtual void BookmarkNodeRemoved(BookmarkModel* model,
     47                                    const BookmarkNode* parent,
     48                                    int old_index,
     49                                    const BookmarkNode* node) OVERRIDE;
     50   virtual void BookmarkNodeChanged(BookmarkModel* model,
     51                                    const BookmarkNode* node) OVERRIDE;
     52   virtual void BookmarkNodeFaviconLoaded(BookmarkModel* model,
     53                                          const BookmarkNode* node) OVERRIDE;
     54   virtual void BookmarkNodeChildrenReordered(BookmarkModel* model,
     55                                              const BookmarkNode* node) OVERRIDE;
     56   virtual void BookmarkImportBeginning(BookmarkModel* model) OVERRIDE;
     57   virtual void BookmarkImportEnding(BookmarkModel* model) OVERRIDE;
     58 
     59  private:
     60   ExtensionBookmarkEventRouter();
     61   friend struct DefaultSingletonTraits<ExtensionBookmarkEventRouter>;
     62 
     63   // Helper to actually dispatch an event to extension listeners.
     64   void DispatchEvent(Profile* profile,
     65                      const char* event_name,
     66                      const std::string& json_args);
     67 
     68   // These are stored so that Observe can be called multiple times safely.
     69   // This way the caller doesn't have to know whether it's already observing
     70   // a particular model or not.  The pointers are not owned by this object.
     71   std::set<BookmarkModel*> models_;
     72 
     73   DISALLOW_COPY_AND_ASSIGN(ExtensionBookmarkEventRouter);
     74 };
     75 
     76 class BookmarksFunction : public AsyncExtensionFunction,
     77                           public NotificationObserver {
     78  public:
     79   // AsyncExtensionFunction:
     80   virtual void Run() OVERRIDE;
     81 
     82   virtual bool RunImpl() = 0;
     83 
     84  protected:
     85   // Helper to get the bookmark id as int64 from the given string id.
     86   // Sets error_ to an error string if the given id string can't be parsed
     87   // as an int64. In case of error, doesn't change id and returns false.
     88   bool GetBookmarkIdAsInt64(const std::string& id_string, int64* id);
     89 
     90   // Helper that checks if bookmark editing is enabled. If it's not, this sets
     91   // error_ to the appropriate error string.
     92   bool EditBookmarksEnabled();
     93 
     94  private:
     95   // NotificationObserver:
     96   virtual void Observe(NotificationType type,
     97                        const NotificationSource& source,
     98                        const NotificationDetails& details) OVERRIDE;
     99 
    100   NotificationRegistrar registrar_;
    101 };
    102 
    103 class GetBookmarksFunction : public BookmarksFunction {
    104  public:
    105   virtual bool RunImpl() OVERRIDE;
    106 
    107  private:
    108   DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.get")
    109 };
    110 
    111 class GetBookmarkChildrenFunction : public BookmarksFunction {
    112  public:
    113   virtual bool RunImpl() OVERRIDE;
    114 
    115  private:
    116   DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.getChildren")
    117 };
    118 
    119 class GetBookmarkRecentFunction : public BookmarksFunction {
    120  public:
    121   virtual bool RunImpl() OVERRIDE;
    122 
    123  private:
    124   DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.getRecent")
    125 };
    126 
    127 class GetBookmarkTreeFunction : public BookmarksFunction {
    128  public:
    129   virtual bool RunImpl() OVERRIDE;
    130 
    131  private:
    132   DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.getTree")
    133 };
    134 
    135 class SearchBookmarksFunction : public BookmarksFunction {
    136  public:
    137   virtual bool RunImpl() OVERRIDE;
    138 
    139  private:
    140   DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.search")
    141 };
    142 
    143 class RemoveBookmarkFunction : public BookmarksFunction {
    144  public:
    145   // Returns true on successful parse and sets invalid_id to true if conversion
    146   // from id string to int64 failed.
    147   static bool ExtractIds(const ListValue* args, std::list<int64>* ids,
    148                          bool* invalid_id);
    149   // BookmarksFunction:
    150   virtual bool RunImpl() OVERRIDE;
    151   virtual void GetQuotaLimitHeuristics(
    152       std::list<QuotaLimitHeuristic*>* heuristics) const;
    153 
    154  private:
    155   DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.remove")
    156 };
    157 
    158 class RemoveTreeBookmarkFunction : public RemoveBookmarkFunction {
    159   DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.removeTree")
    160 };
    161 
    162 class CreateBookmarkFunction : public BookmarksFunction {
    163  public:
    164   virtual void GetQuotaLimitHeuristics(
    165       std::list<QuotaLimitHeuristic*>* heuristics) const;
    166   // BookmarksFunction:
    167   virtual bool RunImpl() OVERRIDE;
    168 
    169  private:
    170   DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.create")
    171 };
    172 
    173 class MoveBookmarkFunction : public BookmarksFunction {
    174  public:
    175   static bool ExtractIds(const ListValue* args, std::list<int64>* ids,
    176                          bool* invalid_id);
    177   virtual void GetQuotaLimitHeuristics(
    178       std::list<QuotaLimitHeuristic*>* heuristics) const;
    179   // BookmarksFunction:
    180   virtual bool RunImpl() OVERRIDE;
    181 
    182  private:
    183   DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.move")
    184 };
    185 
    186 class UpdateBookmarkFunction : public BookmarksFunction {
    187  public:
    188   static bool ExtractIds(const ListValue* args, std::list<int64>* ids,
    189                          bool* invalid_id);
    190   virtual void GetQuotaLimitHeuristics(
    191       std::list<QuotaLimitHeuristic*>* heuristics) const;
    192   virtual bool RunImpl();
    193  private:
    194   DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.update")
    195 };
    196 
    197 class BookmarksIOFunction : public BookmarksFunction,
    198                             public SelectFileDialog::Listener {
    199  public:
    200   BookmarksIOFunction();
    201   virtual ~BookmarksIOFunction();
    202 
    203   virtual void FileSelected(const FilePath& path, int index, void* params) = 0;
    204 
    205   // SelectFileDialog::Listener:
    206   virtual void MultiFilesSelected(const std::vector<FilePath>& files,
    207                                   void* params) OVERRIDE;
    208   virtual void FileSelectionCanceled(void* params) OVERRIDE;
    209 
    210   void SelectFile(SelectFileDialog::Type type);
    211 
    212  private:
    213   void ShowSelectFileDialog(SelectFileDialog::Type type, FilePath default_path);
    214 
    215  protected:
    216   scoped_refptr<SelectFileDialog> select_file_dialog_;
    217 };
    218 
    219 class ImportBookmarksFunction : public BookmarksIOFunction {
    220  public:
    221   // BookmarkManagerIOFunction:
    222   virtual bool RunImpl() OVERRIDE;
    223   virtual void FileSelected(const FilePath& path, int index, void* params)
    224       OVERRIDE;
    225 
    226  private:
    227   DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.import");
    228 };
    229 
    230 class ExportBookmarksFunction : public BookmarksIOFunction {
    231  public:
    232   // BookmarkManagerIOFunction:
    233   virtual bool RunImpl() OVERRIDE;
    234   virtual void FileSelected(const FilePath& path, int index, void* params)
    235       OVERRIDE;
    236 
    237  private:
    238   DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.export");
    239 };
    240 
    241 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_BOOKMARKS_MODULE_H_
    242