Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_ | 5 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_ |
| 6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_ | 6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <iterator> | |
| 9 #include <map> | 10 #include <map> |
| 10 #include <string> | 11 #include <string> |
| 11 #include <vector> | |
| 12 | 12 |
| 13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "chrome/common/extensions/extension.h" | 15 #include "chrome/common/extensions/extension.h" |
| 16 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
| 17 | 17 |
| 18 // The one true extension container. Extensions are identified by their id. | 18 // The one true extension container. Extensions are identified by their id. |
| 19 // Only one extension can be in the set with a given ID. | 19 // Only one extension can be in the set with a given ID. |
| 20 class ExtensionSet { | 20 class ExtensionSet { |
| 21 public: | 21 public: |
| 22 typedef std::pair<FilePath, std::string> ExtensionPathAndDefaultLocale; | 22 typedef std::pair<FilePath, std::string> ExtensionPathAndDefaultLocale; |
| 23 typedef std::map<std::string, scoped_refptr<const Extension> > ExtensionMap; | 23 typedef std::map<std::string, scoped_refptr<const Extension> > ExtensionMap; |
| 24 typedef ExtensionMap::const_iterator const_iterator; | 24 |
| 25 // Iteration over the values of the map (given that it's an ExtensionSet, | |
| 26 // it should iterate like a set iterator). | |
|
Aaron Boodman
2011/12/01 08:27:12
Fair enough.
| |
| 27 class const_iterator : | |
| 28 public std::iterator<std::input_iterator_tag, | |
| 29 scoped_refptr<const Extension> > { | |
| 30 public: | |
| 31 const_iterator() {} | |
| 32 explicit const_iterator(ExtensionMap::const_iterator it) : | |
| 33 it_(it) {} | |
| 34 const_iterator& operator++() { | |
| 35 ++it_; | |
| 36 return *this; | |
| 37 } | |
| 38 const scoped_refptr<const Extension> operator*() { | |
| 39 return it_->second; | |
| 40 } | |
| 41 bool operator!=(const const_iterator& other) { return it_ != other.it_; } | |
| 42 bool operator==(const const_iterator& other) { return it_ == other.it_; } | |
| 43 | |
| 44 private: | |
| 45 ExtensionMap::const_iterator it_; | |
| 46 }; | |
| 25 | 47 |
| 26 ExtensionSet(); | 48 ExtensionSet(); |
| 27 ~ExtensionSet(); | 49 ~ExtensionSet(); |
| 28 | 50 |
| 29 // Gets the number of extensions contained. | |
| 30 size_t size() const; | 51 size_t size() const; |
| 52 bool empty() const; | |
| 31 | 53 |
| 32 // Iteration support. | 54 // Iteration support. |
| 33 const_iterator begin() const { return extensions_.begin(); } | 55 const_iterator begin() const { return const_iterator(extensions_.begin()); } |
| 34 const_iterator end() const { return extensions_.end(); } | 56 const_iterator end() const { return const_iterator(extensions_.end()); } |
| 35 | 57 |
| 36 // Returns true if the set contains the specified extension. | 58 // Returns true if the set contains the specified extension. |
| 37 bool Contains(const std::string& id) const; | 59 bool Contains(const std::string& id) const; |
| 38 | 60 |
| 39 // Adds the specified extension to the set. The set becomes an owner. Any | 61 // Adds the specified extension to the set. The set becomes an owner. Any |
| 40 // previous extension with the same ID is removed. | 62 // previous extension with the same ID is removed. |
| 41 void Insert(const scoped_refptr<const Extension>& extension); | 63 void Insert(const scoped_refptr<const Extension>& extension); |
| 42 | 64 |
| 43 // Removes the specified extension. | 65 // Removes the specified extension. |
| 44 void Remove(const std::string& id); | 66 void Remove(const std::string& id); |
| 45 | 67 |
| 68 // Removes all extensions. | |
| 69 void Clear(); | |
| 70 | |
| 46 // Returns the extension ID that the given URL is a part of, or empty if | 71 // Returns the extension ID that the given URL is a part of, or empty if |
| 47 // none. This includes web URLs that are part of an extension's web extent. | 72 // none. This includes web URLs that are part of an extension's web extent. |
| 48 std::string GetIdByURL(const GURL& url) const; | 73 std::string GetIDByURL(const GURL& url) const; |
| 49 | 74 |
| 50 // Returns the Extension that the given URL is a part of, or NULL if none. | 75 // Returns the Extension that the given URL is a part of, or NULL if none. |
| 51 // This includes web URLs that are part of an extension's web extent. | 76 // This includes web URLs that are part of an extension's web extent. |
| 52 // NOTE: This can return NULL if called before UpdateExtensions receives | 77 // NOTE: This can return NULL if called before UpdateExtensions receives |
| 53 // bulk extension data (e.g. if called from | 78 // bulk extension data (e.g. if called from |
| 54 // EventBindings::HandleContextCreated) | 79 // EventBindings::HandleContextCreated) |
| 55 const Extension* GetByURL(const GURL& url) const; | 80 const Extension* GetByURL(const GURL& url) const; |
| 56 | 81 |
| 57 // Returns true if |new_url| is in the extent of the same extension as | 82 // Returns true if |new_url| is in the extent of the same extension as |
| 58 // |old_url|. Also returns true if neither URL is in an app. | 83 // |old_url|. Also returns true if neither URL is in an app. |
| 59 bool InSameExtent(const GURL& old_url, const GURL& new_url) const; | 84 bool InSameExtent(const GURL& old_url, const GURL& new_url) const; |
| 60 | 85 |
| 61 // Look up an Extension object by id. | 86 // Look up an Extension object by id. |
| 62 const Extension* GetByID(const std::string& id) const; | 87 const Extension* GetByID(const std::string& id) const; |
| 63 | 88 |
| 64 // Returns true if |url| should get extension api bindings and be permitted | 89 // Returns true if |url| should get extension api bindings and be permitted |
| 65 // to make api calls. Note that this is independent of what extension | 90 // to make api calls. Note that this is independent of what extension |
| 66 // permissions the given extension has been granted. | 91 // permissions the given extension has been granted. |
| 67 bool ExtensionBindingsAllowed(const GURL& url) const; | 92 bool ExtensionBindingsAllowed(const GURL& url) const; |
| 68 | 93 |
| 69 private: | 94 private: |
| 70 FRIEND_TEST_ALL_PREFIXES(ExtensionSetTest, ExtensionSet); | 95 FRIEND_TEST_ALL_PREFIXES(ExtensionSetTest, ExtensionSet); |
| 71 | 96 |
| 72 ExtensionMap extensions_; | 97 ExtensionMap extensions_; |
| 73 | 98 |
| 74 DISALLOW_COPY_AND_ASSIGN(ExtensionSet); | 99 DISALLOW_COPY_AND_ASSIGN(ExtensionSet); |
| 75 }; | 100 }; |
| 76 | 101 |
| 77 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_ | 102 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_ |
| OLD | NEW |