diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-29 01:11:36 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-29 01:11:36 +0000 |
commit | ec7db28ad27a4285b43fb53e63c2b932fe6c3a7d (patch) | |
tree | 05799bb8c07843b846194ab26185ef26bab4e433 /chrome/common | |
parent | 4c8179009b83183fe99576e561f5c045bac8c589 (diff) | |
download | chromium_src-ec7db28ad27a4285b43fb53e63c2b932fe6c3a7d.zip chromium_src-ec7db28ad27a4285b43fb53e63c2b932fe6c3a7d.tar.gz chromium_src-ec7db28ad27a4285b43fb53e63c2b932fe6c3a7d.tar.bz2 |
Move ExtensionRendereInfo into common and call it ExtensionSet instead.
BUG=
TEST=
Review URL: http://codereview.chromium.org/6383013
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@73055 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common')
-rw-r--r-- | chrome/common/extensions/extension_set.cc | 81 | ||||
-rw-r--r-- | chrome/common/extensions/extension_set.h | 70 | ||||
-rw-r--r-- | chrome/common/extensions/extension_set_unittest.cc | 118 |
3 files changed, 269 insertions, 0 deletions
diff --git a/chrome/common/extensions/extension_set.cc b/chrome/common/extensions/extension_set.cc new file mode 100644 index 0000000..fcf96e6 --- /dev/null +++ b/chrome/common/extensions/extension_set.cc @@ -0,0 +1,81 @@ +// Copyright (c) 2011 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 "chrome/common/extensions/extension_set.h" + +#include "base/logging.h" +#include "chrome/common/url_constants.h" + +ExtensionSet::ExtensionSet() { +} + +ExtensionSet::~ExtensionSet() { +} + +size_t ExtensionSet::size() const { + return extensions_.size(); +} + +bool ExtensionSet::Contains(const std::string& extension_id) { + return extensions_.find(extension_id) != extensions_.end(); +} + +void ExtensionSet::Insert(const scoped_refptr<const Extension>& extension) { + extensions_[extension->id()] = extension; +} + +void ExtensionSet::Remove(const std::string& id) { + extensions_.erase(id); +} + +std::string ExtensionSet::GetIdByURL(const GURL& url) const { + if (url.SchemeIs(chrome::kExtensionScheme)) + return url.host(); + + const Extension* extension = GetByURL(url); + if (!extension) + return ""; + + return extension->id(); +} + +const Extension* ExtensionSet::GetByURL(const GURL& url) const { + if (url.SchemeIs(chrome::kExtensionScheme)) + return GetByID(url.host()); + + ExtensionMap::const_iterator i = extensions_.begin(); + for (; i != extensions_.end(); ++i) { + if (i->second->web_extent().ContainsURL(url)) + return i->second.get(); + } + + return NULL; +} + +bool ExtensionSet::InSameExtent(const GURL& old_url, + const GURL& new_url) const { + return GetByURL(old_url) == GetByURL(new_url); +} + +const Extension* ExtensionSet::GetByID(const std::string& id) const { + ExtensionMap::const_iterator i = extensions_.find(id); + if (i != extensions_.end()) + return i->second.get(); + else + return NULL; +} + +bool ExtensionSet::ExtensionBindingsAllowed(const GURL& url) const { + if (url.SchemeIs(chrome::kExtensionScheme)) + return true; + + ExtensionMap::const_iterator i = extensions_.begin(); + for (; i != extensions_.end(); ++i) { + if (i->second->location() == Extension::COMPONENT && + i->second->web_extent().ContainsURL(url)) + return true; + } + + return false; +} diff --git a/chrome/common/extensions/extension_set.h b/chrome/common/extensions/extension_set.h new file mode 100644 index 0000000..9d00d1d --- /dev/null +++ b/chrome/common/extensions/extension_set.h @@ -0,0 +1,70 @@ +// Copyright (c) 2011 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. + +#ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_ +#define CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_ +#pragma once + +#include <string> +#include <vector> + +#include "base/gtest_prod_util.h" +#include "base/ref_counted.h" +#include "chrome/common/extensions/extension.h" +#include "googleurl/src/gurl.h" + +// The one true extension container. Extensions are identified by their id. +// Only one extension can be in the set with a given ID. +class ExtensionSet { + public: + ExtensionSet(); + ~ExtensionSet(); + + // Gets the number of extensions contained. + size_t size() const; + + // Returns true if the set contains the specified extension. + bool Contains(const std::string& id); + + // Adds the specified extension to the set. The set becomes an owner. Any + // previous extension with the same ID is removed. + void Insert(const scoped_refptr<const Extension>& extension); + + // Removes the specified extension. + void Remove(const std::string& id); + + // Returns the extension ID that the given URL is a part of, or empty if + // none. This includes web URLs that are part of an extension's web extent. + std::string GetIdByURL(const GURL& url) const; + + // Returns the Extension that the given URL is a part of, or NULL if none. + // This includes web URLs that are part of an extension's web extent. + // NOTE: This can return NULL if called before UpdateExtensions receives + // bulk extension data (e.g. if called from + // EventBindings::HandleContextCreated) + const Extension* GetByURL(const GURL& url) const; + + // Returns true if |new_url| is in the extent of the same extension as + // |old_url|. Also returns true if neither URL is in an app. + bool InSameExtent(const GURL& old_url, const GURL& new_url) const; + + // Look up an Extension object by id. + const Extension* GetByID(const std::string& id) const; + + // Returns true if |url| should get extension api bindings and be permitted + // to make api calls. Note that this is independent of what extension + // permissions the given extension has been granted. + bool ExtensionBindingsAllowed(const GURL& url) const; + + private: + FRIEND_TEST_ALL_PREFIXES(ExtensionSetTest, ExtensionSet); + + // static + typedef std::map<std::string, scoped_refptr<const Extension> > ExtensionMap; + ExtensionMap extensions_; + + DISALLOW_COPY_AND_ASSIGN(ExtensionSet); +}; + +#endif // CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_ diff --git a/chrome/common/extensions/extension_set_unittest.cc b/chrome/common/extensions/extension_set_unittest.cc new file mode 100644 index 0000000..1b85ac5 --- /dev/null +++ b/chrome/common/extensions/extension_set_unittest.cc @@ -0,0 +1,118 @@ +// Copyright (c) 2011 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 "base/file_path.h" +#include "base/logging.h" +#include "base/ref_counted.h" +#include "base/scoped_ptr.h" +#include "base/values.h" +#include "chrome/common/extensions/extension.h" +#include "chrome/common/extensions/extension_set.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +scoped_refptr<Extension> CreateTestExtension(const std::string& name, + const std::string& launch_url, + const std::string& extent) { +#if defined(OS_WIN) + FilePath path(FILE_PATH_LITERAL("c:\\")); +#else + FilePath path(FILE_PATH_LITERAL("/")); +#endif + path = path.AppendASCII(name); + + DictionaryValue manifest; + manifest.SetString("name", name); + manifest.SetString("version", "1"); + + if (!launch_url.empty()) + manifest.SetString("app.launch.web_url", launch_url); + + if (!extent.empty()) { + ListValue* urls = new ListValue(); + manifest.Set("app.urls", urls); + urls->Append(Value::CreateStringValue(extent)); + } + + const bool kRequireKey = false; + std::string error; + scoped_refptr<Extension> extension( + Extension::Create(path, Extension::INTERNAL, manifest, kRequireKey, + &error)); + EXPECT_TRUE(extension.get()) << error; + return extension; +} + +} // namespace + +TEST(ExtensionSetTest, ExtensionSet) { + scoped_refptr<Extension> ext1(CreateTestExtension( + "a", "https://chrome.google.com/launch", "https://chrome.google.com/")); + + scoped_refptr<Extension> ext2(CreateTestExtension( + "a", "http://code.google.com/p/chromium", + "http://code.google.com/p/chromium/")); + + scoped_refptr<Extension> ext3(CreateTestExtension( + "b", "http://dev.chromium.org/", "http://dev.chromium.org/")); + + scoped_refptr<Extension> ext4(CreateTestExtension("c", "", "")); + + ASSERT_TRUE(ext1 && ext2 && ext3 && ext4); + + ExtensionSet extensions; + + // Add an extension. + extensions.Insert(ext1); + EXPECT_EQ(1u, extensions.size()); + EXPECT_EQ(ext1, extensions.GetByID(ext1->id())); + + // Since extension2 has same ID, it should overwrite extension1. + extensions.Insert(ext2); + EXPECT_EQ(1u, extensions.size()); + EXPECT_EQ(ext2, extensions.GetByID(ext1->id())); + + // Add the other extensions. + extensions.Insert(ext3); + extensions.Insert(ext4); + EXPECT_EQ(3u, extensions.size()); + + // Get extension by its chrome-extension:// URL + EXPECT_EQ(ext2, extensions.GetByURL( + ext2->GetResourceURL("test.html"))); + EXPECT_EQ(ext3, extensions.GetByURL( + ext3->GetResourceURL("test.html"))); + EXPECT_EQ(ext4, extensions.GetByURL( + ext4->GetResourceURL("test.html"))); + + // Get extension by web extent. + EXPECT_EQ(ext2, extensions.GetByURL( + GURL("http://code.google.com/p/chromium/monkey"))); + EXPECT_EQ(ext3, extensions.GetByURL( + GURL("http://dev.chromium.org/design-docs/"))); + EXPECT_FALSE(extensions.GetByURL( + GURL("http://blog.chromium.org/"))); + + // Test InSameExtent(). + EXPECT_TRUE(extensions.InSameExtent( + GURL("http://code.google.com/p/chromium/monkey/"), + GURL("http://code.google.com/p/chromium/"))); + EXPECT_FALSE(extensions.InSameExtent( + GURL("http://code.google.com/p/chromium/"), + GURL("https://code.google.com/p/chromium/"))); + EXPECT_FALSE(extensions.InSameExtent( + GURL("http://code.google.com/p/chromium/"), + GURL("http://dev.chromium.org/design-docs/"))); + + // Both of these should be NULL, which mean true for InSameExtent. + EXPECT_TRUE(extensions.InSameExtent( + GURL("http://www.google.com/"), + GURL("http://blog.chromium.org/"))); + + // Remove one of the extensions. + extensions.Remove(ext2->id()); + EXPECT_EQ(2u, extensions.size()); + EXPECT_FALSE(extensions.GetByID(ext2->id())); +} |