OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 EXTENSIONS_COMMON_EXTENSION_SET_H_ |
6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_ | 6 #define EXTENSIONS_COMMON_EXTENSION_SET_H_ |
7 | 7 |
8 #include <iterator> | 8 #include <iterator> |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
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 "extensions/common/extension.h" | 15 #include "extensions/common/extension.h" |
16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
17 | 17 |
| 18 namespace extensions { |
| 19 |
18 // The one true extension container. Extensions are identified by their id. | 20 // The one true extension container. Extensions are identified by their id. |
19 // Only one extension can be in the set with a given ID. | 21 // Only one extension can be in the set with a given ID. |
20 class ExtensionSet { | 22 class ExtensionSet { |
21 public: | 23 public: |
22 typedef std::pair<base::FilePath, std::string> ExtensionPathAndDefaultLocale; | 24 typedef std::pair<base::FilePath, std::string> ExtensionPathAndDefaultLocale; |
23 typedef std::map<std::string, scoped_refptr<const extensions::Extension> > | 25 typedef std::map<std::string, scoped_refptr<const Extension> > ExtensionMap; |
24 ExtensionMap; | 26 typedef base::Callback<void(const ExtensionIdSet&)> |
25 typedef base::Callback<void(const extensions::ExtensionIdSet&)> | |
26 ModificationCallback; | 27 ModificationCallback; |
27 | 28 |
28 // Iteration over the values of the map (given that it's an ExtensionSet, | 29 // Iteration over the values of the map (given that it's an ExtensionSet, |
29 // it should iterate like a set iterator). | 30 // it should iterate like a set iterator). |
30 class const_iterator : | 31 class const_iterator : public std::iterator<std::input_iterator_tag, |
31 public std::iterator<std::input_iterator_tag, | 32 scoped_refptr<const Extension> > { |
32 scoped_refptr<const extensions::Extension> > { | |
33 public: | 33 public: |
34 const_iterator(); | 34 const_iterator(); |
35 const_iterator(const const_iterator& other); | 35 const_iterator(const const_iterator& other); |
36 explicit const_iterator(ExtensionMap::const_iterator it); | 36 explicit const_iterator(ExtensionMap::const_iterator it); |
37 const_iterator& operator++() { | 37 const_iterator& operator++() { |
38 ++it_; | 38 ++it_; |
39 return *this; | 39 return *this; |
40 } | 40 } |
41 const_iterator operator++(int) { | 41 const_iterator operator++(int) { |
42 const const_iterator old(*this); | 42 const const_iterator old(*this); |
43 ++it_; | 43 ++it_; |
44 return old; | 44 return old; |
45 } | 45 } |
46 const scoped_refptr<const extensions::Extension>& operator*() const { | 46 const scoped_refptr<const Extension>& operator*() const { |
47 return it_->second; | 47 return it_->second; |
48 } | 48 } |
49 const scoped_refptr<const extensions::Extension>* operator->() const { | 49 const scoped_refptr<const Extension>* operator->() const { |
50 return &it_->second; | 50 return &it_->second; |
51 } | 51 } |
52 bool operator!=(const const_iterator& other) const { | 52 bool operator!=(const const_iterator& other) const { |
53 return it_ != other.it_; | 53 return it_ != other.it_; |
54 } | 54 } |
55 bool operator==(const const_iterator& other) const { | 55 bool operator==(const const_iterator& other) const { |
56 return it_ == other.it_; | 56 return it_ == other.it_; |
57 } | 57 } |
58 | 58 |
59 private: | 59 private: |
60 ExtensionMap::const_iterator it_; | 60 ExtensionMap::const_iterator it_; |
61 }; | 61 }; |
62 | 62 |
63 ExtensionSet(); | 63 ExtensionSet(); |
64 ~ExtensionSet(); | 64 ~ExtensionSet(); |
65 | 65 |
66 size_t size() const; | 66 size_t size() const; |
67 bool is_empty() const; | 67 bool is_empty() const; |
68 | 68 |
69 // Iteration support. | 69 // Iteration support. |
70 const_iterator begin() const { return const_iterator(extensions_.begin()); } | 70 const_iterator begin() const { return const_iterator(extensions_.begin()); } |
71 const_iterator end() const { return const_iterator(extensions_.end()); } | 71 const_iterator end() const { return const_iterator(extensions_.end()); } |
72 | 72 |
73 // Returns true if the set contains the specified extension. | 73 // Returns true if the set contains the specified extension. |
74 bool Contains(const std::string& id) const; | 74 bool Contains(const std::string& id) const; |
75 | 75 |
76 // Adds the specified extension to the set. The set becomes an owner. Any | 76 // Adds the specified extension to the set. The set becomes an owner. Any |
77 // previous extension with the same ID is removed. | 77 // previous extension with the same ID is removed. |
78 // Returns true if there is no previous extension. | 78 // Returns true if there is no previous extension. |
79 bool Insert(const scoped_refptr<const extensions::Extension>& extension); | 79 bool Insert(const scoped_refptr<const Extension>& extension); |
80 | 80 |
81 // Copies different items from |extensions| to the current set and returns | 81 // Copies different items from |extensions| to the current set and returns |
82 // whether anything changed. | 82 // whether anything changed. |
83 bool InsertAll(const ExtensionSet& extensions); | 83 bool InsertAll(const ExtensionSet& extensions); |
84 | 84 |
85 // Removes the specified extension. | 85 // Removes the specified extension. |
86 // Returns true if the set contained the specified extnesion. | 86 // Returns true if the set contained the specified extnesion. |
87 bool Remove(const std::string& id); | 87 bool Remove(const std::string& id); |
88 | 88 |
89 // Removes all extensions. | 89 // Removes all extensions. |
90 void Clear(); | 90 void Clear(); |
91 | 91 |
92 // Returns the extension ID, or empty if none. This includes web URLs that | 92 // Returns the extension ID, or empty if none. This includes web URLs that |
93 // are part of an extension's web extent. | 93 // are part of an extension's web extent. |
94 std::string GetExtensionOrAppIDByURL(const GURL& url) const; | 94 std::string GetExtensionOrAppIDByURL(const GURL& url) const; |
95 | 95 |
96 // Returns the Extension, or NULL if none. This includes web URLs that are | 96 // Returns the Extension, or NULL if none. This includes web URLs that are |
97 // part of an extension's web extent. | 97 // part of an extension's web extent. |
98 // NOTE: This can return NULL if called before UpdateExtensions receives | 98 // NOTE: This can return NULL if called before UpdateExtensions receives |
99 // bulk extension data (e.g. if called from | 99 // bulk extension data (e.g. if called from |
100 // EventBindings::HandleContextCreated) | 100 // EventBindings::HandleContextCreated) |
101 const extensions::Extension* GetExtensionOrAppByURL(const GURL& url) const; | 101 const Extension* GetExtensionOrAppByURL(const GURL& url) const; |
102 | 102 |
103 // Returns the hosted app whose web extent contains the URL. | 103 // Returns the hosted app whose web extent contains the URL. |
104 const extensions::Extension* GetHostedAppByURL(const GURL& url) const; | 104 const Extension* GetHostedAppByURL(const GURL& url) const; |
105 | 105 |
106 // Returns a hosted app that contains any URL that overlaps with the given | 106 // Returns a hosted app that contains any URL that overlaps with the given |
107 // extent, if one exists. | 107 // extent, if one exists. |
108 const extensions::Extension* GetHostedAppByOverlappingWebExtent( | 108 const Extension* GetHostedAppByOverlappingWebExtent( |
109 const extensions::URLPatternSet& extent) const; | 109 const URLPatternSet& extent) const; |
110 | 110 |
111 // Returns true if |new_url| is in the extent of the same extension as | 111 // Returns true if |new_url| is in the extent of the same extension as |
112 // |old_url|. Also returns true if neither URL is in an app. | 112 // |old_url|. Also returns true if neither URL is in an app. |
113 bool InSameExtent(const GURL& old_url, const GURL& new_url) const; | 113 bool InSameExtent(const GURL& old_url, const GURL& new_url) const; |
114 | 114 |
115 // Look up an Extension object by id. | 115 // Look up an Extension object by id. |
116 const extensions::Extension* GetByID(const std::string& id) const; | 116 const Extension* GetByID(const std::string& id) const; |
117 | 117 |
118 // Gets the IDs of all extensions in the set. | 118 // Gets the IDs of all extensions in the set. |
119 extensions::ExtensionIdSet GetIDs() const; | 119 ExtensionIdSet GetIDs() const; |
120 | 120 |
121 // Returns true if |info| should get extension api bindings and be permitted | 121 // Returns true if |info| should get extension api bindings and be permitted |
122 // to make api calls. Note that this is independent of what extension | 122 // to make api calls. Note that this is independent of what extension |
123 // permissions the given extension has been granted. | 123 // permissions the given extension has been granted. |
124 bool ExtensionBindingsAllowed(const GURL& url) const; | 124 bool ExtensionBindingsAllowed(const GURL& url) const; |
125 | 125 |
126 void set_modification_callback( | 126 void set_modification_callback( |
127 const ModificationCallback& modification_callback) { | 127 const ModificationCallback& modification_callback) { |
128 modification_callback_ = modification_callback; | 128 modification_callback_ = modification_callback; |
129 } | 129 } |
130 | 130 |
131 private: | 131 private: |
132 FRIEND_TEST_ALL_PREFIXES(ExtensionSetTest, ExtensionSet); | 132 FRIEND_TEST_ALL_PREFIXES(ExtensionSetTest, ExtensionSet); |
133 | 133 |
134 ExtensionMap extensions_; | 134 ExtensionMap extensions_; |
135 | 135 |
136 // If non-null, called with the extension ids in this set after a modification | 136 // If non-null, called with the extension ids in this set after a modification |
137 // occurred. This is not called on Clear() which is typically used when | 137 // occurred. This is not called on Clear() which is typically used when |
138 // discarding the set (e.g., on shutdown) and we do not want to track that as | 138 // discarding the set (e.g., on shutdown) and we do not want to track that as |
139 // a real modification. | 139 // a real modification. |
140 ModificationCallback modification_callback_; | 140 ModificationCallback modification_callback_; |
141 | 141 |
142 DISALLOW_COPY_AND_ASSIGN(ExtensionSet); | 142 DISALLOW_COPY_AND_ASSIGN(ExtensionSet); |
143 }; | 143 }; |
144 | 144 |
145 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_ | 145 } // namespace extensions |
| 146 |
| 147 #endif // EXTENSIONS_COMMON_EXTENSION_SET_H_ |
OLD | NEW |