Home | History | Annotate | Download | only in developer_private
      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_DEVELOPER_PRIVATE_DEVELOPER_PRIVATE_API_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_DEVELOPER_PRIVATE_DEVELOPER_PRIVATE_API_H_
      7 
      8 #include "base/platform_file.h"
      9 #include "chrome/browser/extensions/api/developer_private/entry_picker.h"
     10 #include "chrome/browser/extensions/api/file_system/file_system_api.h"
     11 #include "chrome/browser/extensions/event_router.h"
     12 #include "chrome/browser/extensions/extension_function.h"
     13 #include "chrome/browser/extensions/extension_install_prompt.h"
     14 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
     15 #include "chrome/browser/extensions/pack_extension_job.h"
     16 #include "chrome/browser/extensions/requirements_checker.h"
     17 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
     18 #include "content/public/browser/notification_observer.h"
     19 #include "content/public/browser/notification_registrar.h"
     20 #include "content/public/browser/render_view_host.h"
     21 #include "ui/shell_dialogs/select_file_dialog.h"
     22 #include "webkit/browser/fileapi/file_system_context.h"
     23 #include "webkit/browser/fileapi/file_system_operation.h"
     24 
     25 class ExtensionService;
     26 
     27 namespace extensions {
     28 
     29 class ExtensionSystem;
     30 class ManagementPolicy;
     31 
     32 namespace api {
     33 
     34 class EntryPicker;
     35 class EntryPickerClient;
     36 
     37 namespace developer_private {
     38 
     39 struct ItemInfo;
     40 struct ItemInspectView;
     41 struct ProjectInfo;
     42 
     43 }
     44 
     45 }  // namespace api
     46 
     47 }  // namespace extensions
     48 
     49 namespace developer = extensions::api::developer_private;
     50 
     51 typedef std::vector<linked_ptr<developer::ItemInfo> > ItemInfoList;
     52 typedef std::vector<linked_ptr<developer::ProjectInfo> > ProjectInfoList;
     53 typedef std::vector<linked_ptr<developer::ItemInspectView> >
     54     ItemInspectViewList;
     55 
     56 namespace extensions {
     57 
     58 class DeveloperPrivateEventRouter : public content::NotificationObserver {
     59  public:
     60   explicit DeveloperPrivateEventRouter(Profile* profile);
     61   virtual ~DeveloperPrivateEventRouter();
     62 
     63  private:
     64   // content::NotificationObserver implementation
     65   virtual void Observe(int type,
     66                        const content::NotificationSource& source,
     67                        const content::NotificationDetails& details) OVERRIDE;
     68 
     69   content::NotificationRegistrar registrar_;
     70 
     71   Profile* profile_;
     72 
     73   DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateEventRouter);
     74 };
     75 
     76 // The profile-keyed service that manages the DeveloperPrivate API.
     77 class DeveloperPrivateAPI : public BrowserContextKeyedService,
     78                             public extensions::EventRouter::Observer {
     79  public:
     80   // Convenience method to get the DeveloperPrivateAPI for a profile.
     81   static DeveloperPrivateAPI* Get(Profile* profile);
     82 
     83   explicit DeveloperPrivateAPI(Profile* profile);
     84   virtual ~DeveloperPrivateAPI();
     85 
     86   void SetLastUnpackedDirectory(const base::FilePath& path);
     87 
     88   base::FilePath& GetLastUnpackedDirectory() {
     89     return last_unpacked_directory_;
     90   }
     91 
     92   // BrowserContextKeyedService implementation
     93   virtual void Shutdown() OVERRIDE;
     94 
     95   // EventRouter::Observer implementation.
     96   virtual void OnListenerAdded(const extensions::EventListenerInfo& details)
     97       OVERRIDE;
     98   virtual void OnListenerRemoved(const extensions::EventListenerInfo& details)
     99       OVERRIDE;
    100 
    101  private:
    102   void RegisterNotifications();
    103 
    104   Profile* profile_;
    105 
    106   // Used to start the load |load_extension_dialog_| in the last directory that
    107   // was loaded.
    108   base::FilePath last_unpacked_directory_;
    109 
    110   // Created lazily upon OnListenerAdded.
    111   scoped_ptr<DeveloperPrivateEventRouter> developer_private_event_router_;
    112 
    113   DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateAPI);
    114 
    115 };
    116 
    117 namespace api {
    118 
    119 class DeveloperPrivateAutoUpdateFunction : public SyncExtensionFunction {
    120  public:
    121   DECLARE_EXTENSION_FUNCTION("developerPrivate.autoUpdate",
    122                              DEVELOPERPRIVATE_AUTOUPDATE)
    123 
    124  protected:
    125   virtual ~DeveloperPrivateAutoUpdateFunction();
    126 
    127   // ExtensionFunction:
    128   virtual bool RunImpl() OVERRIDE;
    129 };
    130 
    131 class DeveloperPrivateGetItemsInfoFunction : public AsyncExtensionFunction {
    132  public:
    133   DECLARE_EXTENSION_FUNCTION("developerPrivate.getItemsInfo",
    134                              DEVELOPERPRIVATE_GETITEMSINFO)
    135 
    136  protected:
    137   virtual ~DeveloperPrivateGetItemsInfoFunction();
    138 
    139   // ExtensionFunction:
    140   virtual bool RunImpl() OVERRIDE;
    141 
    142  private:
    143 
    144   scoped_ptr<developer::ItemInfo> CreateItemInfo(
    145       const extensions::Extension& item,
    146       bool item_is_enabled);
    147 
    148   void GetIconsOnFileThread(
    149       ItemInfoList item_list,
    150       std::map<std::string, ExtensionResource> itemIdToIconResourceMap);
    151 
    152   // Helper that lists the current inspectable html pages for the extension.
    153   void GetInspectablePagesForExtensionProcess(
    154       const std::set<content::RenderViewHost*>& views,
    155       ItemInspectViewList* result);
    156 
    157   ItemInspectViewList GetInspectablePagesForExtension(
    158       const extensions::Extension* extension,
    159       bool extension_is_enabled);
    160 
    161   void GetShellWindowPagesForExtensionProfile(
    162       const extensions::Extension* extension,
    163       ItemInspectViewList* result);
    164 
    165   linked_ptr<developer::ItemInspectView> constructInspectView(
    166       const GURL& url,
    167       int render_process_id,
    168       int render_view_id,
    169       bool incognito);
    170 };
    171 
    172 class DeveloperPrivateInspectFunction : public SyncExtensionFunction {
    173  public:
    174   DECLARE_EXTENSION_FUNCTION("developerPrivate.inspect",
    175                              DEVELOPERPRIVATE_INSPECT)
    176 
    177  protected:
    178   virtual ~DeveloperPrivateInspectFunction();
    179 
    180   // ExtensionFunction:
    181   virtual bool RunImpl() OVERRIDE;
    182 };
    183 
    184 class DeveloperPrivateAllowFileAccessFunction : public SyncExtensionFunction {
    185  public:
    186   DECLARE_EXTENSION_FUNCTION("developerPrivate.allowFileAccess",
    187                              DEVELOPERPRIVATE_ALLOWFILEACCESS);
    188 
    189  protected:
    190   virtual ~DeveloperPrivateAllowFileAccessFunction();
    191 
    192   // ExtensionFunction:
    193   virtual bool RunImpl() OVERRIDE;
    194 };
    195 
    196 class DeveloperPrivateAllowIncognitoFunction : public SyncExtensionFunction {
    197  public:
    198   DECLARE_EXTENSION_FUNCTION("developerPrivate.allowIncognito",
    199                              DEVELOPERPRIVATE_ALLOWINCOGNITO);
    200 
    201  protected:
    202   virtual ~DeveloperPrivateAllowIncognitoFunction();
    203 
    204   // ExtensionFunction:
    205   virtual bool RunImpl() OVERRIDE;
    206 };
    207 
    208 class DeveloperPrivateReloadFunction : public SyncExtensionFunction {
    209  public:
    210   DECLARE_EXTENSION_FUNCTION("developerPrivate.reload",
    211                              DEVELOPERPRIVATE_RELOAD);
    212 
    213  protected:
    214   virtual ~DeveloperPrivateReloadFunction();
    215 
    216   // ExtensionFunction:
    217   virtual bool RunImpl() OVERRIDE;
    218 };
    219 
    220 class DeveloperPrivateShowPermissionsDialogFunction
    221     : public SyncExtensionFunction,
    222       public ExtensionInstallPrompt::Delegate {
    223  public:
    224   DECLARE_EXTENSION_FUNCTION("developerPrivate.showPermissionsDialog",
    225                              DEVELOPERPRIVATE_PERMISSIONS);
    226 
    227   DeveloperPrivateShowPermissionsDialogFunction();
    228  protected:
    229   virtual ~DeveloperPrivateShowPermissionsDialogFunction();
    230 
    231   // ExtensionFunction:
    232   virtual bool RunImpl() OVERRIDE;
    233 
    234   // Overridden from ExtensionInstallPrompt::Delegate
    235   virtual void InstallUIProceed() OVERRIDE;
    236   virtual void InstallUIAbort(bool user_initiated) OVERRIDE;
    237 
    238   scoped_ptr<ExtensionInstallPrompt> prompt_;
    239   std::string extension_id_;
    240 
    241 };
    242 
    243 class DeveloperPrivateRestartFunction : public SyncExtensionFunction {
    244  public:
    245   DECLARE_EXTENSION_FUNCTION("developerPrivate.restart",
    246                              DEVELOPERPRIVATE_RESTART);
    247 
    248  protected:
    249   virtual ~DeveloperPrivateRestartFunction();
    250 
    251   // ExtensionFunction:
    252   virtual bool RunImpl() OVERRIDE;
    253 };
    254 
    255 class DeveloperPrivateEnableFunction
    256     : public SyncExtensionFunction,
    257       public base::SupportsWeakPtr<DeveloperPrivateEnableFunction> {
    258  public:
    259   DECLARE_EXTENSION_FUNCTION("developerPrivate.enable",
    260                              DEVELOPERPRIVATE_ENABLE);
    261 
    262   DeveloperPrivateEnableFunction();
    263 
    264  protected:
    265   virtual ~DeveloperPrivateEnableFunction();
    266 
    267   // Callback for requirements checker.
    268   void OnRequirementsChecked(std::string extension_id,
    269                              std::vector<std::string> requirements_errors);
    270   // ExtensionFunction:
    271   virtual bool RunImpl() OVERRIDE;
    272 
    273  private:
    274   scoped_ptr<extensions::RequirementsChecker> requirements_checker_;
    275 };
    276 
    277 class DeveloperPrivateChooseEntryFunction : public AsyncExtensionFunction,
    278                                             public EntryPickerClient {
    279  protected:
    280   virtual ~DeveloperPrivateChooseEntryFunction();
    281   virtual bool RunImpl() OVERRIDE;
    282   bool ShowPicker(ui::SelectFileDialog::Type picker_type,
    283                   const base::FilePath& last_directory,
    284                   const string16& select_title,
    285                   const ui::SelectFileDialog::FileTypeInfo& info,
    286                   int file_type_index);
    287 
    288   // EntryPickerClient functions.
    289   virtual void FileSelected(const base::FilePath& path) = 0;
    290   virtual void FileSelectionCanceled() = 0;
    291 };
    292 
    293 
    294 class DeveloperPrivateLoadUnpackedFunction
    295     : public DeveloperPrivateChooseEntryFunction {
    296  public:
    297   DECLARE_EXTENSION_FUNCTION("developerPrivate.loadUnpacked",
    298                              DEVELOPERPRIVATE_LOADUNPACKED);
    299 
    300  protected:
    301   virtual ~DeveloperPrivateLoadUnpackedFunction();
    302   virtual bool RunImpl() OVERRIDE;
    303 
    304   // EntryPickerCLient implementation.
    305   virtual void FileSelected(const base::FilePath& path) OVERRIDE;
    306   virtual void FileSelectionCanceled() OVERRIDE;
    307 };
    308 
    309 class DeveloperPrivateChoosePathFunction
    310     : public DeveloperPrivateChooseEntryFunction {
    311  public:
    312   DECLARE_EXTENSION_FUNCTION("developerPrivate.choosePath",
    313                              DEVELOPERPRIVATE_CHOOSEPATH);
    314 
    315  protected:
    316   virtual ~DeveloperPrivateChoosePathFunction();
    317   virtual bool RunImpl() OVERRIDE;
    318 
    319   // EntryPickerClient functions.
    320   virtual void FileSelected(const base::FilePath& path) OVERRIDE;
    321   virtual void FileSelectionCanceled() OVERRIDE;
    322 };
    323 
    324 class DeveloperPrivatePackDirectoryFunction
    325     : public AsyncExtensionFunction,
    326       public extensions::PackExtensionJob::Client {
    327 
    328  public:
    329   DECLARE_EXTENSION_FUNCTION("developerPrivate.packDirectory",
    330                              DEVELOPERPRIVATE_PACKDIRECTORY);
    331 
    332   DeveloperPrivatePackDirectoryFunction();
    333 
    334   // ExtensionPackJob::Client implementation.
    335   virtual void OnPackSuccess(const base::FilePath& crx_file,
    336                              const base::FilePath& key_file) OVERRIDE;
    337   virtual void OnPackFailure(
    338       const std::string& error,
    339       extensions::ExtensionCreator::ErrorType error_type) OVERRIDE;
    340 
    341  protected:
    342   virtual ~DeveloperPrivatePackDirectoryFunction();
    343   virtual bool RunImpl() OVERRIDE;
    344 
    345  private:
    346   scoped_refptr<extensions::PackExtensionJob> pack_job_;
    347   std::string item_path_str_;
    348   std::string key_path_str_;
    349 };
    350 
    351 class DeveloperPrivateGetStringsFunction : public SyncExtensionFunction {
    352   public:
    353    DECLARE_EXTENSION_FUNCTION("developerPrivate.getStrings",
    354                               DEVELOPERPRIVATE_GETSTRINGS);
    355 
    356   protected:
    357    virtual ~DeveloperPrivateGetStringsFunction();
    358 
    359    // ExtensionFunction
    360    virtual bool RunImpl() OVERRIDE;
    361 };
    362 
    363 class DeveloperPrivateExportSyncfsFolderToLocalfsFunction
    364     : public AsyncExtensionFunction {
    365   public:
    366    DECLARE_EXTENSION_FUNCTION("developerPrivate.exportSyncfsFolderToLocalfs",
    367                               DEVELOPERPRIVATE_LOADUNPACKEDCROS);
    368 
    369    DeveloperPrivateExportSyncfsFolderToLocalfsFunction();
    370 
    371   protected:
    372    virtual ~DeveloperPrivateExportSyncfsFolderToLocalfsFunction();
    373 
    374    // ExtensionFunction
    375    virtual bool RunImpl() OVERRIDE;
    376 
    377    void ClearPrexistingDirectoryContent(const base::FilePath& project_path);
    378 
    379    void ReadSyncFileSystemDirectory(const base::FilePath& project_path,
    380                                     const base::FilePath& destination_path);
    381 
    382    void ReadSyncFileSystemDirectoryCb(
    383        const base::FilePath& project_path,
    384        const base::FilePath& destination_path,
    385        base::PlatformFileError result,
    386        const fileapi::FileSystemOperation::FileEntryList& file_list,
    387        bool has_more);
    388 
    389    void SnapshotFileCallback(
    390        const base::FilePath& target_path,
    391        base::PlatformFileError result,
    392        const base::PlatformFileInfo& file_info,
    393        const base::FilePath& platform_path,
    394        const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref);
    395 
    396    void CopyFile(const base::FilePath& src_path,
    397                  const base::FilePath& dest_path);
    398 
    399    scoped_refptr<fileapi::FileSystemContext> context_;
    400 
    401   private:
    402    int pendingCopyOperationsCount_;
    403 
    404    // This is set to false if any of the copyFile operations fail on
    405    // call of the API. It is returned as a response of the API call.
    406    bool success_;
    407 };
    408 
    409 class DeveloperPrivateLoadProjectFunction : public AsyncExtensionFunction {
    410   public:
    411    DECLARE_EXTENSION_FUNCTION("developerPrivate.loadProject",
    412                               DEVELOPERPRIVATE_LOADPROJECT);
    413 
    414    DeveloperPrivateLoadProjectFunction();
    415 
    416   protected:
    417    virtual ~DeveloperPrivateLoadProjectFunction();
    418 
    419    // ExtensionFunction
    420    virtual bool RunImpl() OVERRIDE;
    421 
    422    void GetUnpackedExtension(const base::FilePath& path,
    423                              const ExtensionSet* extensions);
    424 };
    425 
    426 }  // namespace api
    427 
    428 }  // namespace extensions
    429 
    430 #endif  // CHROME_BROWSER_EXTENSIONS_API_DEVELOPER_PRIVATE_DEVELOPER_PRIVATE_API_H_
    431