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/chrome_extension_function.h" 12 #include "chrome/browser/extensions/extension_install_prompt.h" 13 #include "chrome/browser/extensions/extension_uninstall_dialog.h" 14 #include "chrome/browser/extensions/pack_extension_job.h" 15 #include "chrome/browser/extensions/requirements_checker.h" 16 #include "components/browser_context_keyed_service/browser_context_keyed_service.h" 17 #include "content/public/browser/notification_observer.h" 18 #include "content/public/browser/notification_registrar.h" 19 #include "content/public/browser/render_view_host.h" 20 #include "extensions/browser/event_router.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 ChromeSyncExtensionFunction { 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 132 : public ChromeAsyncExtensionFunction { 133 public: 134 DECLARE_EXTENSION_FUNCTION("developerPrivate.getItemsInfo", 135 DEVELOPERPRIVATE_GETITEMSINFO) 136 137 protected: 138 virtual ~DeveloperPrivateGetItemsInfoFunction(); 139 140 // ExtensionFunction: 141 virtual bool RunImpl() OVERRIDE; 142 143 private: 144 145 scoped_ptr<developer::ItemInfo> CreateItemInfo( 146 const extensions::Extension& item, 147 bool item_is_enabled); 148 149 void GetIconsOnFileThread( 150 ItemInfoList item_list, 151 std::map<std::string, ExtensionResource> itemIdToIconResourceMap); 152 153 // Helper that lists the current inspectable html pages for the extension. 154 void GetInspectablePagesForExtensionProcess( 155 const Extension* extension, 156 const std::set<content::RenderViewHost*>& views, 157 ItemInspectViewList* result); 158 159 ItemInspectViewList GetInspectablePagesForExtension( 160 const extensions::Extension* extension, 161 bool extension_is_enabled); 162 163 void GetShellWindowPagesForExtensionProfile( 164 const extensions::Extension* extension, 165 ItemInspectViewList* result); 166 167 linked_ptr<developer::ItemInspectView> constructInspectView( 168 const GURL& url, 169 int render_process_id, 170 int render_view_id, 171 bool incognito, 172 bool generated_background_page); 173 }; 174 175 class DeveloperPrivateInspectFunction : public ChromeSyncExtensionFunction { 176 public: 177 DECLARE_EXTENSION_FUNCTION("developerPrivate.inspect", 178 DEVELOPERPRIVATE_INSPECT) 179 180 protected: 181 virtual ~DeveloperPrivateInspectFunction(); 182 183 // ExtensionFunction: 184 virtual bool RunImpl() OVERRIDE; 185 }; 186 187 class DeveloperPrivateAllowFileAccessFunction 188 : public ChromeSyncExtensionFunction { 189 public: 190 DECLARE_EXTENSION_FUNCTION("developerPrivate.allowFileAccess", 191 DEVELOPERPRIVATE_ALLOWFILEACCESS); 192 193 protected: 194 virtual ~DeveloperPrivateAllowFileAccessFunction(); 195 196 // ExtensionFunction: 197 virtual bool RunImpl() OVERRIDE; 198 }; 199 200 class DeveloperPrivateAllowIncognitoFunction 201 : public ChromeSyncExtensionFunction { 202 public: 203 DECLARE_EXTENSION_FUNCTION("developerPrivate.allowIncognito", 204 DEVELOPERPRIVATE_ALLOWINCOGNITO); 205 206 protected: 207 virtual ~DeveloperPrivateAllowIncognitoFunction(); 208 209 // ExtensionFunction: 210 virtual bool RunImpl() OVERRIDE; 211 }; 212 213 class DeveloperPrivateReloadFunction : public ChromeSyncExtensionFunction { 214 public: 215 DECLARE_EXTENSION_FUNCTION("developerPrivate.reload", 216 DEVELOPERPRIVATE_RELOAD); 217 218 protected: 219 virtual ~DeveloperPrivateReloadFunction(); 220 221 // ExtensionFunction: 222 virtual bool RunImpl() OVERRIDE; 223 }; 224 225 class DeveloperPrivateShowPermissionsDialogFunction 226 : public ChromeSyncExtensionFunction, 227 public ExtensionInstallPrompt::Delegate { 228 public: 229 DECLARE_EXTENSION_FUNCTION("developerPrivate.showPermissionsDialog", 230 DEVELOPERPRIVATE_PERMISSIONS); 231 232 DeveloperPrivateShowPermissionsDialogFunction(); 233 protected: 234 virtual ~DeveloperPrivateShowPermissionsDialogFunction(); 235 236 // ExtensionFunction: 237 virtual bool RunImpl() OVERRIDE; 238 239 // Overridden from ExtensionInstallPrompt::Delegate 240 virtual void InstallUIProceed() OVERRIDE; 241 virtual void InstallUIAbort(bool user_initiated) OVERRIDE; 242 243 scoped_ptr<ExtensionInstallPrompt> prompt_; 244 std::string extension_id_; 245 246 }; 247 248 class DeveloperPrivateEnableFunction 249 : public ChromeSyncExtensionFunction, 250 public base::SupportsWeakPtr<DeveloperPrivateEnableFunction> { 251 public: 252 DECLARE_EXTENSION_FUNCTION("developerPrivate.enable", 253 DEVELOPERPRIVATE_ENABLE); 254 255 DeveloperPrivateEnableFunction(); 256 257 protected: 258 virtual ~DeveloperPrivateEnableFunction(); 259 260 // Callback for requirements checker. 261 void OnRequirementsChecked(std::string extension_id, 262 std::vector<std::string> requirements_errors); 263 // ExtensionFunction: 264 virtual bool RunImpl() OVERRIDE; 265 266 private: 267 scoped_ptr<extensions::RequirementsChecker> requirements_checker_; 268 }; 269 270 class DeveloperPrivateChooseEntryFunction : public ChromeAsyncExtensionFunction, 271 public EntryPickerClient { 272 protected: 273 virtual ~DeveloperPrivateChooseEntryFunction(); 274 virtual bool RunImpl() OVERRIDE; 275 bool ShowPicker(ui::SelectFileDialog::Type picker_type, 276 const base::FilePath& last_directory, 277 const base::string16& select_title, 278 const ui::SelectFileDialog::FileTypeInfo& info, 279 int file_type_index); 280 281 // EntryPickerClient functions. 282 virtual void FileSelected(const base::FilePath& path) = 0; 283 virtual void FileSelectionCanceled() = 0; 284 }; 285 286 287 class DeveloperPrivateLoadUnpackedFunction 288 : public DeveloperPrivateChooseEntryFunction { 289 public: 290 DECLARE_EXTENSION_FUNCTION("developerPrivate.loadUnpacked", 291 DEVELOPERPRIVATE_LOADUNPACKED); 292 293 protected: 294 virtual ~DeveloperPrivateLoadUnpackedFunction(); 295 virtual bool RunImpl() OVERRIDE; 296 297 // EntryPickerCLient implementation. 298 virtual void FileSelected(const base::FilePath& path) OVERRIDE; 299 virtual void FileSelectionCanceled() OVERRIDE; 300 }; 301 302 class DeveloperPrivateChoosePathFunction 303 : public DeveloperPrivateChooseEntryFunction { 304 public: 305 DECLARE_EXTENSION_FUNCTION("developerPrivate.choosePath", 306 DEVELOPERPRIVATE_CHOOSEPATH); 307 308 protected: 309 virtual ~DeveloperPrivateChoosePathFunction(); 310 virtual bool RunImpl() OVERRIDE; 311 312 // EntryPickerClient functions. 313 virtual void FileSelected(const base::FilePath& path) OVERRIDE; 314 virtual void FileSelectionCanceled() OVERRIDE; 315 }; 316 317 class DeveloperPrivatePackDirectoryFunction 318 : public ChromeAsyncExtensionFunction, 319 public extensions::PackExtensionJob::Client { 320 321 public: 322 DECLARE_EXTENSION_FUNCTION("developerPrivate.packDirectory", 323 DEVELOPERPRIVATE_PACKDIRECTORY); 324 325 DeveloperPrivatePackDirectoryFunction(); 326 327 // ExtensionPackJob::Client implementation. 328 virtual void OnPackSuccess(const base::FilePath& crx_file, 329 const base::FilePath& key_file) OVERRIDE; 330 virtual void OnPackFailure( 331 const std::string& error, 332 extensions::ExtensionCreator::ErrorType error_type) OVERRIDE; 333 334 protected: 335 virtual ~DeveloperPrivatePackDirectoryFunction(); 336 virtual bool RunImpl() OVERRIDE; 337 338 private: 339 scoped_refptr<extensions::PackExtensionJob> pack_job_; 340 std::string item_path_str_; 341 std::string key_path_str_; 342 }; 343 344 class DeveloperPrivateGetStringsFunction : public ChromeSyncExtensionFunction { 345 public: 346 DECLARE_EXTENSION_FUNCTION("developerPrivate.getStrings", 347 DEVELOPERPRIVATE_GETSTRINGS); 348 349 protected: 350 virtual ~DeveloperPrivateGetStringsFunction(); 351 352 // ExtensionFunction 353 virtual bool RunImpl() OVERRIDE; 354 }; 355 356 class DeveloperPrivateIsProfileManagedFunction 357 : public ChromeSyncExtensionFunction { 358 public: 359 DECLARE_EXTENSION_FUNCTION("developerPrivate.isProfileManaged", 360 DEVELOPERPRIVATE_ISPROFILEMANAGED); 361 362 protected: 363 virtual ~DeveloperPrivateIsProfileManagedFunction(); 364 365 // ExtensionFunction 366 virtual bool RunImpl() OVERRIDE; 367 }; 368 369 class DeveloperPrivateExportSyncfsFolderToLocalfsFunction 370 : public ChromeAsyncExtensionFunction { 371 public: 372 DECLARE_EXTENSION_FUNCTION("developerPrivate.exportSyncfsFolderToLocalfs", 373 DEVELOPERPRIVATE_LOADUNPACKEDCROS); 374 375 DeveloperPrivateExportSyncfsFolderToLocalfsFunction(); 376 377 protected: 378 virtual ~DeveloperPrivateExportSyncfsFolderToLocalfsFunction(); 379 380 // ExtensionFunction 381 virtual bool RunImpl() OVERRIDE; 382 383 void ClearPrexistingDirectoryContent(const base::FilePath& project_path); 384 385 void ReadSyncFileSystemDirectory(const base::FilePath& project_path, 386 const base::FilePath& destination_path); 387 388 void ReadSyncFileSystemDirectoryCb( 389 const base::FilePath& project_path, 390 const base::FilePath& destination_path, 391 base::PlatformFileError result, 392 const fileapi::FileSystemOperation::FileEntryList& file_list, 393 bool has_more); 394 395 void SnapshotFileCallback( 396 const base::FilePath& target_path, 397 base::PlatformFileError result, 398 const base::PlatformFileInfo& file_info, 399 const base::FilePath& platform_path, 400 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref); 401 402 void CopyFile(const base::FilePath& src_path, 403 const base::FilePath& dest_path); 404 405 scoped_refptr<fileapi::FileSystemContext> context_; 406 407 private: 408 int pendingCopyOperationsCount_; 409 410 // This is set to false if any of the copyFile operations fail on 411 // call of the API. It is returned as a response of the API call. 412 bool success_; 413 }; 414 415 class DeveloperPrivateLoadProjectFunction 416 : public ChromeAsyncExtensionFunction { 417 public: 418 DECLARE_EXTENSION_FUNCTION("developerPrivate.loadProject", 419 DEVELOPERPRIVATE_LOADPROJECT); 420 421 DeveloperPrivateLoadProjectFunction(); 422 423 protected: 424 virtual ~DeveloperPrivateLoadProjectFunction(); 425 426 // ExtensionFunction 427 virtual bool RunImpl() OVERRIDE; 428 429 void GetUnpackedExtension(const base::FilePath& path, 430 const ExtensionSet* extensions); 431 }; 432 433 } // namespace api 434 435 } // namespace extensions 436 437 #endif // CHROME_BROWSER_EXTENSIONS_API_DEVELOPER_PRIVATE_DEVELOPER_PRIVATE_API_H_ 438