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 #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 typedef ExtensionMap::const_iterator const_iterator; | 47 |
| 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 }; |
48 | 70 |
49 ExtensionSet(); | 71 ExtensionSet(); |
50 ~ExtensionSet(); | 72 ~ExtensionSet(); |
51 | 73 |
52 // Gets the number of extensions contained. | |
53 size_t size() const; | 74 size_t size() const; |
| 75 bool is_empty() const; |
54 | 76 |
55 // Iteration support. | 77 // Iteration support. |
56 const_iterator begin() const { return extensions_.begin(); } | 78 const_iterator begin() const { return const_iterator(extensions_.begin()); } |
57 const_iterator end() const { return extensions_.end(); } | 79 const_iterator end() const { return const_iterator(extensions_.end()); } |
58 | 80 |
59 // Returns true if the set contains the specified extension. | 81 // Returns true if the set contains the specified extension. |
60 bool Contains(const std::string& id) const; | 82 bool Contains(const std::string& id) const; |
61 | 83 |
62 // Adds the specified extension to the set. The set becomes an owner. Any | 84 // Adds the specified extension to the set. The set becomes an owner. Any |
63 // previous extension with the same ID is removed. | 85 // previous extension with the same ID is removed. |
64 void Insert(const scoped_refptr<const Extension>& extension); | 86 void Insert(const scoped_refptr<const Extension>& extension); |
65 | 87 |
66 // Removes the specified extension. | 88 // Removes the specified extension. |
67 void Remove(const std::string& id); | 89 void Remove(const std::string& id); |
68 | 90 |
| 91 // Removes all extensions. |
| 92 void Clear(); |
| 93 |
69 // Returns the extension ID, or empty if none. This includes web URLs that | 94 // Returns the extension ID, or empty if none. This includes web URLs that |
70 // are part of an extension's web extent. | 95 // are part of an extension's web extent. |
71 std::string GetIdByURL(const ExtensionURLInfo& info) const; | 96 std::string GetIDByURL(const ExtensionURLInfo& info) const; |
72 | 97 |
73 // Returns the Extension, or NULL if none. This includes web URLs that are | 98 // Returns the Extension, or NULL if none. This includes web URLs that are |
74 // part of an extension's web extent. | 99 // part of an extension's web extent. |
75 // NOTE: This can return NULL if called before UpdateExtensions receives | 100 // NOTE: This can return NULL if called before UpdateExtensions receives |
76 // bulk extension data (e.g. if called from | 101 // bulk extension data (e.g. if called from |
77 // EventBindings::HandleContextCreated) | 102 // EventBindings::HandleContextCreated) |
78 const Extension* GetByURL(const ExtensionURLInfo& info) const; | 103 const Extension* GetByURL(const ExtensionURLInfo& info) const; |
79 | 104 |
80 // Returns true if |new_url| is in the extent of the same extension as | 105 // Returns true if |new_url| is in the extent of the same extension as |
81 // |old_url|. Also returns true if neither URL is in an app. | 106 // |old_url|. Also returns true if neither URL is in an app. |
82 bool InSameExtent(const GURL& old_url, const GURL& new_url) const; | 107 bool InSameExtent(const GURL& old_url, const GURL& new_url) const; |
83 | 108 |
84 // Look up an Extension object by id. | 109 // Look up an Extension object by id. |
85 const Extension* GetByID(const std::string& id) const; | 110 const Extension* GetByID(const std::string& id) const; |
86 | 111 |
87 // Returns true if |info| should get extension api bindings and be permitted | 112 // Returns true if |info| should get extension api bindings and be permitted |
88 // to make api calls. Note that this is independent of what extension | 113 // to make api calls. Note that this is independent of what extension |
89 // permissions the given extension has been granted. | 114 // permissions the given extension has been granted. |
90 bool ExtensionBindingsAllowed(const ExtensionURLInfo& info) const; | 115 bool ExtensionBindingsAllowed(const ExtensionURLInfo& info) const; |
91 | 116 |
92 private: | 117 private: |
93 FRIEND_TEST_ALL_PREFIXES(ExtensionSetTest, ExtensionSet); | 118 FRIEND_TEST_ALL_PREFIXES(ExtensionSetTest, ExtensionSet); |
94 | 119 |
95 ExtensionMap extensions_; | 120 ExtensionMap extensions_; |
96 | 121 |
97 DISALLOW_COPY_AND_ASSIGN(ExtensionSet); | 122 DISALLOW_COPY_AND_ASSIGN(ExtensionSet); |
98 }; | 123 }; |
99 | 124 |
100 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_ | 125 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_ |
OLD | NEW |