Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / developer_private / developer_private_api.h
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 <set>
9
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"
27
28 class ExtensionService;
29 class Profile;
30
31 namespace extensions {
32
33 class ExtensionError;
34 class ExtensionSystem;
35 class ManagementPolicy;
36
37 namespace api {
38
39 class EntryPicker;
40 class EntryPickerClient;
41
42 namespace developer_private {
43
44 struct ItemInfo;
45 struct ItemInspectView;
46 struct ProjectInfo;
47
48 }  // namespace developer_private
49
50 }  // namespace api
51
52 namespace developer = api::developer_private;
53
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> >
57     ItemInspectViewList;
58
59 class DeveloperPrivateEventRouter : public content::NotificationObserver,
60                                     public ErrorConsole::Observer {
61  public:
62   explicit DeveloperPrivateEventRouter(Profile* profile);
63   virtual ~DeveloperPrivateEventRouter();
64
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);
68
69  private:
70   // content::NotificationObserver implementation
71   virtual void Observe(int type,
72                        const content::NotificationSource& source,
73                        const content::NotificationDetails& details) OVERRIDE;
74
75   // ErrorConsole::Observer implementation
76   virtual void OnErrorAdded(const ExtensionError* error) OVERRIDE;
77
78   content::NotificationRegistrar registrar_;
79
80   Profile* profile_;
81
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_;
89
90   DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateEventRouter);
91 };
92
93 // The profile-keyed service that manages the DeveloperPrivate API.
94 class DeveloperPrivateAPI : public BrowserContextKeyedAPI,
95                             public EventRouter::Observer {
96  public:
97   static BrowserContextKeyedAPIFactory<DeveloperPrivateAPI>*
98       GetFactoryInstance();
99
100   // Convenience method to get the DeveloperPrivateAPI for a profile.
101   static DeveloperPrivateAPI* Get(content::BrowserContext* context);
102
103   explicit DeveloperPrivateAPI(content::BrowserContext* context);
104   virtual ~DeveloperPrivateAPI();
105
106   void SetLastUnpackedDirectory(const base::FilePath& path);
107
108   base::FilePath& GetLastUnpackedDirectory() {
109     return last_unpacked_directory_;
110   }
111
112   // KeyedService implementation
113   virtual void Shutdown() OVERRIDE;
114
115   // EventRouter::Observer implementation.
116   virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
117   virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE;
118
119  private:
120   friend class BrowserContextKeyedAPIFactory<DeveloperPrivateAPI>;
121
122   // BrowserContextKeyedAPI implementation.
123   static const char* service_name() { return "DeveloperPrivateAPI"; }
124   static const bool kServiceRedirectedInIncognito = true;
125   static const bool kServiceIsNULLWhileTesting = true;
126
127   void RegisterNotifications();
128
129   Profile* profile_;
130
131   // Used to start the load |load_extension_dialog_| in the last directory that
132   // was loaded.
133   base::FilePath last_unpacked_directory_;
134
135   // Created lazily upon OnListenerAdded.
136   scoped_ptr<DeveloperPrivateEventRouter> developer_private_event_router_;
137
138   DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateAPI);
139 };
140
141 namespace api {
142
143 class DeveloperPrivateAutoUpdateFunction : public ChromeSyncExtensionFunction {
144  public:
145   DECLARE_EXTENSION_FUNCTION("developerPrivate.autoUpdate",
146                              DEVELOPERPRIVATE_AUTOUPDATE)
147
148  protected:
149   virtual ~DeveloperPrivateAutoUpdateFunction();
150
151   // ExtensionFunction:
152   virtual bool RunSync() OVERRIDE;
153 };
154
155 class DeveloperPrivateGetItemsInfoFunction
156     : public ChromeAsyncExtensionFunction {
157  public:
158   DECLARE_EXTENSION_FUNCTION("developerPrivate.getItemsInfo",
159                              DEVELOPERPRIVATE_GETITEMSINFO)
160
161  protected:
162   virtual ~DeveloperPrivateGetItemsInfoFunction();
163
164   // ExtensionFunction:
165   virtual bool RunAsync() OVERRIDE;
166
167  private:
168   scoped_ptr<developer::ItemInfo> CreateItemInfo(const Extension& item,
169                                                  bool item_is_enabled);
170
171   void GetIconsOnFileThread(
172       ItemInfoList item_list,
173       std::map<std::string, ExtensionResource> itemIdToIconResourceMap);
174
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);
180
181   ItemInspectViewList GetInspectablePagesForExtension(
182       const Extension* extension,
183       bool extension_is_enabled);
184
185   void GetAppWindowPagesForExtensionProfile(const Extension* extension,
186                                             ItemInspectViewList* result);
187
188   linked_ptr<developer::ItemInspectView> constructInspectView(
189       const GURL& url,
190       int render_process_id,
191       int render_view_id,
192       bool incognito,
193       bool generated_background_page);
194 };
195
196 class DeveloperPrivateInspectFunction : public ChromeSyncExtensionFunction {
197  public:
198   DECLARE_EXTENSION_FUNCTION("developerPrivate.inspect",
199                              DEVELOPERPRIVATE_INSPECT)
200
201  protected:
202   virtual ~DeveloperPrivateInspectFunction();
203
204   // ExtensionFunction:
205   virtual bool RunSync() OVERRIDE;
206 };
207
208 class DeveloperPrivateAllowFileAccessFunction
209     : public ChromeSyncExtensionFunction {
210  public:
211   DECLARE_EXTENSION_FUNCTION("developerPrivate.allowFileAccess",
212                              DEVELOPERPRIVATE_ALLOWFILEACCESS);
213
214  protected:
215   virtual ~DeveloperPrivateAllowFileAccessFunction();
216
217   // ExtensionFunction:
218   virtual bool RunSync() OVERRIDE;
219 };
220
221 class DeveloperPrivateAllowIncognitoFunction
222     : public ChromeSyncExtensionFunction {
223  public:
224   DECLARE_EXTENSION_FUNCTION("developerPrivate.allowIncognito",
225                              DEVELOPERPRIVATE_ALLOWINCOGNITO);
226
227  protected:
228   virtual ~DeveloperPrivateAllowIncognitoFunction();
229
230   // ExtensionFunction:
231   virtual bool RunSync() OVERRIDE;
232 };
233
234 class DeveloperPrivateReloadFunction : public ChromeSyncExtensionFunction {
235  public:
236   DECLARE_EXTENSION_FUNCTION("developerPrivate.reload",
237                              DEVELOPERPRIVATE_RELOAD);
238
239  protected:
240   virtual ~DeveloperPrivateReloadFunction();
241
242   // ExtensionFunction:
243   virtual bool RunSync() OVERRIDE;
244 };
245
246 class DeveloperPrivateShowPermissionsDialogFunction
247     : public ChromeSyncExtensionFunction,
248       public ExtensionInstallPrompt::Delegate {
249  public:
250   DECLARE_EXTENSION_FUNCTION("developerPrivate.showPermissionsDialog",
251                              DEVELOPERPRIVATE_PERMISSIONS);
252
253   DeveloperPrivateShowPermissionsDialogFunction();
254  protected:
255   virtual ~DeveloperPrivateShowPermissionsDialogFunction();
256
257   // ExtensionFunction:
258   virtual bool RunSync() OVERRIDE;
259
260   // Overridden from ExtensionInstallPrompt::Delegate
261   virtual void InstallUIProceed() OVERRIDE;
262   virtual void InstallUIAbort(bool user_initiated) OVERRIDE;
263
264   scoped_ptr<ExtensionInstallPrompt> prompt_;
265   std::string extension_id_;
266 };
267
268 class DeveloperPrivateEnableFunction
269     : public ChromeSyncExtensionFunction,
270       public base::SupportsWeakPtr<DeveloperPrivateEnableFunction> {
271  public:
272   DECLARE_EXTENSION_FUNCTION("developerPrivate.enable",
273                              DEVELOPERPRIVATE_ENABLE);
274
275   DeveloperPrivateEnableFunction();
276
277  protected:
278   virtual ~DeveloperPrivateEnableFunction();
279
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;
285
286  private:
287   scoped_ptr<RequirementsChecker> requirements_checker_;
288 };
289
290 class DeveloperPrivateChooseEntryFunction : public ChromeAsyncExtensionFunction,
291                                             public EntryPickerClient {
292  protected:
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);
300
301   // EntryPickerClient functions.
302   virtual void FileSelected(const base::FilePath& path) = 0;
303   virtual void FileSelectionCanceled() = 0;
304 };
305
306
307 class DeveloperPrivateLoadUnpackedFunction
308     : public DeveloperPrivateChooseEntryFunction {
309  public:
310   DECLARE_EXTENSION_FUNCTION("developerPrivate.loadUnpacked",
311                              DEVELOPERPRIVATE_LOADUNPACKED);
312
313  protected:
314   virtual ~DeveloperPrivateLoadUnpackedFunction();
315   virtual bool RunAsync() OVERRIDE;
316
317   // EntryPickerCLient implementation.
318   virtual void FileSelected(const base::FilePath& path) OVERRIDE;
319   virtual void FileSelectionCanceled() OVERRIDE;
320 };
321
322 class DeveloperPrivateChoosePathFunction
323     : public DeveloperPrivateChooseEntryFunction {
324  public:
325   DECLARE_EXTENSION_FUNCTION("developerPrivate.choosePath",
326                              DEVELOPERPRIVATE_CHOOSEPATH);
327
328  protected:
329   virtual ~DeveloperPrivateChoosePathFunction();
330   virtual bool RunAsync() OVERRIDE;
331
332   // EntryPickerClient functions.
333   virtual void FileSelected(const base::FilePath& path) OVERRIDE;
334   virtual void FileSelectionCanceled() OVERRIDE;
335 };
336
337 class DeveloperPrivatePackDirectoryFunction
338     : public ChromeAsyncExtensionFunction,
339       public PackExtensionJob::Client {
340
341  public:
342   DECLARE_EXTENSION_FUNCTION("developerPrivate.packDirectory",
343                              DEVELOPERPRIVATE_PACKDIRECTORY);
344
345   DeveloperPrivatePackDirectoryFunction();
346
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;
352
353  protected:
354   virtual ~DeveloperPrivatePackDirectoryFunction();
355   virtual bool RunAsync() OVERRIDE;
356
357  private:
358   scoped_refptr<PackExtensionJob> pack_job_;
359   std::string item_path_str_;
360   std::string key_path_str_;
361 };
362
363 class DeveloperPrivateIsProfileManagedFunction
364     : public ChromeSyncExtensionFunction {
365  public:
366   DECLARE_EXTENSION_FUNCTION("developerPrivate.isProfileManaged",
367                              DEVELOPERPRIVATE_ISPROFILEMANAGED);
368
369  protected:
370   virtual ~DeveloperPrivateIsProfileManagedFunction();
371
372   // ExtensionFunction:
373   virtual bool RunSync() OVERRIDE;
374 };
375
376 class DeveloperPrivateLoadDirectoryFunction
377     : public ChromeAsyncExtensionFunction {
378  public:
379   DECLARE_EXTENSION_FUNCTION("developerPrivate.loadDirectory",
380                              DEVELOPERPRIVATE_LOADUNPACKEDCROS);
381
382   DeveloperPrivateLoadDirectoryFunction();
383
384  protected:
385   virtual ~DeveloperPrivateLoadDirectoryFunction();
386
387   // ExtensionFunction:
388   virtual bool RunAsync() OVERRIDE;
389
390   void ClearExistingDirectoryContent(const base::FilePath& project_path);
391
392   void ReadSyncFileSystemDirectory(const base::FilePath& project_path,
393                                    const base::FilePath& destination_path);
394
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,
400       bool has_more);
401
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);
408
409   void CopyFile(const base::FilePath& src_path,
410                 const base::FilePath& dest_path);
411
412   void Load();
413
414   scoped_refptr<fileapi::FileSystemContext> context_;
415
416   // syncfs url representing the root of the folder to be copied.
417   std::string project_base_url_;
418
419   // physical path on disc of the folder to be copied.
420   base::FilePath project_base_path_;
421
422   // Path of the current folder to be copied.
423   base::FilePath current_path_;
424
425  private:
426   int pending_copy_operations_count_;
427
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.
430   bool success_;
431 };
432
433 class DeveloperPrivateRequestFileSourceFunction
434     : public ChromeAsyncExtensionFunction {
435  public:
436   DECLARE_EXTENSION_FUNCTION("developerPrivate.requestFileSource",
437                              DEVELOPERPRIVATE_REQUESTFILESOURCE);
438
439   DeveloperPrivateRequestFileSourceFunction();
440
441  protected:
442   virtual ~DeveloperPrivateRequestFileSourceFunction();
443
444   // ExtensionFunction:
445   virtual bool RunAsync() OVERRIDE;
446
447  private:
448   void LaunchCallback(const base::DictionaryValue& results);
449 };
450
451 class DeveloperPrivateOpenDevToolsFunction
452     : public ChromeAsyncExtensionFunction {
453  public:
454   DECLARE_EXTENSION_FUNCTION("developerPrivate.openDevTools",
455                              DEVELOPERPRIVATE_OPENDEVTOOLS);
456
457   DeveloperPrivateOpenDevToolsFunction();
458
459  protected:
460   virtual ~DeveloperPrivateOpenDevToolsFunction();
461
462   // ExtensionFunction:
463   virtual bool RunAsync() OVERRIDE;
464 };
465
466 }  // namespace api
467
468 }  // namespace extensions
469
470 #endif  // CHROME_BROWSER_EXTENSIONS_API_DEVELOPER_PRIVATE_DEVELOPER_PRIVATE_API_H_