Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / developer_private / developer_private_api.h
blob41fe433c1c7321a63bd11fd014d66784ad7a063e
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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DEVELOPER_PRIVATE_DEVELOPER_PRIVATE_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_DEVELOPER_PRIVATE_DEVELOPER_PRIVATE_API_H_
8 #include <set>
10 #include "base/files/file.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/prefs/pref_change_registrar.h"
13 #include "base/scoped_observer.h"
14 #include "chrome/browser/extensions/api/commands/command_service.h"
15 #include "chrome/browser/extensions/api/developer_private/entry_picker.h"
16 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
17 #include "chrome/browser/extensions/api/file_system/file_system_api.h"
18 #include "chrome/browser/extensions/chrome_extension_function.h"
19 #include "chrome/browser/extensions/error_console/error_console.h"
20 #include "chrome/browser/extensions/extension_management.h"
21 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
22 #include "chrome/browser/extensions/pack_extension_job.h"
23 #include "chrome/common/extensions/api/developer_private.h"
24 #include "chrome/common/extensions/webstore_install_result.h"
25 #include "extensions/browser/app_window/app_window_registry.h"
26 #include "extensions/browser/browser_context_keyed_api_factory.h"
27 #include "extensions/browser/event_router.h"
28 #include "extensions/browser/extension_prefs_observer.h"
29 #include "extensions/browser/extension_registry_observer.h"
30 #include "extensions/browser/process_manager_observer.h"
31 #include "extensions/browser/warning_service.h"
32 #include "storage/browser/fileapi/file_system_context.h"
33 #include "storage/browser/fileapi/file_system_operation.h"
34 #include "ui/shell_dialogs/select_file_dialog.h"
36 class Profile;
38 namespace extensions {
40 class EventRouter;
41 class ExtensionError;
42 class ExtensionInfoGenerator;
43 class ExtensionRegistry;
44 class ExtensionSystem;
45 class ManagementPolicy;
46 class ProcessManager;
47 class RequirementsChecker;
49 namespace api {
51 class EntryPicker;
52 class EntryPickerClient;
54 } // namespace api
56 class DeveloperPrivateEventRouter : public ExtensionRegistryObserver,
57 public ErrorConsole::Observer,
58 public ProcessManagerObserver,
59 public AppWindowRegistry::Observer,
60 public CommandService::Observer,
61 public ExtensionActionAPI::Observer,
62 public ExtensionPrefsObserver,
63 public ExtensionManagement::Observer,
64 public WarningService::Observer {
65 public:
66 explicit DeveloperPrivateEventRouter(Profile* profile);
67 ~DeveloperPrivateEventRouter() override;
69 // Add or remove an ID to the list of extensions subscribed to events.
70 void AddExtensionId(const std::string& extension_id);
71 void RemoveExtensionId(const std::string& extension_id);
73 private:
74 // ExtensionRegistryObserver:
75 void OnExtensionLoaded(content::BrowserContext* browser_context,
76 const Extension* extension) override;
77 void OnExtensionUnloaded(content::BrowserContext* browser_context,
78 const Extension* extension,
79 UnloadedExtensionInfo::Reason reason) override;
80 void OnExtensionInstalled(content::BrowserContext* browser_context,
81 const Extension* extension,
82 bool is_update) override;
83 void OnExtensionUninstalled(content::BrowserContext* browser_context,
84 const Extension* extension,
85 extensions::UninstallReason reason) override;
87 // ErrorConsole::Observer:
88 void OnErrorAdded(const ExtensionError* error) override;
89 void OnErrorsRemoved(const std::set<std::string>& extension_ids) override;
91 // ProcessManagerObserver:
92 void OnExtensionFrameRegistered(
93 const std::string& extension_id,
94 content::RenderFrameHost* render_frame_host) override;
95 void OnExtensionFrameUnregistered(
96 const std::string& extension_id,
97 content::RenderFrameHost* render_frame_host) override;
99 // AppWindowRegistry::Observer:
100 void OnAppWindowAdded(AppWindow* window) override;
101 void OnAppWindowRemoved(AppWindow* window) override;
103 // CommandService::Observer:
104 void OnExtensionCommandAdded(const std::string& extension_id,
105 const Command& added_command) override;
106 void OnExtensionCommandRemoved(const std::string& extension_id,
107 const Command& removed_command) override;
109 // ExtensionActionAPI::Observer:
110 void OnExtensionActionVisibilityChanged(const std::string& extension_id,
111 bool is_now_visible) override;
113 // ExtensionPrefsObserver:
114 void OnExtensionDisableReasonsChanged(const std::string& extension_id,
115 int disable_reasons) override;
117 // ExtensionManagement::Observer:
118 void OnExtensionManagementSettingsChanged() override;
120 // WarningService::Observer:
121 void ExtensionWarningsChanged(
122 const ExtensionIdSet& affected_extensions) override;
124 // Handles a profile preferance change.
125 void OnProfilePrefChanged();
127 // Broadcasts an event to all listeners.
128 void BroadcastItemStateChanged(api::developer_private::EventType event_type,
129 const std::string& id);
130 void BroadcastItemStateChangedHelper(
131 api::developer_private::EventType event_type,
132 const std::string& extension_id,
133 scoped_ptr<ExtensionInfoGenerator> info_generator,
134 const std::vector<linked_ptr<api::developer_private::ExtensionInfo>>&
135 infos);
137 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
138 extension_registry_observer_;
139 ScopedObserver<ErrorConsole, ErrorConsole::Observer> error_console_observer_;
140 ScopedObserver<ProcessManager, ProcessManagerObserver>
141 process_manager_observer_;
142 ScopedObserver<AppWindowRegistry, AppWindowRegistry::Observer>
143 app_window_registry_observer_;
144 ScopedObserver<ExtensionActionAPI, ExtensionActionAPI::Observer>
145 extension_action_api_observer_;
146 ScopedObserver<WarningService, WarningService::Observer>
147 warning_service_observer_;
148 ScopedObserver<ExtensionPrefs, ExtensionPrefsObserver>
149 extension_prefs_observer_;
150 ScopedObserver<ExtensionManagement, ExtensionManagement::Observer>
151 extension_management_observer_;
152 ScopedObserver<CommandService, CommandService::Observer>
153 command_service_observer_;
155 Profile* profile_;
157 EventRouter* event_router_;
159 // The set of IDs of the Extensions that have subscribed to DeveloperPrivate
160 // events. Since the only consumer of the DeveloperPrivate API is currently
161 // the Apps Developer Tool (which replaces the chrome://extensions page), we
162 // don't want to send information about the subscribing extension in an
163 // update. In particular, we want to avoid entering a loop, which could happen
164 // when, e.g., the Apps Developer Tool throws an error.
165 std::set<std::string> extension_ids_;
167 PrefChangeRegistrar pref_change_registrar_;
169 base::WeakPtrFactory<DeveloperPrivateEventRouter> weak_factory_;
171 DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateEventRouter);
174 // The profile-keyed service that manages the DeveloperPrivate API.
175 class DeveloperPrivateAPI : public BrowserContextKeyedAPI,
176 public EventRouter::Observer {
177 public:
178 static BrowserContextKeyedAPIFactory<DeveloperPrivateAPI>*
179 GetFactoryInstance();
181 // Convenience method to get the DeveloperPrivateAPI for a profile.
182 static DeveloperPrivateAPI* Get(content::BrowserContext* context);
184 explicit DeveloperPrivateAPI(content::BrowserContext* context);
185 ~DeveloperPrivateAPI() override;
187 void SetLastUnpackedDirectory(const base::FilePath& path);
189 base::FilePath& GetLastUnpackedDirectory() {
190 return last_unpacked_directory_;
193 // KeyedService implementation
194 void Shutdown() override;
196 // EventRouter::Observer implementation.
197 void OnListenerAdded(const EventListenerInfo& details) override;
198 void OnListenerRemoved(const EventListenerInfo& details) override;
200 DeveloperPrivateEventRouter* developer_private_event_router() {
201 return developer_private_event_router_.get();
204 private:
205 friend class BrowserContextKeyedAPIFactory<DeveloperPrivateAPI>;
207 // BrowserContextKeyedAPI implementation.
208 static const char* service_name() { return "DeveloperPrivateAPI"; }
209 static const bool kServiceRedirectedInIncognito = true;
210 static const bool kServiceIsNULLWhileTesting = true;
212 void RegisterNotifications();
214 Profile* profile_;
216 // Used to start the load |load_extension_dialog_| in the last directory that
217 // was loaded.
218 base::FilePath last_unpacked_directory_;
220 // Created lazily upon OnListenerAdded.
221 scoped_ptr<DeveloperPrivateEventRouter> developer_private_event_router_;
223 DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateAPI);
226 namespace api {
228 class DeveloperPrivateAPIFunction : public ChromeUIThreadExtensionFunction {
229 protected:
230 ~DeveloperPrivateAPIFunction() override;
232 // Returns the extension with the given |id| from the registry, including
233 // all possible extensions (enabled, disabled, terminated, etc).
234 const Extension* GetExtensionById(const std::string& id);
236 // Returns the extension with the given |id| from the registry, only checking
237 // enabled extensions.
238 const Extension* GetEnabledExtensionById(const std::string& id);
241 class DeveloperPrivateAutoUpdateFunction : public DeveloperPrivateAPIFunction {
242 public:
243 DECLARE_EXTENSION_FUNCTION("developerPrivate.autoUpdate",
244 DEVELOPERPRIVATE_AUTOUPDATE)
246 protected:
247 ~DeveloperPrivateAutoUpdateFunction() override;
248 ResponseAction Run() override;
251 class DeveloperPrivateGetItemsInfoFunction
252 : public DeveloperPrivateAPIFunction {
253 public:
254 DECLARE_EXTENSION_FUNCTION("developerPrivate.getItemsInfo",
255 DEVELOPERPRIVATE_GETITEMSINFO)
256 DeveloperPrivateGetItemsInfoFunction();
258 private:
259 ~DeveloperPrivateGetItemsInfoFunction() override;
260 ResponseAction Run() override;
262 void OnInfosGenerated(
263 const std::vector<linked_ptr<api::developer_private::ExtensionInfo>>&
264 infos);
266 scoped_ptr<ExtensionInfoGenerator> info_generator_;
268 DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateGetItemsInfoFunction);
271 class DeveloperPrivateGetExtensionsInfoFunction
272 : public DeveloperPrivateAPIFunction {
273 public:
274 DeveloperPrivateGetExtensionsInfoFunction();
275 DECLARE_EXTENSION_FUNCTION("developerPrivate.getExtensionsInfo",
276 DEVELOPERPRIVATE_GETEXTENSIONSINFO);
278 private:
279 ~DeveloperPrivateGetExtensionsInfoFunction() override;
280 ResponseAction Run() override;
282 void OnInfosGenerated(
283 const std::vector<linked_ptr<api::developer_private::ExtensionInfo>>&
284 infos);
286 scoped_ptr<ExtensionInfoGenerator> info_generator_;
288 DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateGetExtensionsInfoFunction);
291 class DeveloperPrivateGetExtensionInfoFunction
292 : public DeveloperPrivateAPIFunction {
293 public:
294 DeveloperPrivateGetExtensionInfoFunction();
295 DECLARE_EXTENSION_FUNCTION("developerPrivate.getExtensionInfo",
296 DEVELOPERPRIVATE_GETEXTENSIONINFO);
298 private:
299 ~DeveloperPrivateGetExtensionInfoFunction() override;
300 ResponseAction Run() override;
302 void OnInfosGenerated(
303 const std::vector<linked_ptr<api::developer_private::ExtensionInfo>>&
304 infos);
306 scoped_ptr<ExtensionInfoGenerator> info_generator_;
308 DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateGetExtensionInfoFunction);
311 class DeveloperPrivateGetProfileConfigurationFunction
312 : public DeveloperPrivateAPIFunction {
313 public:
314 DECLARE_EXTENSION_FUNCTION("developerPrivate.getProfileConfiguration",
315 DEVELOPERPRIVATE_GETPROFILECONFIGURATION);
317 private:
318 ~DeveloperPrivateGetProfileConfigurationFunction() override;
319 ResponseAction Run() override;
322 class DeveloperPrivateUpdateProfileConfigurationFunction
323 : public DeveloperPrivateAPIFunction {
324 public:
325 DECLARE_EXTENSION_FUNCTION("developerPrivate.updateProfileConfiguration",
326 DEVELOPERPRIVATE_UPDATEPROFILECONFIGURATION);
328 private:
329 ~DeveloperPrivateUpdateProfileConfigurationFunction() override;
330 ResponseAction Run() override;
333 class DeveloperPrivateUpdateExtensionConfigurationFunction
334 : public DeveloperPrivateAPIFunction {
335 public:
336 DECLARE_EXTENSION_FUNCTION("developerPrivate.updateExtensionConfiguration",
337 DEVELOPERPRIVATE_UPDATEEXTENSIONCONFIGURATION);
339 protected:
340 ~DeveloperPrivateUpdateExtensionConfigurationFunction() override;
341 ResponseAction Run() override;
344 class DeveloperPrivateReloadFunction : public DeveloperPrivateAPIFunction {
345 public:
346 DECLARE_EXTENSION_FUNCTION("developerPrivate.reload",
347 DEVELOPERPRIVATE_RELOAD);
349 protected:
350 ~DeveloperPrivateReloadFunction() override;
352 // ExtensionFunction:
353 ResponseAction Run() override;
356 class DeveloperPrivateShowPermissionsDialogFunction
357 : public DeveloperPrivateAPIFunction {
358 public:
359 DECLARE_EXTENSION_FUNCTION("developerPrivate.showPermissionsDialog",
360 DEVELOPERPRIVATE_PERMISSIONS);
361 DeveloperPrivateShowPermissionsDialogFunction();
363 protected:
364 // DeveloperPrivateAPIFunction:
365 ~DeveloperPrivateShowPermissionsDialogFunction() override;
366 ResponseAction Run() override;
368 void Finish();
370 DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateShowPermissionsDialogFunction);
373 class DeveloperPrivateChooseEntryFunction : public UIThreadExtensionFunction,
374 public EntryPickerClient {
375 protected:
376 ~DeveloperPrivateChooseEntryFunction() override;
377 bool ShowPicker(ui::SelectFileDialog::Type picker_type,
378 const base::string16& select_title,
379 const ui::SelectFileDialog::FileTypeInfo& info,
380 int file_type_index);
384 class DeveloperPrivateLoadUnpackedFunction
385 : public DeveloperPrivateChooseEntryFunction {
386 public:
387 DECLARE_EXTENSION_FUNCTION("developerPrivate.loadUnpacked",
388 DEVELOPERPRIVATE_LOADUNPACKED);
389 DeveloperPrivateLoadUnpackedFunction();
391 protected:
392 ~DeveloperPrivateLoadUnpackedFunction() override;
393 ResponseAction Run() override;
395 // EntryPickerClient:
396 void FileSelected(const base::FilePath& path) override;
397 void FileSelectionCanceled() override;
399 // Callback for the UnpackedLoader.
400 void OnLoadComplete(const Extension* extension,
401 const base::FilePath& file_path,
402 const std::string& error);
404 private:
405 // Whether or not we should fail quietly in the event of a load error.
406 bool fail_quietly_;
409 class DeveloperPrivateChoosePathFunction
410 : public DeveloperPrivateChooseEntryFunction {
411 public:
412 DECLARE_EXTENSION_FUNCTION("developerPrivate.choosePath",
413 DEVELOPERPRIVATE_CHOOSEPATH);
415 protected:
416 ~DeveloperPrivateChoosePathFunction() override;
417 ResponseAction Run() override;
419 // EntryPickerClient:
420 void FileSelected(const base::FilePath& path) override;
421 void FileSelectionCanceled() override;
424 class DeveloperPrivatePackDirectoryFunction
425 : public DeveloperPrivateAPIFunction,
426 public PackExtensionJob::Client {
428 public:
429 DECLARE_EXTENSION_FUNCTION("developerPrivate.packDirectory",
430 DEVELOPERPRIVATE_PACKDIRECTORY);
432 DeveloperPrivatePackDirectoryFunction();
434 // ExtensionPackJob::Client implementation.
435 void OnPackSuccess(const base::FilePath& crx_file,
436 const base::FilePath& key_file) override;
437 void OnPackFailure(const std::string& error,
438 ExtensionCreator::ErrorType error_type) override;
440 protected:
441 ~DeveloperPrivatePackDirectoryFunction() override;
442 ResponseAction Run() override;
444 private:
445 scoped_refptr<PackExtensionJob> pack_job_;
446 std::string item_path_str_;
447 std::string key_path_str_;
450 class DeveloperPrivateIsProfileManagedFunction
451 : public ChromeSyncExtensionFunction {
452 public:
453 DECLARE_EXTENSION_FUNCTION("developerPrivate.isProfileManaged",
454 DEVELOPERPRIVATE_ISPROFILEMANAGED);
456 protected:
457 ~DeveloperPrivateIsProfileManagedFunction() override;
459 // ExtensionFunction:
460 bool RunSync() override;
463 class DeveloperPrivateLoadDirectoryFunction
464 : public ChromeAsyncExtensionFunction {
465 public:
466 DECLARE_EXTENSION_FUNCTION("developerPrivate.loadDirectory",
467 DEVELOPERPRIVATE_LOADUNPACKEDCROS);
469 DeveloperPrivateLoadDirectoryFunction();
471 protected:
472 ~DeveloperPrivateLoadDirectoryFunction() override;
474 // ExtensionFunction:
475 bool RunAsync() override;
477 bool LoadByFileSystemAPI(const storage::FileSystemURL& directory_url);
479 void ClearExistingDirectoryContent(const base::FilePath& project_path);
481 void ReadDirectoryByFileSystemAPI(const base::FilePath& project_path,
482 const base::FilePath& destination_path);
484 void ReadDirectoryByFileSystemAPICb(
485 const base::FilePath& project_path,
486 const base::FilePath& destination_path,
487 base::File::Error result,
488 const storage::FileSystemOperation::FileEntryList& file_list,
489 bool has_more);
491 void SnapshotFileCallback(
492 const base::FilePath& target_path,
493 base::File::Error result,
494 const base::File::Info& file_info,
495 const base::FilePath& platform_path,
496 const scoped_refptr<storage::ShareableFileReference>& file_ref);
498 void CopyFile(const base::FilePath& src_path,
499 const base::FilePath& dest_path);
501 void Load();
503 scoped_refptr<storage::FileSystemContext> context_;
505 // syncfs url representing the root of the folder to be copied.
506 std::string project_base_url_;
508 // physical path on disc of the folder to be copied.
509 base::FilePath project_base_path_;
511 private:
512 int pending_copy_operations_count_;
514 // This is set to false if any of the copyFile operations fail on
515 // call of the API. It is returned as a response of the API call.
516 bool success_;
519 class DeveloperPrivateRequestFileSourceFunction
520 : public DeveloperPrivateAPIFunction {
521 public:
522 DECLARE_EXTENSION_FUNCTION("developerPrivate.requestFileSource",
523 DEVELOPERPRIVATE_REQUESTFILESOURCE);
524 DeveloperPrivateRequestFileSourceFunction();
526 protected:
527 ~DeveloperPrivateRequestFileSourceFunction() override;
528 ResponseAction Run() override;
530 private:
531 void Finish(const std::string& file_contents);
533 scoped_ptr<api::developer_private::RequestFileSource::Params> params_;
536 class DeveloperPrivateOpenDevToolsFunction
537 : public DeveloperPrivateAPIFunction {
538 public:
539 DECLARE_EXTENSION_FUNCTION("developerPrivate.openDevTools",
540 DEVELOPERPRIVATE_OPENDEVTOOLS);
541 DeveloperPrivateOpenDevToolsFunction();
543 protected:
544 ~DeveloperPrivateOpenDevToolsFunction() override;
545 ResponseAction Run() override;
548 class DeveloperPrivateDeleteExtensionErrorsFunction
549 : public DeveloperPrivateAPIFunction {
550 public:
551 DECLARE_EXTENSION_FUNCTION("developerPrivate.deleteExtensionErrors",
552 DEVELOPERPRIVATE_DELETEEXTENSIONERRORS);
554 protected:
555 ~DeveloperPrivateDeleteExtensionErrorsFunction() override;
556 ResponseAction Run() override;
559 class DeveloperPrivateRepairExtensionFunction
560 : public DeveloperPrivateAPIFunction {
561 public:
562 DECLARE_EXTENSION_FUNCTION("developerPrivate.repairExtension",
563 DEVELOPERPRIVATE_REPAIREXTENSION);
565 protected:
566 ~DeveloperPrivateRepairExtensionFunction() override;
567 ResponseAction Run() override;
569 void OnReinstallComplete(bool success,
570 const std::string& error,
571 webstore_install::Result result);
574 class DeveloperPrivateShowOptionsFunction : public DeveloperPrivateAPIFunction {
575 public:
576 DECLARE_EXTENSION_FUNCTION("developerPrivate.showOptions",
577 DEVELOPERPRIVATE_SHOWOPTIONS);
579 protected:
580 ~DeveloperPrivateShowOptionsFunction() override;
581 ResponseAction Run() override;
584 class DeveloperPrivateShowPathFunction : public DeveloperPrivateAPIFunction {
585 public:
586 DECLARE_EXTENSION_FUNCTION("developerPrivate.showPath",
587 DEVELOPERPRIVATE_SHOWPATH);
589 protected:
590 ~DeveloperPrivateShowPathFunction() override;
591 ResponseAction Run() override;
594 class DeveloperPrivateSetShortcutHandlingSuspendedFunction
595 : public DeveloperPrivateAPIFunction {
596 public:
597 DECLARE_EXTENSION_FUNCTION("developerPrivate.setShortcutHandlingSuspended",
598 DEVELOPERPRIVATE_SETSHORTCUTHANDLINGSUSPENDED);
600 protected:
601 ~DeveloperPrivateSetShortcutHandlingSuspendedFunction() override;
602 ResponseAction Run() override;
605 class DeveloperPrivateUpdateExtensionCommandFunction
606 : public DeveloperPrivateAPIFunction {
607 public:
608 DECLARE_EXTENSION_FUNCTION("developerPrivate.updateExtensionCommand",
609 DEVELOPERPRIVATE_UPDATEEXTENSIONCOMMAND);
611 protected:
612 ~DeveloperPrivateUpdateExtensionCommandFunction() override;
613 ResponseAction Run() override;
616 } // namespace api
618 } // namespace extensions
620 #endif // CHROME_BROWSER_EXTENSIONS_API_DEVELOPER_PRIVATE_DEVELOPER_PRIVATE_API_H_