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) { ...@@ -117,7 +117,7 @@ TEST_F(ExtensionHooksDelegateTest, SendRequestDisabled) {
// extension with an event page). // extension with an event page).
scoped_refptr<const Extension> extension = scoped_refptr<const Extension> extension =
ExtensionBuilder("foo") ExtensionBuilder("foo")
.SetBackgroundPage(ExtensionBuilder::BackgroundPage::EVENT) .SetBackgroundContext(ExtensionBuilder::BackgroundContext::EVENT_PAGE)
.SetLocation(Manifest::UNPACKED) .SetLocation(Manifest::UNPACKED)
.Build(); .Build();
RegisterExtension(extension); RegisterExtension(extension);
......
...@@ -92,7 +92,8 @@ class LazyBackgroundTaskQueueTest : public ExtensionsTest { ...@@ -92,7 +92,8 @@ class LazyBackgroundTaskQueueTest : public ExtensionsTest {
scoped_refptr<const Extension> CreateLazyBackgroundExtension() { scoped_refptr<const Extension> CreateLazyBackgroundExtension() {
scoped_refptr<const Extension> extension = scoped_refptr<const Extension> extension =
ExtensionBuilder("Lazy background") ExtensionBuilder("Lazy background")
.SetBackgroundPage(ExtensionBuilder::BackgroundPage::EVENT) .SetBackgroundContext(
ExtensionBuilder::BackgroundContext::EVENT_PAGE)
.SetID("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb") .SetID("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
.Build(); .Build();
ExtensionRegistry::Get(browser_context())->AddEnabled(extension); ExtensionRegistry::Get(browser_context())->AddEnabled(extension);
...@@ -222,7 +223,7 @@ TEST_F(LazyBackgroundTaskQueueTest, CreateLazyBackgroundPageOnExtensionLoaded) { ...@@ -222,7 +223,7 @@ TEST_F(LazyBackgroundTaskQueueTest, CreateLazyBackgroundPageOnExtensionLoaded) {
scoped_refptr<const Extension> lazy_background = scoped_refptr<const Extension> lazy_background =
ExtensionBuilder("Lazy background") ExtensionBuilder("Lazy background")
.SetBackgroundPage(ExtensionBuilder::BackgroundPage::EVENT) .SetBackgroundContext(ExtensionBuilder::BackgroundContext::EVENT_PAGE)
.SetID("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb") .SetID("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
.Build(); .Build();
......
...@@ -20,7 +20,8 @@ namespace { ...@@ -20,7 +20,8 @@ namespace {
// Creates a very simple extension with a background page. // Creates a very simple extension with a background page.
scoped_refptr<const Extension> CreateExtensionWithBackgroundPage() { scoped_refptr<const Extension> CreateExtensionWithBackgroundPage() {
return ExtensionBuilder("test") return ExtensionBuilder("test")
.SetBackgroundPage(ExtensionBuilder::BackgroundPage::PERSISTENT) .SetBackgroundContext(
ExtensionBuilder::BackgroundContext::BACKGROUND_PAGE)
.SetID("id2") .SetID("id2")
.Build(); .Build();
} }
......
...@@ -13,12 +13,14 @@ ...@@ -13,12 +13,14 @@
namespace extensions { namespace extensions {
constexpr char ExtensionBuilder::kServiceWorkerScriptFile[];
struct ExtensionBuilder::ManifestData { struct ExtensionBuilder::ManifestData {
Type type; Type type;
std::string name; std::string name;
std::vector<std::string> permissions; std::vector<std::string> permissions;
base::Optional<ActionType> action; base::Optional<ActionType> action;
base::Optional<BackgroundPage> background_page; base::Optional<BackgroundContext> background_context;
base::Optional<std::string> version; base::Optional<std::string> version;
// A ContentScriptEntry includes a string name, and a vector of string // A ContentScriptEntry includes a string name, and a vector of string
...@@ -68,19 +70,25 @@ struct ExtensionBuilder::ManifestData { ...@@ -68,19 +70,25 @@ struct ExtensionBuilder::ManifestData {
manifest.Set(action_key, std::make_unique<base::DictionaryValue>()); manifest.Set(action_key, std::make_unique<base::DictionaryValue>());
} }
if (background_page) { if (background_context) {
DictionaryBuilder background; DictionaryBuilder background;
base::Optional<bool> persistent;
switch (*background_context) {
case BackgroundContext::BACKGROUND_PAGE:
background.Set("page", "background_page.html"); background.Set("page", "background_page.html");
bool persistent = false;
switch (*background_page) {
case BackgroundPage::PERSISTENT:
persistent = true; persistent = true;
break; break;
case BackgroundPage::EVENT: case BackgroundContext::EVENT_PAGE:
background.Set("page", "background_page.html");
persistent = false; persistent = false;
break; 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()); manifest.Set("background", background.Build());
} }
...@@ -172,10 +180,10 @@ ExtensionBuilder& ExtensionBuilder::SetAction(ActionType action) { ...@@ -172,10 +180,10 @@ ExtensionBuilder& ExtensionBuilder::SetAction(ActionType action) {
return *this; return *this;
} }
ExtensionBuilder& ExtensionBuilder::SetBackgroundPage( ExtensionBuilder& ExtensionBuilder::SetBackgroundContext(
BackgroundPage background_page) { BackgroundContext background_context) {
CHECK(manifest_data_); CHECK(manifest_data_);
manifest_data_->background_page = background_page; manifest_data_->background_context = background_context;
return *this; return *this;
} }
......
...@@ -50,11 +50,14 @@ class ExtensionBuilder { ...@@ -50,11 +50,14 @@ class ExtensionBuilder {
BROWSER_ACTION, BROWSER_ACTION,
}; };
enum class BackgroundPage { enum class BackgroundContext {
PERSISTENT, BACKGROUND_PAGE,
EVENT, EVENT_PAGE,
SERVICE_WORKER,
}; };
static constexpr char kServiceWorkerScriptFile[] = "sw.js";
// Initializes an ExtensionBuilder that can be used with SetManifest() for // Initializes an ExtensionBuilder that can be used with SetManifest() for
// complete customization. // complete customization.
ExtensionBuilder(); ExtensionBuilder();
...@@ -85,9 +88,8 @@ class ExtensionBuilder { ...@@ -85,9 +88,8 @@ class ExtensionBuilder {
// be set (though note that we synthesize a page action for most extensions). // be set (though note that we synthesize a page action for most extensions).
ExtensionBuilder& SetAction(ActionType action); ExtensionBuilder& SetAction(ActionType action);
// Sets a background page for the extension to have. By default, no background // Sets a background context for the extension. By default, none will be set.
// page will be set. ExtensionBuilder& SetBackgroundContext(BackgroundContext background_context);
ExtensionBuilder& SetBackgroundPage(BackgroundPage background_page);
// Adds a content script to the extension, with a script with the specified // Adds a content script to the extension, with a script with the specified
// |script_name| that matches the given |match_patterns|. // |script_name| that matches the given |match_patterns|.
......
...@@ -5,7 +5,9 @@ ...@@ -5,7 +5,9 @@
#include "extensions/common/extension_builder.h" #include "extensions/common/extension_builder.h"
#include "base/version.h" #include "base/version.h"
#include "components/version_info/channel.h"
#include "extensions/common/extension.h" #include "extensions/common/extension.h"
#include "extensions/common/features/feature_channel.h"
#include "extensions/common/manifest_constants.h" #include "extensions/common/manifest_constants.h"
#include "extensions/common/manifest_handlers/background_info.h" #include "extensions/common/manifest_handlers/background_info.h"
#include "extensions/common/manifest_handlers/content_scripts_handler.h" #include "extensions/common/manifest_handlers/content_scripts_handler.h"
...@@ -90,18 +92,41 @@ TEST(ExtensionBuilderTest, Background) { ...@@ -90,18 +92,41 @@ TEST(ExtensionBuilderTest, Background) {
{ {
scoped_refptr<const Extension> extension = scoped_refptr<const Extension> extension =
ExtensionBuilder("persistent background page") ExtensionBuilder("persistent background page")
.SetBackgroundPage(ExtensionBuilder::BackgroundPage::PERSISTENT) .SetBackgroundContext(
ExtensionBuilder::BackgroundContext::BACKGROUND_PAGE)
.Build(); .Build();
EXPECT_TRUE(BackgroundInfo::HasBackgroundPage(extension.get())); EXPECT_TRUE(BackgroundInfo::HasBackgroundPage(extension.get()));
EXPECT_TRUE(BackgroundInfo::HasPersistentBackgroundPage(extension.get())); EXPECT_TRUE(BackgroundInfo::HasPersistentBackgroundPage(extension.get()));
EXPECT_FALSE(BackgroundInfo::IsServiceWorkerBased(extension.get()));
} }
{ {
scoped_refptr<const Extension> extension = scoped_refptr<const Extension> extension =
ExtensionBuilder("event page") ExtensionBuilder("event page")
.SetBackgroundPage(ExtensionBuilder::BackgroundPage::EVENT) .SetBackgroundContext(
ExtensionBuilder::BackgroundContext::EVENT_PAGE)
.Build(); .Build();
EXPECT_TRUE(BackgroundInfo::HasBackgroundPage(extension.get())); EXPECT_TRUE(BackgroundInfo::HasBackgroundPage(extension.get()));
EXPECT_TRUE(BackgroundInfo::HasLazyBackgroundPage(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,11 +999,9 @@ TEST(SimpleFeatureUnitTest, DisallowForServiceWorkers) { ...@@ -999,11 +999,9 @@ TEST(SimpleFeatureUnitTest, DisallowForServiceWorkers) {
feature.set_contexts({Feature::BLESSED_EXTENSION_CONTEXT}); feature.set_contexts({Feature::BLESSED_EXTENSION_CONTEXT});
feature.set_extension_types({Manifest::TYPE_EXTENSION}); feature.set_extension_types({Manifest::TYPE_EXTENSION});
constexpr char script_file[] = "script.js"; auto extension = ExtensionBuilder("test")
.SetBackgroundContext(
auto extension = ExtensionBuilder::BackgroundContext::SERVICE_WORKER)
ExtensionBuilder("test")
.SetManifestPath({"background", "service_worker"}, script_file)
.Build(); .Build();
ASSERT_TRUE(extension.get()); ASSERT_TRUE(extension.get());
EXPECT_TRUE(BackgroundInfo::IsServiceWorkerBased(extension.get())); EXPECT_TRUE(BackgroundInfo::IsServiceWorkerBased(extension.get()));
...@@ -1014,9 +1012,10 @@ TEST(SimpleFeatureUnitTest, DisallowForServiceWorkers) { ...@@ -1014,9 +1012,10 @@ TEST(SimpleFeatureUnitTest, DisallowForServiceWorkers) {
// these two EXPECTs. // these two EXPECTs.
EXPECT_EQ(Feature::INVALID_CONTEXT, EXPECT_EQ(Feature::INVALID_CONTEXT,
feature feature
.IsAvailableToContext(extension.get(), .IsAvailableToContext(
Feature::BLESSED_EXTENSION_CONTEXT, extension.get(), Feature::BLESSED_EXTENSION_CONTEXT,
extension->GetResourceURL(script_file), extension->GetResourceURL(
ExtensionBuilder::kServiceWorkerScriptFile),
Feature::CHROMEOS_PLATFORM) Feature::CHROMEOS_PLATFORM)
.result()); .result());
...@@ -1034,9 +1033,10 @@ TEST(SimpleFeatureUnitTest, DisallowForServiceWorkers) { ...@@ -1034,9 +1033,10 @@ TEST(SimpleFeatureUnitTest, DisallowForServiceWorkers) {
feature.set_disallow_for_service_workers(false); feature.set_disallow_for_service_workers(false);
EXPECT_EQ(Feature::IS_AVAILABLE, EXPECT_EQ(Feature::IS_AVAILABLE,
feature feature
.IsAvailableToContext(extension.get(), .IsAvailableToContext(
Feature::BLESSED_EXTENSION_CONTEXT, extension.get(), Feature::BLESSED_EXTENSION_CONTEXT,
extension->GetResourceURL(script_file), extension->GetResourceURL(
ExtensionBuilder::kServiceWorkerScriptFile),
Feature::CHROMEOS_PLATFORM) Feature::CHROMEOS_PLATFORM)
.result()); .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