Skip to content

Commit

Permalink
WebShare: Windows Implementation (Reland)
Browse files Browse the repository at this point in the history
Completing the implementation of navigator.share() for Windows by
hooking in the relevant components and updating the corresponding tests.

Previously committed (https://crrev.com/c/2506573) but reverted due to
the ShareServiceUnitTest.Multimedia test failing from lack of an
IsSupportedEnvironment check. Patchset 1 is a cherry pick of this
original change for easy comparison.

Bug: 1035527
Change-Id: I57454fcf0da211c49cb582f77261fe2afe6b1e23
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2507652
Reviewed-by: Eric Willigers <ericwilligers@chromium.org>
Commit-Queue: Hoch Hochkeppel <mhochk@microsoft.com>
Cr-Commit-Position: refs/heads/master@{#822385}
  • Loading branch information
mhochk authored and Commit Bot committed Oct 29, 2020
1 parent d3954b9 commit 59ab098
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
30 changes: 30 additions & 0 deletions chrome/browser/webshare/share_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,47 @@
#include "content/public/test/browser_test_utils.h"
#include "net/test/embedded_test_server/embedded_test_server.h"

#if defined(OS_WIN)
#include "chrome/browser/webshare/win/scoped_share_operation_fake_components.h"
#endif

class ShareServiceBrowserTest : public InProcessBrowserTest {
public:
ShareServiceBrowserTest() {
feature_list_.InitAndEnableFeature(features::kWebShare);
}

#if defined(OS_WIN)
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
if (!IsSupportedEnvironment())
return;

ASSERT_NO_FATAL_FAILURE(scoped_fake_components_.SetUp());
}
#endif

protected:
#if defined(OS_WIN)
bool IsSupportedEnvironment() {
return webshare::ScopedShareOperationFakeComponents::
IsSupportedEnvironment();
}
#endif

private:
base::test::ScopedFeatureList feature_list_;
#if defined(OS_WIN)
webshare::ScopedShareOperationFakeComponents scoped_fake_components_;
#endif
};

IN_PROC_BROWSER_TEST_F(ShareServiceBrowserTest, Text) {
#if defined(OS_WIN)
if (!IsSupportedEnvironment())
return;
#endif

ASSERT_TRUE(embedded_test_server()->Start());
ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL("/webshare/index.html"));
Expand Down
18 changes: 15 additions & 3 deletions chrome/browser/webshare/share_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "chrome/browser/webshare/share_service_impl.h"

#include <algorithm>
#include <memory>

#include "base/feature_list.h"
#include "base/strings/string_piece.h"
Expand All @@ -13,6 +14,10 @@
#include "content/public/browser/web_contents.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"

#if defined(OS_WIN)
#include "chrome/browser/webshare/win/share_operation.h"
#endif

// IsDangerousFilename() and IsDangerousMimeType() should be kept in sync with
// //third_party/blink/renderer/modules/webshare/FILE_TYPES.md
// //components/browser_ui/webshare/android/java/src/org/chromium/components/browser_ui/webshare/ShareServiceImpl.java
Expand Down Expand Up @@ -165,10 +170,17 @@ void ShareServiceImpl::Share(const std::string& title,
#if defined(OS_CHROMEOS)
sharesheet_client_.Share(title, text, share_url, std::move(files),
std::move(callback));
#elif defined(OS_WIN)
auto share_operation = std::make_unique<webshare::ShareOperation>(
title, text, share_url, std::move(files), web_contents);
share_operation->Run(base::BindOnce(
[](std::unique_ptr<webshare::ShareOperation> share_operation,
ShareCallback callback,
blink::mojom::ShareError result) { std::move(callback).Run(result); },
std::move(share_operation), std::move(callback)));
#else
// TODO(crbug.com/1035527): Add implementation for OS_WIN
NOTIMPLEMENTED();
std::move(callback).Run(blink::mojom::ShareError::OK);
NOTREACHED();
std::move(callback).Run(blink::mojom::ShareError::INTERNAL_ERROR);
#endif
}

Expand Down
34 changes: 34 additions & 0 deletions chrome/browser/webshare/share_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ using blink::mojom::ShareError;
#include "chrome/browser/sharesheet/sharesheet_types.h"
#include "chrome/browser/webshare/chromeos/sharesheet_client.h"
#endif
#if defined(OS_WIN)
#include "chrome/browser/webshare/win/scoped_share_operation_fake_components.h"
#endif

class ShareServiceUnitTest : public ChromeRenderViewHostTestHarness {
public:
Expand All @@ -44,9 +47,22 @@ class ShareServiceUnitTest : public ChromeRenderViewHostTestHarness {
#if defined(OS_CHROMEOS)
webshare::SharesheetClient::SetSharesheetCallbackForTesting(
base::BindRepeating(&ShareServiceUnitTest::AcceptShareRequest));
#endif
#if defined(OS_WIN)
if (!IsSupportedEnvironment())
return;

ASSERT_NO_FATAL_FAILURE(scoped_fake_components_.SetUp());
#endif
}

#if defined(OS_WIN)
bool IsSupportedEnvironment() {
return webshare::ScopedShareOperationFakeComponents::
IsSupportedEnvironment();
}
#endif

ShareError ShareGeneratedFileData(const std::string& extension,
const std::string& content_type,
unsigned file_length = 100,
Expand Down Expand Up @@ -123,11 +139,19 @@ class ShareServiceUnitTest : public ChromeRenderViewHostTestHarness {
}
#endif

#if defined(OS_WIN)
webshare::ScopedShareOperationFakeComponents scoped_fake_components_;
#endif
base::test::ScopedFeatureList feature_list_;
std::unique_ptr<ShareServiceImpl> share_service_;
};

TEST_F(ShareServiceUnitTest, FileCount) {
#if defined(OS_WIN)
if (!IsSupportedEnvironment())
return;
#endif

EXPECT_EQ(ShareError::OK, ShareGeneratedFileData(".txt", "text/plain", 1234,
kMaxSharedFileCount));
EXPECT_EQ(ShareError::PERMISSION_DENIED,
Expand Down Expand Up @@ -165,13 +189,23 @@ TEST_F(ShareServiceUnitTest, DangerousMimeType) {
}

TEST_F(ShareServiceUnitTest, Multimedia) {
#if defined(OS_WIN)
if (!IsSupportedEnvironment())
return;
#endif

EXPECT_EQ(ShareError::OK, ShareGeneratedFileData(".bmp", "image/bmp"));
EXPECT_EQ(ShareError::OK, ShareGeneratedFileData(".xbm", "image/x-xbitmap"));
EXPECT_EQ(ShareError::OK, ShareGeneratedFileData(".flac", "audio/flac"));
EXPECT_EQ(ShareError::OK, ShareGeneratedFileData(".webm", "video/webm"));
}

TEST_F(ShareServiceUnitTest, PortableDocumentFormat) {
#if defined(OS_WIN)
if (!IsSupportedEnvironment())
return;
#endif

// TODO(crbug.com/1006055): Support sharing of pdf files.
// The URL will be checked using Safe Browsing.
EXPECT_EQ(ShareError::PERMISSION_DENIED,
Expand Down
1 change: 1 addition & 0 deletions chrome/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ static_library("test_support") {
"//components/crash/core/app",
"//third_party/wtl",
]
libs = [ "runtimeobject.lib" ]
}

if (is_chromeos) {
Expand Down

0 comments on commit 59ab098

Please sign in to comment.