file_system_api.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_FILE_SYSTEM_API_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_FILE_SYSTEM_API_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/extension_function.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/extensions/api/file_system.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/shell_dialogs/select_file_dialog.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochnamespace base {
13eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass FilePath;
14eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
15eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace extensions {
17eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass ExtensionPrefs;
18eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
19eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochnamespace file_system_api {
20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
21eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Methods to get and set the path of the directory containing the last file
22eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// chosen by the user in response to a chrome.fileSystem.chooseEntry() call for
23eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// the given extension.
24eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
25eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Returns true and populates result on success; false on failure.
26eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool GetLastChooseEntryDirectory(const ExtensionPrefs* prefs,
27eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                 const std::string& extension_id,
28eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                 base::FilePath* path);
29eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
30eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid SetLastChooseEntryDirectory(ExtensionPrefs* prefs,
31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                 const std::string& extension_id,
32eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                 const base::FilePath& path);
33eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
34eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}  // namespace file_system_api
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FileSystemGetDisplayPathFunction : public SyncExtensionFunction {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DECLARE_EXTENSION_FUNCTION("fileSystem.getDisplayPath",
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                             FILESYSTEM_GETDISPLAYPATH)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~FileSystemGetDisplayPathFunction() {}
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool RunImpl() OVERRIDE;
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FileSystemEntryFunction : public AsyncExtensionFunction {
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum EntryType {
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    READ_ONLY,
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WRITABLE
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~FileSystemEntryFunction() {}
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasFileSystemWritePermission();
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This is called when a writable file entry is being returned. The function
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // will ensure the file exists, creating it if necessary, and also check that
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // the file is not a link. If it succeeds it proceeds to
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // RegisterFileSystemAndSendResponse, otherwise to HandleWritableFileError.
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void CheckWritableFile(const base::FilePath& path);
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This will finish the choose file process. This is either called directly
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // from FileSelected, or from CreateFileIfNecessary. It is called on the UI
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // thread.
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RegisterFileSystemAndSendResponse(const base::FilePath& path,
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                         EntryType entry_type);
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // This will finish the choose file process. This is either called directly
7090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // from FileSelected, or from CreateFileIfNecessary. It is called on the UI
7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // thread. |id_override| specifies the id to send in the response instead of
7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // the generated id. This can be useful for creating a file entry with an id
7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // matching another file entry, e.g. for restoreEntry.
7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void RegisterFileSystemAndSendResponseWithIdOverride(
7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      const base::FilePath& path,
7690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      EntryType entry_type,
7790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      const std::string& id_override);
7890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // called on the UI thread if there is a problem checking a writable file.
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void HandleWritableFileError();
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FileSystemGetWritableEntryFunction : public FileSystemEntryFunction {
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DECLARE_EXTENSION_FUNCTION("fileSystem.getWritableEntry",
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                             FILESYSTEM_GETWRITABLEENTRY)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~FileSystemGetWritableEntryFunction() {}
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool RunImpl() OVERRIDE;
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FileSystemIsWritableEntryFunction : public SyncExtensionFunction {
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DECLARE_EXTENSION_FUNCTION("fileSystem.isWritableEntry",
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                             FILESYSTEM_ISWRITABLEENTRY)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~FileSystemIsWritableEntryFunction() {}
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool RunImpl() OVERRIDE;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FileSystemChooseEntryFunction : public FileSystemEntryFunction {
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Allow picker UI to be skipped in testing.
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static void SkipPickerAndAlwaysSelectPathForTest(base::FilePath* path);
107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  static void SkipPickerAndSelectSuggestedPathForTest();
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void SkipPickerAndAlwaysCancelForTest();
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void StopSkippingPickerForTest();
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Call this with the directory for test file paths. On Chrome OS, accessed
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // path needs to be explicitly registered for smooth integration with Google
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Drive support.
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static void RegisterTempExternalFileSystemForTest(const std::string& name,
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                    const base::FilePath& path);
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DECLARE_EXTENSION_FUNCTION("fileSystem.chooseEntry", FILESYSTEM_CHOOSEENTRY)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::vector<linked_ptr<extensions::api::file_system::AcceptOption> >
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      AcceptOptions;
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void BuildFileTypeInfo(
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ui::SelectFileDialog::FileTypeInfo* file_type_info,
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const base::FilePath::StringType& suggested_extension,
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const AcceptOptions* accepts,
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const bool* acceptsAllTypes);
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void BuildSuggestion(const std::string* opt_name,
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              base::FilePath* suggested_name,
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              base::FilePath::StringType* suggested_extension);
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class FilePicker;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~FileSystemChooseEntryFunction() {}
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool RunImpl() OVERRIDE;
135c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void ShowPicker(const ui::SelectFileDialog::FileTypeInfo& file_type_info,
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  ui::SelectFileDialog::Type picker_type,
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  EntryType entry_type);
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
140c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void SetInitialPathOnFileThread(const base::FilePath& suggested_name,
141c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                  const base::FilePath& previous_path);
142c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // FileSelected and FileSelectionCanceled are called by the file picker.
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void FileSelected(const base::FilePath& path, EntryType entry_type);
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void FileSelectionCanceled();
146c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
147c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::FilePath initial_path_;
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class FileSystemRetainEntryFunction : public SyncExtensionFunction {
15190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public:
15290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DECLARE_EXTENSION_FUNCTION("fileSystem.retainEntry", FILESYSTEM_RETAINENTRY)
15390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
15490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) protected:
15590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual ~FileSystemRetainEntryFunction() {}
15690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual bool RunImpl() OVERRIDE;
15790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
15890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) private:
15990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Retains the file entry referenced by |entry_id| in apps::SavedFilesService.
16090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // |entry_id| must refer to an entry in an isolated file system.
16190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool RetainFileEntry(const std::string& entry_id);
16290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)};
16390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
16490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class FileSystemIsRestorableFunction : public SyncExtensionFunction {
16590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public:
16690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DECLARE_EXTENSION_FUNCTION("fileSystem.isRestorable", FILESYSTEM_ISRESTORABLE)
16790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
16890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) protected:
16990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual ~FileSystemIsRestorableFunction() {}
17090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual bool RunImpl() OVERRIDE;
17190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)};
17290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
17390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class FileSystemRestoreEntryFunction : public FileSystemEntryFunction {
17490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public:
17590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DECLARE_EXTENSION_FUNCTION("fileSystem.restoreEntry", FILESYSTEM_RESTOREENTRY)
17690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
17790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) protected:
17890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual ~FileSystemRestoreEntryFunction() {}
17990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual bool RunImpl() OVERRIDE;
18090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)};
18190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace extensions
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_FILE_SYSTEM_API_H_
185