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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/webshare/win/fake_storage_file_statics.h"
#include <windows.foundation.h>
#include <windows.storage.streams.h>
#include <wrl/module.h>
#include <memory>
#include <string>
#include <tuple>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/fake_iasync_operation_win.h"
#include "base/win/scoped_hstring.h"
#include "chrome/browser/webshare/win/fake_random_access_stream.h"
#include "testing/gtest/include/gtest/gtest.h"
using ABI::Windows::Foundation::DateTime;
using ABI::Windows::Foundation::IAsyncAction;
using ABI::Windows::Foundation::IAsyncOperation;
using ABI::Windows::Foundation::IUriRuntimeClass;
using ABI::Windows::Storage::FileAccessMode;
using ABI::Windows::Storage::FileAttributes;
using ABI::Windows::Storage::IStorageFile;
using ABI::Windows::Storage::IStorageFolder;
using ABI::Windows::Storage::IStorageItem;
using ABI::Windows::Storage::IStreamedFileDataRequestedHandler;
using ABI::Windows::Storage::NameCollisionOption;
using ABI::Windows::Storage::StorageDeleteOption;
using ABI::Windows::Storage::StorageFile;
using ABI::Windows::Storage::StorageItemTypes;
using ABI::Windows::Storage::StorageStreamTransaction;
using ABI::Windows::Storage::FileProperties::BasicProperties;
using ABI::Windows::Storage::Streams::IOutputStream;
using ABI::Windows::Storage::Streams::IRandomAccessStream;
using ABI::Windows::Storage::Streams::IRandomAccessStreamReference;
using Microsoft::WRL::ComPtr;
using Microsoft::WRL::Make;
using Microsoft::WRL::RuntimeClass;
using Microsoft::WRL::RuntimeClassFlags;
using Microsoft::WRL::WinRtClassicComMix;
namespace webshare {
namespace {
class FakeStorageFile final
: public RuntimeClass<RuntimeClassFlags<WinRtClassicComMix>,
IStorageFile,
IStorageItem> {
public:
FakeStorageFile(HSTRING display_name_with_extension,
IStreamedFileDataRequestedHandler* data_requested,
IRandomAccessStreamReference* thumbnail)
: streamed_file_data_requested_handler_(data_requested) {
// ScopedHString takes ownership of the HSTRING provided to it, but taking
// ownership is not an expected behavior when passing an HSTRING to a system
// API, so we use a temporary ScopedHString to make a copy we can safely own
// and release ownership of the original 'back' to the caller.
base::win::ScopedHString holder(display_name_with_extension);
display_name_with_extension_ = holder.GetAsUTF8();
std::ignore = holder.release();
}
FakeStorageFile(const FakeStorageFile&) = delete;
FakeStorageFile& operator=(const FakeStorageFile&) = delete;
~FakeStorageFile() final {
EXPECT_FALSE(open_async_in_progress_)
<< "FakeStorageFile destroyed while open operation is in progress.";
}
// ABI::Windows::Storage::IStorageFile
IFACEMETHODIMP get_FileType(HSTRING* value) final { NOTREACHED(); }
IFACEMETHODIMP get_ContentType(HSTRING* value) final { NOTREACHED(); }
IFACEMETHODIMP OpenAsync(
FileAccessMode access_mode,
IAsyncOperation<IRandomAccessStream*>** operation) final {
if (open_async_in_progress_) {
ADD_FAILURE()
<< "OpenAsync called while an open operation is in progress.";
return E_ILLEGAL_METHOD_CALL;
}
auto fake_iasync_operation =
Make<base::win::FakeIAsyncOperation<IRandomAccessStream*>>();
HRESULT hr = fake_iasync_operation->QueryInterface(IID_PPV_ARGS(operation));
if (FAILED(hr)) {
EXPECT_HRESULT_SUCCEEDED(hr);
return hr;
}
bool success = base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&FakeStorageFile::OnOpenAsync,
weak_factory_.GetWeakPtr(), fake_iasync_operation));
if (!success) {
EXPECT_TRUE(success);
return E_ASYNC_OPERATION_NOT_STARTED;
}
open_async_in_progress_ = true;
return S_OK;
}
IFACEMETHODIMP OpenTransactedWriteAsync(
IAsyncOperation<StorageStreamTransaction*>** operation) final {
NOTREACHED();
}
IFACEMETHODIMP CopyOverloadDefaultNameAndOptions(
IStorageFolder* destination_folder,
IAsyncOperation<StorageFile*>** operation) final {
NOTREACHED();
}
IFACEMETHODIMP CopyOverloadDefaultOptions(
IStorageFolder* destination_folder,
HSTRING desired_new_name,
IAsyncOperation<StorageFile*>** operation) final {
NOTREACHED();
}
IFACEMETHODIMP CopyOverload(IStorageFolder* destination_folder,
HSTRING desired_new_name,
NameCollisionOption option,
IAsyncOperation<StorageFile*>** operation) final {
NOTREACHED();
}
IFACEMETHODIMP
CopyAndReplaceAsync(IStorageFile* file_to_replace,
IAsyncAction** operation) final {
NOTREACHED();
}
IFACEMETHODIMP MoveOverloadDefaultNameAndOptions(
IStorageFolder* destination_folder,
IAsyncAction** operation) final {
NOTREACHED();
}
IFACEMETHODIMP MoveOverloadDefaultOptions(IStorageFolder* destination_folder,
HSTRING desired_new_name,
IAsyncAction** operation) final {
NOTREACHED();
}
IFACEMETHODIMP
MoveOverload(IStorageFolder* destination_folder,
HSTRING desired_new_name,
NameCollisionOption option,
IAsyncAction** operation) final {
NOTREACHED();
}
IFACEMETHODIMP
MoveAndReplaceAsync(IStorageFile* file_to_replace,
IAsyncAction** operation) final {
NOTREACHED();
}
// ABI::Windows::Storage::IStorageItem
IFACEMETHODIMP RenameAsyncOverloadDefaultOptions(
HSTRING desired_name,
IAsyncAction** operation) final {
NOTREACHED();
}
IFACEMETHODIMP
RenameAsync(HSTRING desired_name,
NameCollisionOption option,
IAsyncAction** operation) final {
NOTREACHED();
}
IFACEMETHODIMP DeleteAsyncOverloadDefaultOptions(
IAsyncAction** operation) final {
NOTREACHED();
}
IFACEMETHODIMP
DeleteAsync(StorageDeleteOption option, IAsyncAction** operation) final {
NOTREACHED();
}
IFACEMETHODIMP GetBasicPropertiesAsync(
IAsyncOperation<BasicProperties*>** operation) final {
NOTREACHED();
}
IFACEMETHODIMP get_Name(HSTRING* value) final {
auto copy = base::win::ScopedHString::Create(display_name_with_extension_);
*value = copy.release();
return S_OK;
}
IFACEMETHODIMP get_Path(HSTRING* value) final { NOTREACHED(); }
IFACEMETHODIMP
get_Attributes(FileAttributes* value) final { NOTREACHED(); }
IFACEMETHODIMP
get_DateCreated(DateTime* value) final { NOTREACHED(); }
IFACEMETHODIMP
IsOfType(StorageItemTypes type, boolean* value) final { NOTREACHED(); }
private:
void OnOpenAsync(ComPtr<base::win::FakeIAsyncOperation<IRandomAccessStream*>>
fake_iasync_operation) {
ASSERT_TRUE(open_async_in_progress_);
open_async_in_progress_ = false;
auto fake_stream = Make<FakeRandomAccessStream>();
ComPtr<IOutputStream> output_stream;
ASSERT_HRESULT_SUCCEEDED(fake_stream->GetOutputStreamAt(0, &output_stream));
ComPtr<FakeRandomAccessStream> fake_output_stream =
static_cast<FakeRandomAccessStream*>(output_stream.Get());
ASSERT_TRUE(fake_output_stream);
fake_output_stream->OnClose(
base::BindLambdaForTesting([fake_stream, fake_iasync_operation]() {
ComPtr<IRandomAccessStream> random_access_stream;
ASSERT_HRESULT_SUCCEEDED(fake_stream.As(&random_access_stream));
fake_iasync_operation->CompleteWithResults(random_access_stream);
}));
ASSERT_HRESULT_SUCCEEDED(
streamed_file_data_requested_handler_->Invoke(output_stream.Get()));
}
std::string display_name_with_extension_;
bool open_async_in_progress_ = false;
ComPtr<IStreamedFileDataRequestedHandler>
streamed_file_data_requested_handler_;
base::WeakPtrFactory<FakeStorageFile> weak_factory_{this};
};
} // namespace
FakeStorageFileStatics::FakeStorageFileStatics() = default;
FakeStorageFileStatics::~FakeStorageFileStatics() = default;
IFACEMETHODIMP FakeStorageFileStatics::GetFileFromPathAsync(
HSTRING path,
IAsyncOperation<StorageFile*>** operation) {
NOTREACHED();
}
IFACEMETHODIMP FakeStorageFileStatics::GetFileFromApplicationUriAsync(
IUriRuntimeClass* uri,
IAsyncOperation<StorageFile*>** operation) {
NOTREACHED();
}
IFACEMETHODIMP FakeStorageFileStatics::CreateStreamedFileAsync(
HSTRING display_name_with_extension,
IStreamedFileDataRequestedHandler* data_requested,
IRandomAccessStreamReference* thumbnail,
IAsyncOperation<StorageFile*>** operation) {
auto fake_iasync_operation =
Make<base::win::FakeIAsyncOperation<StorageFile*>>();
HRESULT hr = fake_iasync_operation->QueryInterface(IID_PPV_ARGS(operation));
if (FAILED(hr)) {
EXPECT_HRESULT_SUCCEEDED(hr);
return hr;
}
auto fake_storage_file = Make<FakeStorageFile>(display_name_with_extension,
data_requested, thumbnail);
bool success = base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindLambdaForTesting([fake_iasync_operation, fake_storage_file]() {
fake_iasync_operation->CompleteWithResults(fake_storage_file);
}));
if (!success) {
EXPECT_TRUE(success);
return E_ASYNC_OPERATION_NOT_STARTED;
}
return S_OK;
}
IFACEMETHODIMP FakeStorageFileStatics::ReplaceWithStreamedFileAsync(
IStorageFile* file_to_replace,
IStreamedFileDataRequestedHandler* data_requested,
IRandomAccessStreamReference* thumbnail,
IAsyncOperation<StorageFile*>** operation) {
NOTREACHED();
}
IFACEMETHODIMP FakeStorageFileStatics::CreateStreamedFileFromUriAsync(
HSTRING display_name_with_extension,
IUriRuntimeClass* uri,
IRandomAccessStreamReference* thumbnail,
IAsyncOperation<StorageFile*>** operation) {
NOTREACHED();
}
IFACEMETHODIMP FakeStorageFileStatics::ReplaceWithStreamedFileFromUriAsync(
IStorageFile* file_to_replace,
IUriRuntimeClass* uri,
IRandomAccessStreamReference* thumbnail,
IAsyncOperation<StorageFile*>** operation) {
NOTREACHED();
}
} // namespace webshare
|