| 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> | |
| 10 #include <map> | 9 #include <map> |
| 11 #include <string> | 10 #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 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
| 18 | 18 |
| 19 class ExtensionURLInfo { | 19 class ExtensionURLInfo { |
| 20 public: | 20 public: |
| 21 // The extension system uses both a document's origin and its URL to | 21 // The extension system uses both a document's origin and its URL to |
| (...skipping 15 matching lines...) Expand all Loading... |
| 37 WebKit::WebSecurityOrigin origin_; | 37 WebKit::WebSecurityOrigin origin_; |
| 38 GURL url_; | 38 GURL url_; |
| 39 }; | 39 }; |
| 40 | 40 |
| 41 // The one true extension container. Extensions are identified by their id. | 41 // The one true extension container. Extensions are identified by their id. |
| 42 // Only one extension can be in the set with a given ID. | 42 // Only one extension can be in the set with a given ID. |
| 43 class ExtensionSet { | 43 class ExtensionSet { |
| 44 public: | 44 public: |
| 45 typedef std::pair<FilePath, std::string> ExtensionPathAndDefaultLocale; | 45 typedef std::pair<FilePath, std::string> ExtensionPathAndDefaultLocale; |
| 46 typedef std::map<std::string, scoped_refptr<const Extension> > ExtensionMap; | 46 typedef std::map<std::string, scoped_refptr<const Extension> > ExtensionMap; |
| 47 | 47 typedef ExtensionMap::const_iterator const_iterator; |
| 48 // Iteration over the values of the map (given that it's an ExtensionSet, | |
| 49 // it should iterate like a set iterator). | |
| 50 class const_iterator : | |
| 51 public std::iterator<std::input_iterator_tag, | |
| 52 scoped_refptr<const Extension> > { | |
| 53 public: | |
| 54 const_iterator() {} | |
| 55 explicit const_iterator(ExtensionMap::const_iterator it) : | |
| 56 it_(it) {} | |
| 57 const_iterator& operator++() { | |
| 58 ++it_; | |
| 59 return *this; | |
| 60 } | |
| 61 const scoped_refptr<const Extension> operator*() { | |
| 62 return it_->second; | |
| 63 } | |
| 64 bool operator!=(const const_iterator& other) { return it_ != other.it_; } | |
| 65 bool operator==(const const_iterator& other) { return it_ == other.it_; } | |
| 66 | |
| 67 private: | |
| 68 ExtensionMap::const_iterator it_; | |
| 69 }; | |
| 70 | 48 |
| 71 ExtensionSet(); | 49 ExtensionSet(); |
| 72 ~ExtensionSet(); | 50 ~ExtensionSet(); |
| 73 | 51 |
| 52 // Gets the number of extensions contained. |
| 74 size_t size() const; | 53 size_t size() const; |
| 75 bool is_empty() const; | |
| 76 | 54 |
| 77 // Iteration support. | 55 // Iteration support. |
| 78 const_iterator begin() const { return const_iterator(extensions_.begin()); } | 56 const_iterator begin() const { return extensions_.begin(); } |
| 79 const_iterator end() const { return const_iterator(extensions_.end()); } | 57 const_iterator end() const { return extensions_.end(); } |
| 80 | 58 |
| 81 // Returns true if the set contains the specified extension. | 59 // Returns true if the set contains the specified extension. |
| 82 bool Contains(const std::string& id) const; | 60 bool Contains(const std::string& id) const; |
| 83 | 61 |
| 84 // Adds the specified extension to the set. The set becomes an owner. Any | 62 // Adds the specified extension to the set. The set becomes an owner. Any |
| 85 // previous extension with the same ID is removed. | 63 // previous extension with the same ID is removed. |
| 86 void Insert(const scoped_refptr<const Extension>& extension); | 64 void Insert(const scoped_refptr<const Extension>& extension); |
| 87 | 65 |
| 88 // Removes the specified extension. | 66 // Removes the specified extension. |
| 89 void Remove(const std::string& id); | 67 void Remove(const std::string& id); |
| 90 | 68 |
| 91 // Removes all extensions. | |
| 92 void Clear(); | |
| 93 | |
| 94 // Returns the extension ID, or empty if none. This includes web URLs that | 69 // Returns the extension ID, or empty if none. This includes web URLs that |
| 95 // are part of an extension's web extent. | 70 // are part of an extension's web extent. |
| 96 std::string GetIDByURL(const ExtensionURLInfo& info) const; | 71 std::string GetIdByURL(const ExtensionURLInfo& info) const; |
| 97 | 72 |
| 98 // Returns the Extension, or NULL if none. This includes web URLs that are | 73 // Returns the Extension, or NULL if none. This includes web URLs that are |
| 99 // part of an extension's web extent. | 74 // part of an extension's web extent. |
| 100 // NOTE: This can return NULL if called before UpdateExtensions receives | 75 // NOTE: This can return NULL if called before UpdateExtensions receives |
| 101 // bulk extension data (e.g. if called from | 76 // bulk extension data (e.g. if called from |
| 102 // EventBindings::HandleContextCreated) | 77 // EventBindings::HandleContextCreated) |
| 103 const Extension* GetByURL(const ExtensionURLInfo& info) const; | 78 const Extension* GetByURL(const ExtensionURLInfo& info) const; |
| 104 | 79 |
| 105 // Returns true if |new_url| is in the extent of the same extension as | 80 // Returns true if |new_url| is in the extent of the same extension as |
| 106 // |old_url|. Also returns true if neither URL is in an app. | 81 // |old_url|. Also returns true if neither URL is in an app. |
| 107 bool InSameExtent(const GURL& old_url, const GURL& new_url) const; | 82 bool InSameExtent(const GURL& old_url, const GURL& new_url) const; |
| 108 | 83 |
| 109 // Look up an Extension object by id. | 84 // Look up an Extension object by id. |
| 110 const Extension* GetByID(const std::string& id) const; | 85 const Extension* GetByID(const std::string& id) const; |
| 111 | 86 |
| 112 // Returns true if |info| should get extension api bindings and be permitted | 87 // Returns true if |info| should get extension api bindings and be permitted |
| 113 // to make api calls. Note that this is independent of what extension | 88 // to make api calls. Note that this is independent of what extension |
| 114 // permissions the given extension has been granted. | 89 // permissions the given extension has been granted. |
| 115 bool ExtensionBindingsAllowed(const ExtensionURLInfo& info) const; | 90 bool ExtensionBindingsAllowed(const ExtensionURLInfo& info) const; |
| 116 | 91 |
| 117 private: | 92 private: |
| 118 FRIEND_TEST_ALL_PREFIXES(ExtensionSetTest, ExtensionSet); | 93 FRIEND_TEST_ALL_PREFIXES(ExtensionSetTest, ExtensionSet); |
| 119 | 94 |
| 120 ExtensionMap extensions_; | 95 ExtensionMap extensions_; |
| 121 | 96 |
| 122 DISALLOW_COPY_AND_ASSIGN(ExtensionSet); | 97 DISALLOW_COPY_AND_ASSIGN(ExtensionSet); |
| 123 }; | 98 }; |
| 124 | 99 |
| 125 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_ | 100 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_ |
| OLD | NEW |