diff options
author | kundaji <kundaji@chromium.org> | 2015-09-18 16:19:09 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-09-18 23:19:46 +0000 |
commit | e548e74412d0a18f8829ce60201eb953a700b09b (patch) | |
tree | 4ead1264cfb19591b40be5177c35ba7bbb473970 /extensions/common/manifest_handlers | |
parent | 7305eb15590a4a3b363eeed2f1ed1b8638ee37fe (diff) | |
download | chromium_src-e548e74412d0a18f8829ce60201eb953a700b09b.zip chromium_src-e548e74412d0a18f8829ce60201eb953a700b09b.tar.gz chromium_src-e548e74412d0a18f8829ce60201eb953a700b09b.tar.bz2 |
Allow extensions to specify that they are not allowed in incognito mode.
Add "not_allowed" as a valid value to the "incognito" field in the
manifest. Track incognito behavior using an enum instead of a boolean
to distinguish between "split", "spanning", and "not_allowed". Add
CanBeIncognitoEnabled method to utils which checks the manifest entry
in addition to checking if extension is platform app or component.
BUG=455756
Review URL: https://codereview.chromium.org/1351223003
Cr-Commit-Position: refs/heads/master@{#349806}
Diffstat (limited to 'extensions/common/manifest_handlers')
3 files changed, 78 insertions, 19 deletions
diff --git a/extensions/common/manifest_handlers/incognito_info.cc b/extensions/common/manifest_handlers/incognito_info.cc index 02c3b28..b7dc8b2 100644 --- a/extensions/common/manifest_handlers/incognito_info.cc +++ b/extensions/common/manifest_handlers/incognito_info.cc @@ -14,9 +14,7 @@ namespace extensions { namespace keys = manifest_keys; -IncognitoInfo::IncognitoInfo(bool incognito_split_mode) - : split_mode(incognito_split_mode) { -} +IncognitoInfo::IncognitoInfo(Mode mode) : mode(mode) {} IncognitoInfo::~IncognitoInfo() { } @@ -25,7 +23,14 @@ IncognitoInfo::~IncognitoInfo() { bool IncognitoInfo::IsSplitMode(const Extension* extension) { IncognitoInfo* info = static_cast<IncognitoInfo*>( extension->GetManifestData(keys::kIncognito)); - return info ? info->split_mode : false; + return info ? info->mode == Mode::SPLIT : false; +} + +// static +bool IncognitoInfo::IsIncognitoAllowed(const Extension* extension) { + IncognitoInfo* info = + static_cast<IncognitoInfo*>(extension->GetManifestData(keys::kIncognito)); + return info ? info->mode != Mode::NOT_ALLOWED : true; } IncognitoHandler::IncognitoHandler() { @@ -35,33 +40,35 @@ IncognitoHandler::~IncognitoHandler() { } bool IncognitoHandler::Parse(Extension* extension, base::string16* error) { + // Extensions and Chrome apps default to spanning mode. + // Hosted and legacy packaged apps default to split mode. + IncognitoInfo::Mode mode = + extension->is_hosted_app() || extension->is_legacy_packaged_app() + ? IncognitoInfo::Mode::SPLIT + : IncognitoInfo::Mode::SPANNING; if (!extension->manifest()->HasKey(keys::kIncognito)) { - // Extensions and Chrome apps default to spanning mode. - // Hosted and legacy packaged apps default to split mode. - extension->SetManifestData( - keys::kIncognito, - new IncognitoInfo(extension->is_hosted_app() || - extension->is_legacy_packaged_app())); + extension->SetManifestData(keys::kIncognito, new IncognitoInfo(mode)); return true; } - bool split_mode = false; std::string incognito_string; if (!extension->manifest()->GetString(keys::kIncognito, &incognito_string)) { *error = base::ASCIIToUTF16(manifest_errors::kInvalidIncognitoBehavior); return false; } - if (incognito_string == manifest_values::kIncognitoSplit) - split_mode = true; - else if (incognito_string != manifest_values::kIncognitoSpanning) { - // If incognito_string == kIncognitoSpanning, it is valid and - // split_mode remains false. + if (incognito_string == manifest_values::kIncognitoSplit) { + mode = IncognitoInfo::Mode::SPLIT; + } else if (incognito_string == manifest_values::kIncognitoSpanning) { + mode = IncognitoInfo::Mode::SPANNING; + } else if (incognito_string == manifest_values::kIncognitoNotAllowed) { + mode = IncognitoInfo::Mode::NOT_ALLOWED; + } else { *error = base::ASCIIToUTF16(manifest_errors::kInvalidIncognitoBehavior); return false; } - extension->SetManifestData(keys::kIncognito, new IncognitoInfo(split_mode)); + extension->SetManifestData(keys::kIncognito, new IncognitoInfo(mode)); return true; } diff --git a/extensions/common/manifest_handlers/incognito_info.h b/extensions/common/manifest_handlers/incognito_info.h index 0b7a870..c217a23 100644 --- a/extensions/common/manifest_handlers/incognito_info.h +++ b/extensions/common/manifest_handlers/incognito_info.h @@ -12,15 +12,22 @@ namespace extensions { struct IncognitoInfo : public Extension::ManifestData { - explicit IncognitoInfo(bool split_mode); + enum Mode { SPLIT, SPANNING, NOT_ALLOWED }; + + explicit IncognitoInfo(Mode mode); + ~IncognitoInfo() override; // If true, a separate process will be used for the extension in incognito // mode. - bool split_mode; + Mode mode; // Return the incognito mode information for the given |extension|. static bool IsSplitMode(const Extension* extension); + + // Return whether this extension can be run in incognito mode as specified + // in its manifest. + static bool IsIncognitoAllowed(const Extension* extension); }; // Parses the "incognito" manifest key. diff --git a/extensions/common/manifest_handlers/incognito_manifest_unittest.cc b/extensions/common/manifest_handlers/incognito_manifest_unittest.cc new file mode 100644 index 0000000..0551162 --- /dev/null +++ b/extensions/common/manifest_handlers/incognito_manifest_unittest.cc @@ -0,0 +1,45 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "extensions/browser/extension_util.h" +#include "extensions/common/extension.h" +#include "extensions/common/manifest_handlers/incognito_info.h" +#include "extensions/common/manifest_test.h" + +namespace extensions { + +using IncognitoManifestTest = ManifestTest; + +TEST_F(IncognitoManifestTest, IncognitoNotAllowed) { + scoped_refptr<Extension> extension( + LoadAndExpectSuccess("incognito_not_allowed.json")); + EXPECT_FALSE(IncognitoInfo::IsIncognitoAllowed(extension.get())); + EXPECT_FALSE(IncognitoInfo::IsSplitMode(extension.get())); + EXPECT_FALSE(util::CanBeIncognitoEnabled(extension.get())); +} + +TEST_F(IncognitoManifestTest, IncognitoSpanning) { + scoped_refptr<Extension> extension( + LoadAndExpectSuccess("incognito_spanning.json")); + EXPECT_TRUE(IncognitoInfo::IsIncognitoAllowed(extension.get())); + EXPECT_FALSE(IncognitoInfo::IsSplitMode(extension.get())); + EXPECT_TRUE(util::CanBeIncognitoEnabled(extension.get())); +} + +TEST_F(IncognitoManifestTest, IncognitoSplit) { + scoped_refptr<Extension> extension( + LoadAndExpectSuccess("incognito_split.json")); + EXPECT_TRUE(IncognitoInfo::IsIncognitoAllowed(extension.get())); + EXPECT_TRUE(IncognitoInfo::IsSplitMode(extension.get())); + EXPECT_TRUE(util::CanBeIncognitoEnabled(extension.get())); +} + +TEST_F(IncognitoManifestTest, IncognitoUnspecified) { + scoped_refptr<Extension> extension(LoadAndExpectSuccess("minimal.json")); + EXPECT_TRUE(IncognitoInfo::IsIncognitoAllowed(extension.get())); + EXPECT_FALSE(IncognitoInfo::IsSplitMode(extension.get())); + EXPECT_TRUE(util::CanBeIncognitoEnabled(extension.get())); +} + +} // namespace extensions |