Commit ed0b0115 authored by David Bertoni's avatar David Bertoni Committed by Commit Bot

[Extensions] Add support to ExtensionBuilder for SW-based extensions.

This CL adds support for Service Worker-based extensions to the
ExtensionBuilder class. It also renames the SetBackgroundPage()
member function to SetBackgroundContext().

Bug: 853378
Change-Id: I30018b46aafefdfb8e6e450101b86f489669cc36
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1719376
Commit-Queue: David Bertoni <dbertoni@chromium.org>
Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#681133}
parent d9edaf4e
......@@ -117,7 +117,7 @@ TEST_F(ExtensionHooksDelegateTest, SendRequestDisabled) {
// extension with an event page).
scoped_refptr<const Extension> extension =
ExtensionBuilder("foo")
.SetBackgroundPage(ExtensionBuilder::BackgroundPage::EVENT)
.SetBackgroundContext(ExtensionBuilder::BackgroundContext::EVENT_PAGE)
.SetLocation(Manifest::UNPACKED)
.Build();
RegisterExtension(extension);
......
......@@ -92,7 +92,8 @@ class LazyBackgroundTaskQueueTest : public ExtensionsTest {
scoped_refptr<const Extension> CreateLazyBackgroundExtension() {
scoped_refptr<const Extension> extension =
ExtensionBuilder("Lazy background")
.SetBackgroundPage(ExtensionBuilder::BackgroundPage::EVENT)
.SetBackgroundContext(
ExtensionBuilder::BackgroundContext::EVENT_PAGE)
.SetID("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
.Build();
ExtensionRegistry::Get(browser_context())->AddEnabled(extension);
......@@ -222,7 +223,7 @@ TEST_F(LazyBackgroundTaskQueueTest, CreateLazyBackgroundPageOnExtensionLoaded) {
scoped_refptr<const Extension> lazy_background =
ExtensionBuilder("Lazy background")
.SetBackgroundPage(ExtensionBuilder::BackgroundPage::EVENT)
.SetBackgroundContext(ExtensionBuilder::BackgroundContext::EVENT_PAGE)
.SetID("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
.Build();
......
......@@ -20,7 +20,8 @@ namespace {
// Creates a very simple extension with a background page.
scoped_refptr<const Extension> CreateExtensionWithBackgroundPage() {
return ExtensionBuilder("test")
.SetBackgroundPage(ExtensionBuilder::BackgroundPage::PERSISTENT)
.SetBackgroundContext(
ExtensionBuilder::BackgroundContext::BACKGROUND_PAGE)
.SetID("id2")
.Build();
}
......
......@@ -13,12 +13,14 @@
namespace extensions {
constexpr char ExtensionBuilder::kServiceWorkerScriptFile[];
struct ExtensionBuilder::ManifestData {
Type type;
std::string name;
std::vector<std::string> permissions;
base::Optional<ActionType> action;
base::Optional<BackgroundPage> background_page;
base::Optional<BackgroundContext> background_context;
base::Optional<std::string> version;
// A ContentScriptEntry includes a string name, and a vector of string
......@@ -68,19 +70,25 @@ struct ExtensionBuilder::ManifestData {
manifest.Set(action_key, std::make_unique<base::DictionaryValue>());
}
if (background_page) {
if (background_context) {
DictionaryBuilder background;
background.Set("page", "background_page.html");
bool persistent = false;
switch (*background_page) {
case BackgroundPage::PERSISTENT:
base::Optional<bool> persistent;
switch (*background_context) {
case BackgroundContext::BACKGROUND_PAGE:
background.Set("page", "background_page.html");
persistent = true;
break;
case BackgroundPage::EVENT:
case BackgroundContext::EVENT_PAGE:
background.Set("page", "background_page.html");
persistent = false;
break;
case BackgroundContext::SERVICE_WORKER:
background.Set("service_worker", kServiceWorkerScriptFile);
break;
}
if (persistent) {
background.Set("persistent", *persistent);
}
background.Set("persistent", persistent);
manifest.Set("background", background.Build());
}
......@@ -172,10 +180,10 @@ ExtensionBuilder& ExtensionBuilder::SetAction(ActionType action) {
return *this;
}
ExtensionBuilder& ExtensionBuilder::SetBackgroundPage(
BackgroundPage background_page) {
ExtensionBuilder& ExtensionBuilder::SetBackgroundContext(
BackgroundContext background_context) {
CHECK(manifest_data_);
manifest_data_->background_page = background_page;
manifest_data_->background_context = background_context;
return *this;
}
......
......@@ -50,11 +50,14 @@ class ExtensionBuilder {
BROWSER_ACTION,
};
enum class BackgroundPage {
PERSISTENT,
EVENT,
enum class BackgroundContext {
BACKGROUND_PAGE,
EVENT_PAGE,
SERVICE_WORKER,
};
static constexpr char kServiceWorkerScriptFile[] = "sw.js";
// Initializes an ExtensionBuilder that can be used with SetManifest() for
// complete customization.
ExtensionBuilder();
......@@ -85,9 +88,8 @@ class ExtensionBuilder {
// be set (though note that we synthesize a page action for most extensions).
ExtensionBuilder& SetAction(ActionType action);
// Sets a background page for the extension to have. By default, no background
// page will be set.
ExtensionBuilder& SetBackgroundPage(BackgroundPage background_page);
// Sets a background context for the extension. By default, none will be set.
ExtensionBuilder& SetBackgroundContext(BackgroundContext background_context);
// Adds a content script to the extension, with a script with the specified
// |script_name| that matches the given |match_patterns|.
......
......@@ -5,7 +5,9 @@
#include "extensions/common/extension_builder.h"
#include "base/version.h"
#include "components/version_info/channel.h"
#include "extensions/common/extension.h"
#include "extensions/common/features/feature_channel.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/manifest_handlers/background_info.h"
#include "extensions/common/manifest_handlers/content_scripts_handler.h"
......@@ -90,18 +92,41 @@ TEST(ExtensionBuilderTest, Background) {
{
scoped_refptr<const Extension> extension =
ExtensionBuilder("persistent background page")
.SetBackgroundPage(ExtensionBuilder::BackgroundPage::PERSISTENT)
.SetBackgroundContext(
ExtensionBuilder::BackgroundContext::BACKGROUND_PAGE)
.Build();
EXPECT_TRUE(BackgroundInfo::HasBackgroundPage(extension.get()));
EXPECT_TRUE(BackgroundInfo::HasPersistentBackgroundPage(extension.get()));
EXPECT_FALSE(BackgroundInfo::IsServiceWorkerBased(extension.get()));
}
{
scoped_refptr<const Extension> extension =
ExtensionBuilder("event page")
.SetBackgroundPage(ExtensionBuilder::BackgroundPage::EVENT)
.SetBackgroundContext(
ExtensionBuilder::BackgroundContext::EVENT_PAGE)
.Build();
EXPECT_TRUE(BackgroundInfo::HasBackgroundPage(extension.get()));
EXPECT_TRUE(BackgroundInfo::HasLazyBackgroundPage(extension.get()));
EXPECT_FALSE(BackgroundInfo::IsServiceWorkerBased(extension.get()));
}
{
// Service Worker features are only available on the trunk.
// TODO(crbug.com/853378): Remove this when we open up support for
// service workers.
ScopedCurrentChannel current_channel_override(
version_info::Channel::UNKNOWN);
scoped_refptr<const Extension> extension =
ExtensionBuilder("service worker")
.SetBackgroundContext(
ExtensionBuilder::BackgroundContext::SERVICE_WORKER)
.Build();
EXPECT_FALSE(BackgroundInfo::HasBackgroundPage(extension.get()));
EXPECT_FALSE(BackgroundInfo::HasLazyBackgroundPage(extension.get()));
EXPECT_FALSE(BackgroundInfo::HasPersistentBackgroundPage(extension.get()));
EXPECT_TRUE(BackgroundInfo::IsServiceWorkerBased(extension.get()));
EXPECT_EQ(
ExtensionBuilder::kServiceWorkerScriptFile,
BackgroundInfo::GetBackgroundServiceWorkerScript(extension.get()));
}
}
......
......@@ -999,12 +999,10 @@ TEST(SimpleFeatureUnitTest, DisallowForServiceWorkers) {
feature.set_contexts({Feature::BLESSED_EXTENSION_CONTEXT});
feature.set_extension_types({Manifest::TYPE_EXTENSION});
constexpr char script_file[] = "script.js";
auto extension =
ExtensionBuilder("test")
.SetManifestPath({"background", "service_worker"}, script_file)
.Build();
auto extension = ExtensionBuilder("test")
.SetBackgroundContext(
ExtensionBuilder::BackgroundContext::SERVICE_WORKER)
.Build();
ASSERT_TRUE(extension.get());
EXPECT_TRUE(BackgroundInfo::IsServiceWorkerBased(extension.get()));
......@@ -1014,10 +1012,11 @@ TEST(SimpleFeatureUnitTest, DisallowForServiceWorkers) {
// these two EXPECTs.
EXPECT_EQ(Feature::INVALID_CONTEXT,
feature
.IsAvailableToContext(extension.get(),
Feature::BLESSED_EXTENSION_CONTEXT,
extension->GetResourceURL(script_file),
Feature::CHROMEOS_PLATFORM)
.IsAvailableToContext(
extension.get(), Feature::BLESSED_EXTENSION_CONTEXT,
extension->GetResourceURL(
ExtensionBuilder::kServiceWorkerScriptFile),
Feature::CHROMEOS_PLATFORM)
.result());
// Check with a different script file, which should return available,
......@@ -1034,10 +1033,11 @@ TEST(SimpleFeatureUnitTest, DisallowForServiceWorkers) {
feature.set_disallow_for_service_workers(false);
EXPECT_EQ(Feature::IS_AVAILABLE,
feature
.IsAvailableToContext(extension.get(),
Feature::BLESSED_EXTENSION_CONTEXT,
extension->GetResourceURL(script_file),
Feature::CHROMEOS_PLATFORM)
.IsAvailableToContext(
extension.get(), Feature::BLESSED_EXTENSION_CONTEXT,
extension->GetResourceURL(
ExtensionBuilder::kServiceWorkerScriptFile),
Feature::CHROMEOS_PLATFORM)
.result());
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment