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_
10 #include "base/files/file.h"
11 #include "chrome/browser/extensions/api/developer_private/entry_picker.h"
12 #include "chrome/browser/extensions/api/file_system/file_system_api.h"
13 #include "chrome/browser/extensions/chrome_extension_function.h"
14 #include "chrome/browser/extensions/error_console/error_console.h"
15 #include "chrome/browser/extensions/extension_install_prompt.h"
16 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
17 #include "chrome/browser/extensions/pack_extension_job.h"
18 #include "chrome/browser/extensions/requirements_checker.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "content/public/browser/render_view_host.h"
22 #include "extensions/browser/browser_context_keyed_api_factory.h"
23 #include "extensions/browser/event_router.h"
24 #include "ui/shell_dialogs/select_file_dialog.h"
25 #include "webkit/browser/fileapi/file_system_context.h"
26 #include "webkit/browser/fileapi/file_system_operation.h"
28 class ExtensionService;
31 namespace extensions {
34 class ExtensionSystem;
35 class ManagementPolicy;
40 class EntryPickerClient;
42 namespace developer_private {
45 struct ItemInspectView;
48 } // namespace developer_private
52 namespace developer = api::developer_private;
54 typedef std::vector<linked_ptr<developer::ItemInfo> > ItemInfoList;
55 typedef std::vector<linked_ptr<developer::ProjectInfo> > ProjectInfoList;
56 typedef std::vector<linked_ptr<developer::ItemInspectView> >
59 class DeveloperPrivateEventRouter : public content::NotificationObserver,
60 public ErrorConsole::Observer {
62 explicit DeveloperPrivateEventRouter(Profile* profile);
63 virtual ~DeveloperPrivateEventRouter();
65 // Add or remove an ID to the list of extensions subscribed to events.
66 void AddExtensionId(const std::string& extension_id);
67 void RemoveExtensionId(const std::string& extension_id);
70 // content::NotificationObserver implementation
71 virtual void Observe(int type,
72 const content::NotificationSource& source,
73 const content::NotificationDetails& details) OVERRIDE;
75 // ErrorConsole::Observer implementation
76 virtual void OnErrorAdded(const ExtensionError* error) OVERRIDE;
78 content::NotificationRegistrar registrar_;
82 // The set of IDs of the Extensions that have subscribed to DeveloperPrivate
83 // events. Since the only consumer of the DeveloperPrivate API is currently
84 // the Apps Developer Tool (which replaces the chrome://extensions page), we
85 // don't want to send information about the subscribing extension in an
86 // update. In particular, we want to avoid entering a loop, which could happen
87 // when, e.g., the Apps Developer Tool throws an error.
88 std::set<std::string> extension_ids_;
90 DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateEventRouter);
93 // The profile-keyed service that manages the DeveloperPrivate API.
94 class DeveloperPrivateAPI : public BrowserContextKeyedAPI,
95 public EventRouter::Observer {
97 static BrowserContextKeyedAPIFactory<DeveloperPrivateAPI>*
100 // Convenience method to get the DeveloperPrivateAPI for a profile.
101 static DeveloperPrivateAPI* Get(content::BrowserContext* context);
103 explicit DeveloperPrivateAPI(content::BrowserContext* context);
104 virtual ~DeveloperPrivateAPI();
106 void SetLastUnpackedDirectory(const base::FilePath& path);
108 base::FilePath& GetLastUnpackedDirectory() {
109 return last_unpacked_directory_;
112 // KeyedService implementation
113 virtual void Shutdown() OVERRIDE;
115 // EventRouter::Observer implementation.
116 virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
117 virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE;
120 friend class BrowserContextKeyedAPIFactory<DeveloperPrivateAPI>;
122 // BrowserContextKeyedAPI implementation.
123 static const char* service_name() { return "DeveloperPrivateAPI"; }
124 static const bool kServiceRedirectedInIncognito = true;
125 static const bool kServiceIsNULLWhileTesting = true;
127 void RegisterNotifications();
131 // Used to start the load |load_extension_dialog_| in the last directory that
133 base::FilePath last_unpacked_directory_;
135 // Created lazily upon OnListenerAdded.
136 scoped_ptr<DeveloperPrivateEventRouter> developer_private_event_router_;
138 DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateAPI);
143 class DeveloperPrivateAutoUpdateFunction : public ChromeSyncExtensionFunction {
145 DECLARE_EXTENSION_FUNCTION("developerPrivate.autoUpdate",
146 DEVELOPERPRIVATE_AUTOUPDATE)
149 virtual ~DeveloperPrivateAutoUpdateFunction();
151 // ExtensionFunction:
152 virtual bool RunSync() OVERRIDE;
155 class DeveloperPrivateGetItemsInfoFunction
156 : public ChromeAsyncExtensionFunction {
158 DECLARE_EXTENSION_FUNCTION("developerPrivate.getItemsInfo",
159 DEVELOPERPRIVATE_GETITEMSINFO)
162 virtual ~DeveloperPrivateGetItemsInfoFunction();
164 // ExtensionFunction:
165 virtual bool RunAsync() OVERRIDE;
168 scoped_ptr<developer::ItemInfo> CreateItemInfo(const Extension& item,
169 bool item_is_enabled);
171 void GetIconsOnFileThread(
172 ItemInfoList item_list,
173 std::map<std::string, ExtensionResource> itemIdToIconResourceMap);
175 // Helper that lists the current inspectable html pages for the extension.
176 void GetInspectablePagesForExtensionProcess(
177 const Extension* extension,
178 const std::set<content::RenderViewHost*>& views,
179 ItemInspectViewList* result);
181 ItemInspectViewList GetInspectablePagesForExtension(
182 const Extension* extension,
183 bool extension_is_enabled);
185 void GetAppWindowPagesForExtensionProfile(const Extension* extension,
186 ItemInspectViewList* result);
188 linked_ptr<developer::ItemInspectView> constructInspectView(
190 int render_process_id,
193 bool generated_background_page);
196 class DeveloperPrivateInspectFunction : public ChromeSyncExtensionFunction {
198 DECLARE_EXTENSION_FUNCTION("developerPrivate.inspect",
199 DEVELOPERPRIVATE_INSPECT)
202 virtual ~DeveloperPrivateInspectFunction();
204 // ExtensionFunction:
205 virtual bool RunSync() OVERRIDE;
208 class DeveloperPrivateAllowFileAccessFunction
209 : public ChromeSyncExtensionFunction {
211 DECLARE_EXTENSION_FUNCTION("developerPrivate.allowFileAccess",
212 DEVELOPERPRIVATE_ALLOWFILEACCESS);
215 virtual ~DeveloperPrivateAllowFileAccessFunction();
217 // ExtensionFunction:
218 virtual bool RunSync() OVERRIDE;
221 class DeveloperPrivateAllowIncognitoFunction
222 : public ChromeSyncExtensionFunction {
224 DECLARE_EXTENSION_FUNCTION("developerPrivate.allowIncognito",
225 DEVELOPERPRIVATE_ALLOWINCOGNITO);
228 virtual ~DeveloperPrivateAllowIncognitoFunction();
230 // ExtensionFunction:
231 virtual bool RunSync() OVERRIDE;
234 class DeveloperPrivateReloadFunction : public ChromeSyncExtensionFunction {
236 DECLARE_EXTENSION_FUNCTION("developerPrivate.reload",
237 DEVELOPERPRIVATE_RELOAD);
240 virtual ~DeveloperPrivateReloadFunction();
242 // ExtensionFunction:
243 virtual bool RunSync() OVERRIDE;
246 class DeveloperPrivateShowPermissionsDialogFunction
247 : public ChromeSyncExtensionFunction,
248 public ExtensionInstallPrompt::Delegate {
250 DECLARE_EXTENSION_FUNCTION("developerPrivate.showPermissionsDialog",
251 DEVELOPERPRIVATE_PERMISSIONS);
253 DeveloperPrivateShowPermissionsDialogFunction();
255 virtual ~DeveloperPrivateShowPermissionsDialogFunction();
257 // ExtensionFunction:
258 virtual bool RunSync() OVERRIDE;
260 // Overridden from ExtensionInstallPrompt::Delegate
261 virtual void InstallUIProceed() OVERRIDE;
262 virtual void InstallUIAbort(bool user_initiated) OVERRIDE;
264 scoped_ptr<ExtensionInstallPrompt> prompt_;
265 std::string extension_id_;
268 class DeveloperPrivateEnableFunction
269 : public ChromeSyncExtensionFunction,
270 public base::SupportsWeakPtr<DeveloperPrivateEnableFunction> {
272 DECLARE_EXTENSION_FUNCTION("developerPrivate.enable",
273 DEVELOPERPRIVATE_ENABLE);
275 DeveloperPrivateEnableFunction();
278 virtual ~DeveloperPrivateEnableFunction();
280 // Callback for requirements checker.
281 void OnRequirementsChecked(const std::string& extension_id,
282 std::vector<std::string> requirements_errors);
283 // ExtensionFunction:
284 virtual bool RunSync() OVERRIDE;
287 scoped_ptr<RequirementsChecker> requirements_checker_;
290 class DeveloperPrivateChooseEntryFunction : public ChromeAsyncExtensionFunction,
291 public EntryPickerClient {
293 virtual ~DeveloperPrivateChooseEntryFunction();
294 virtual bool RunAsync() OVERRIDE;
295 bool ShowPicker(ui::SelectFileDialog::Type picker_type,
296 const base::FilePath& last_directory,
297 const base::string16& select_title,
298 const ui::SelectFileDialog::FileTypeInfo& info,
299 int file_type_index);
301 // EntryPickerClient functions.
302 virtual void FileSelected(const base::FilePath& path) = 0;
303 virtual void FileSelectionCanceled() = 0;
307 class DeveloperPrivateLoadUnpackedFunction
308 : public DeveloperPrivateChooseEntryFunction {
310 DECLARE_EXTENSION_FUNCTION("developerPrivate.loadUnpacked",
311 DEVELOPERPRIVATE_LOADUNPACKED);
314 virtual ~DeveloperPrivateLoadUnpackedFunction();
315 virtual bool RunAsync() OVERRIDE;
317 // EntryPickerCLient implementation.
318 virtual void FileSelected(const base::FilePath& path) OVERRIDE;
319 virtual void FileSelectionCanceled() OVERRIDE;
322 class DeveloperPrivateChoosePathFunction
323 : public DeveloperPrivateChooseEntryFunction {
325 DECLARE_EXTENSION_FUNCTION("developerPrivate.choosePath",
326 DEVELOPERPRIVATE_CHOOSEPATH);
329 virtual ~DeveloperPrivateChoosePathFunction();
330 virtual bool RunAsync() OVERRIDE;
332 // EntryPickerClient functions.
333 virtual void FileSelected(const base::FilePath& path) OVERRIDE;
334 virtual void FileSelectionCanceled() OVERRIDE;
337 class DeveloperPrivatePackDirectoryFunction
338 : public ChromeAsyncExtensionFunction,
339 public PackExtensionJob::Client {
342 DECLARE_EXTENSION_FUNCTION("developerPrivate.packDirectory",
343 DEVELOPERPRIVATE_PACKDIRECTORY);
345 DeveloperPrivatePackDirectoryFunction();
347 // ExtensionPackJob::Client implementation.
348 virtual void OnPackSuccess(const base::FilePath& crx_file,
349 const base::FilePath& key_file) OVERRIDE;
350 virtual void OnPackFailure(const std::string& error,
351 ExtensionCreator::ErrorType error_type) OVERRIDE;
354 virtual ~DeveloperPrivatePackDirectoryFunction();
355 virtual bool RunAsync() OVERRIDE;
358 scoped_refptr<PackExtensionJob> pack_job_;
359 std::string item_path_str_;
360 std::string key_path_str_;
363 class DeveloperPrivateIsProfileManagedFunction
364 : public ChromeSyncExtensionFunction {
366 DECLARE_EXTENSION_FUNCTION("developerPrivate.isProfileManaged",
367 DEVELOPERPRIVATE_ISPROFILEMANAGED);
370 virtual ~DeveloperPrivateIsProfileManagedFunction();
372 // ExtensionFunction:
373 virtual bool RunSync() OVERRIDE;
376 class DeveloperPrivateLoadDirectoryFunction
377 : public ChromeAsyncExtensionFunction {
379 DECLARE_EXTENSION_FUNCTION("developerPrivate.loadDirectory",
380 DEVELOPERPRIVATE_LOADUNPACKEDCROS);
382 DeveloperPrivateLoadDirectoryFunction();
385 virtual ~DeveloperPrivateLoadDirectoryFunction();
387 // ExtensionFunction:
388 virtual bool RunAsync() OVERRIDE;
390 void ClearExistingDirectoryContent(const base::FilePath& project_path);
392 void ReadSyncFileSystemDirectory(const base::FilePath& project_path,
393 const base::FilePath& destination_path);
395 void ReadSyncFileSystemDirectoryCb(
396 const base::FilePath& project_path,
397 const base::FilePath& destination_path,
398 base::File::Error result,
399 const fileapi::FileSystemOperation::FileEntryList& file_list,
402 void SnapshotFileCallback(
403 const base::FilePath& target_path,
404 base::File::Error result,
405 const base::File::Info& file_info,
406 const base::FilePath& platform_path,
407 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref);
409 void CopyFile(const base::FilePath& src_path,
410 const base::FilePath& dest_path);
414 scoped_refptr<fileapi::FileSystemContext> context_;
416 // syncfs url representing the root of the folder to be copied.
417 std::string project_base_url_;
419 // physical path on disc of the folder to be copied.
420 base::FilePath project_base_path_;
422 // Path of the current folder to be copied.
423 base::FilePath current_path_;
426 int pending_copy_operations_count_;
428 // This is set to false if any of the copyFile operations fail on
429 // call of the API. It is returned as a response of the API call.
433 class DeveloperPrivateRequestFileSourceFunction
434 : public ChromeAsyncExtensionFunction {
436 DECLARE_EXTENSION_FUNCTION("developerPrivate.requestFileSource",
437 DEVELOPERPRIVATE_REQUESTFILESOURCE);
439 DeveloperPrivateRequestFileSourceFunction();
442 virtual ~DeveloperPrivateRequestFileSourceFunction();
444 // ExtensionFunction:
445 virtual bool RunAsync() OVERRIDE;
448 void LaunchCallback(const base::DictionaryValue& results);
451 class DeveloperPrivateOpenDevToolsFunction
452 : public ChromeAsyncExtensionFunction {
454 DECLARE_EXTENSION_FUNCTION("developerPrivate.openDevTools",
455 DEVELOPERPRIVATE_OPENDEVTOOLS);
457 DeveloperPrivateOpenDevToolsFunction();
460 virtual ~DeveloperPrivateOpenDevToolsFunction();
462 // ExtensionFunction:
463 virtual bool RunAsync() OVERRIDE;
468 } // namespace extensions
470 #endif // CHROME_BROWSER_EXTENSIONS_API_DEVELOPER_PRIVATE_DEVELOPER_PRIVATE_API_H_