// Copyright 2023 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_view>

#include "base/test/gmock_expected_support.h"
#include "base/test/test_future.h"
#include "base/types/expected.h"
#include "chrome/browser/ui/web_applications/test/isolated_web_app_test_utils.h"
#include "chrome/browser/web_applications/isolated_web_apps/commands/install_isolated_web_app_command.h"
#include "chrome/browser/web_applications/isolated_web_apps/install/isolated_web_app_install_source.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_trust_checker.h"
#include "chrome/browser/web_applications/isolated_web_apps/test/isolated_web_app_builder.h"
#include "chrome/browser/web_applications/sub_apps/sub_apps_service_impl.h"
#include "chrome/browser/web_applications/test/web_app_icon_test_utils.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_command_scheduler.h"
#include "chrome/browser/web_applications/web_app_filter.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "components/webapps/isolated_web_apps/test_support/signing_keys.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "mojo/public/cpp/test_support/fake_message_dispatch_context.h"
#include "third_party/blink/public/common/features.h"

using InstallResult =
    base::expected<web_app::InstallIsolatedWebAppCommandSuccess,
                   web_app::InstallIsolatedWebAppCommandError>;
using blink::mojom::SubAppsService;

namespace web_app {

class SubAppsPermissionsPolicyBrowserTest
    : public IsolatedWebAppBrowserTestHarness {
  base::ScopedTempDir scoped_temp_dir_;
 public:
  webapps::AppId parent_app_id_;

  void InstallIwaApp() {
    IsolatedWebAppUrlInfo url_info =
        *web_app::IsolatedWebAppBuilder(web_app::ManifestBuilder()
                                            .SetName("Parent apps test app")
                                            .SetVersion("1.0.0"))
             .BuildBundle()
             ->Install(profile());

    parent_app_id_ = url_info.app_id();

    auto installed_version = base::Version("1.0.0");

    std::unique_ptr<ScopedBundledIsolatedWebApp> bundle =
        IsolatedWebAppBuilder(ManifestBuilder()
                                  .SetName("Sub apps test app")
                                  .SetVersion(installed_version.GetString()))
            .BuildBundle();

    *bundle->Install(profile());

    const WebApp* web_app =
        provider().registrar_unsafe().GetAppById(parent_app_id_);

    EXPECT_TRUE(provider().registrar_unsafe().AppMatches(
        parent_app_id_, WebAppFilter::InstalledInOperatingSystemForTesting()));

    EXPECT_TRUE(provider().registrar_unsafe().AppMatches(
        parent_app_id_, WebAppFilter::IsIsolatedApp()));

    ASSERT_THAT(web_app, Pointee(test::Property(
                             "untranslated_name", &WebApp::untranslated_name,
                             testing::Eq("Parent apps test app"))));
  }

  void BindRemote(content::RenderFrameHost* frame) {
    // Any navigation causes the remote to be destroyed (since the
    // render_frame_host that owns it gets destroyed.)
    SubAppsServiceImpl::CreateIfAllowed(frame,
                                        remote_.BindNewPipeAndPassReceiver());
  }

 protected:
  base::test::ScopedFeatureList features_{blink::features::kSubApps};
  mojo::Remote<SubAppsService> remote_;
};

IN_PROC_BROWSER_TEST_F(SubAppsPermissionsPolicyBrowserTest,
                       EndToEndAddFailsIfPermissionsPolicyIsMissing) {
  ASSERT_NO_FATAL_FAILURE(InstallIwaApp());
  content::RenderFrameHost* iwa_frame = OpenApp(parent_app_id_);

  auto result = content::ExecJs(iwa_frame, "window.subApps.add([])");
  EXPECT_FALSE(result);
  std::string message = result.message();
  EXPECT_NE(message.find(
                "The executing top-level browsing context is not granted the "
                "\"sub-apps\" permissions policy."),
            std::string::npos);

  EXPECT_THAT(provider().registrar_unsafe().GetAllSubAppIds(parent_app_id_),
              testing::IsEmpty());
}

IN_PROC_BROWSER_TEST_F(SubAppsPermissionsPolicyBrowserTest,
                       MojoBadMessageIfPermissionPolicyIsMissedBypassingJS) {
  ASSERT_NO_FATAL_FAILURE(InstallIwaApp());
  content::RenderFrameHost* iwa_frame = OpenApp(parent_app_id_);

  mojo::FakeMessageDispatchContext fake_dispatch_context;
  mojo::test::BadMessageObserver bad_message_observer;

  BindRemote(iwa_frame);

  EXPECT_THAT(bad_message_observer.WaitForBadMessage(),
              "No subApps permission policy provided");
}

}  // namespace web_app
