1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103

content / public / test / fake_file_system_access_permission_context.h [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/files/file_path.h"
#include "content/public/browser/file_system_access_permission_context.h"
#include "third_party/blink/public/mojom/file_system_access/file_system_access_manager.mojom-shared.h"

#ifndef CONTENT_PUBLIC_TEST_FAKE_FILE_SYSTEM_ACCESS_PERMISSION_CONTEXT_H_
#define CONTENT_PUBLIC_TEST_FAKE_FILE_SYSTEM_ACCESS_PERMISSION_CONTEXT_H_

namespace content {

// Fake permission context which uses an in-memory map for
// [GS]etLastPickedDirectory and returns permissions which are always granted.
// Support for WellKnown directories is provided via a setter which allows
// setting custom paths in an in-memory map.
class FakeFileSystemAccessPermissionContext
    : public FileSystemAccessPermissionContext {
 public:
  static constexpr char16_t kPickerTitle[] = u"Choose something";

  FakeFileSystemAccessPermissionContext();
  ~FakeFileSystemAccessPermissionContext() override;

  scoped_refptr<FileSystemAccessPermissionGrant> GetReadPermissionGrant(
      const url::Origin& origin,
      const PathInfo& path_info,
      HandleType handle_type,
      UserAction user_action) override;

  scoped_refptr<FileSystemAccessPermissionGrant> GetWritePermissionGrant(
      const url::Origin& origin,
      const PathInfo& path_info,
      HandleType handle_type,
      UserAction user_action) override;

  void ConfirmSensitiveEntryAccess(
      const url::Origin& origin,
      const PathInfo& path_info,
      HandleType handle_type,
      UserAction user_action,
      GlobalRenderFrameHostId frame_id,
      base::OnceCallback<void(SensitiveEntryResult)> callback) override;

  void PerformAfterWriteChecks(
      std::unique_ptr<FileSystemAccessWriteItem> item,
      GlobalRenderFrameHostId frame_id,
      base::OnceCallback<void(AfterWriteCheckResult)> callback) override;

  bool IsFileTypeDangerous(const base::FilePath& path,
                           const url::Origin& origin) override;

  base::expected<void, std::string> CanShowFilePicker(
      content::RenderFrameHost* rfh) override;

  bool CanObtainReadPermission(const url::Origin& origin) override;
  bool CanObtainWritePermission(const url::Origin& origin) override;

  void SetLastPickedDirectory(const url::Origin& origin,
                              const std::string& id,
                              const PathInfo& path_info) override;
  PathInfo GetLastPickedDirectory(const url::Origin& origin,
                                  const std::string& id) override;

  // Establishes a mapping between a WellKnownDirectory and a custom path.
  void SetWellKnownDirectoryPath(blink::mojom::WellKnownDirectory directory,
                                 base::FilePath path);
  // Retrieves a path which was earlier specified via SetWellKnownDirectoryPath.
  // Otherwise, returns an empty path.
  base::FilePath GetWellKnownDirectoryPath(
      blink::mojom::WellKnownDirectory directory,
      const url::Origin& origin) override;

  // Returns `kPickerTitle`.
  std::u16string GetPickerTitle(
      const blink::mojom::FilePickerOptionsPtr& options) override;

  // No-op. This class does not manage any permission grants.
  void NotifyEntryMoved(const url::Origin& origin,
                        const PathInfo& old_path,
                        const PathInfo& new_path) override;

  void OnFileCreatedFromShowSaveFilePicker(
      const GURL& file_picker_binding_context,
      const storage::FileSystemURL& url) override;

  void CheckPathsAgainstEnterprisePolicy(
      std::vector<PathInfo> entries,
      GlobalRenderFrameHostId frame_id,
      EntriesAllowedByEnterprisePolicyCallback callback) override;

 private:
  std::map<std::string, PathInfo> id_pathinfo_map_;
  std::map<blink::mojom::WellKnownDirectory, base::FilePath>
      well_known_directory_map_;
};

}  // namespace content

#endif  // CONTENT_PUBLIC_TEST_FAKE_FILE_SYSTEM_ACCESS_PERMISSION_CONTEXT_H_