[email protected] | 289c44b | 2013-12-17 03:26:57 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 289c44b | 2013-12-17 03:26:57 | [diff] [blame] | 5 | #ifndef EXTENSIONS_COMMON_EXTENSION_SET_H_ |
| 6 | #define EXTENSIONS_COMMON_EXTENSION_SET_H_ |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 7 | |
avi | 2d124c0 | 2015-12-23 06:36:42 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 10 | #include <iterator> |
[email protected] | 6757060 | 2011-08-23 21:50:54 | [diff] [blame] | 11 | #include <map> |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 12 | #include <string> |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 13 | |
[email protected] | 076ebeda | 2014-06-06 21:47:26 | [diff] [blame] | 14 | #include "base/callback.h" |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 15 | #include "base/gtest_prod_util.h" |
avi | 2d124c0 | 2015-12-23 06:36:42 | [diff] [blame] | 16 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 17 | #include "base/memory/ref_counted.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 18 | #include "extensions/common/extension.h" |
[email protected] | 5f5ef80 | 2013-07-04 16:11:13 | [diff] [blame] | 19 | #include "url/gurl.h" |
[email protected] | 69729ca | 2011-12-02 08:01:25 | [diff] [blame] | 20 | |
[email protected] | 289c44b | 2013-12-17 03:26:57 | [diff] [blame] | 21 | namespace extensions { |
| 22 | |
[email protected] | ec7db28 | 2011-01-29 01:11:36 | [diff] [blame] | 23 | // The one true extension container. Extensions are identified by their id. |
| 24 | // Only one extension can be in the set with a given ID. |
| 25 | class ExtensionSet { |
[email protected] | dbb2416 | 2012-06-06 01:41:22 | [diff] [blame] | 26 | public: |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 27 | typedef std::pair<base::FilePath, std::string> ExtensionPathAndDefaultLocale; |
[email protected] | 289c44b | 2013-12-17 03:26:57 | [diff] [blame] | 28 | typedef std::map<std::string, scoped_refptr<const Extension> > ExtensionMap; |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 29 | |
| 30 | // Iteration over the values of the map (given that it's an ExtensionSet, |
| 31 | // it should iterate like a set iterator). |
[email protected] | 289c44b | 2013-12-17 03:26:57 | [diff] [blame] | 32 | class const_iterator : public std::iterator<std::input_iterator_tag, |
| 33 | scoped_refptr<const Extension> > { |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 34 | public: |
[email protected] | 23827ec | 2012-08-10 22:08:08 | [diff] [blame] | 35 | const_iterator(); |
| 36 | const_iterator(const const_iterator& other); |
| 37 | explicit const_iterator(ExtensionMap::const_iterator it); |
[email protected] | a8d94b4 | 2013-12-10 18:52:22 | [diff] [blame] | 38 | ~const_iterator(); |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 39 | const_iterator& operator++() { |
| 40 | ++it_; |
| 41 | return *this; |
| 42 | } |
[email protected] | d478a1d | 2013-10-10 22:09:50 | [diff] [blame] | 43 | const_iterator operator++(int) { |
| 44 | const const_iterator old(*this); |
| 45 | ++it_; |
| 46 | return old; |
| 47 | } |
[email protected] | 289c44b | 2013-12-17 03:26:57 | [diff] [blame] | 48 | const scoped_refptr<const Extension>& operator*() const { |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 49 | return it_->second; |
| 50 | } |
[email protected] | 289c44b | 2013-12-17 03:26:57 | [diff] [blame] | 51 | const scoped_refptr<const Extension>* operator->() const { |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 52 | return &it_->second; |
| 53 | } |
[email protected] | d478a1d | 2013-10-10 22:09:50 | [diff] [blame] | 54 | bool operator!=(const const_iterator& other) const { |
| 55 | return it_ != other.it_; |
| 56 | } |
| 57 | bool operator==(const const_iterator& other) const { |
| 58 | return it_ == other.it_; |
| 59 | } |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 60 | |
| 61 | private: |
| 62 | ExtensionMap::const_iterator it_; |
| 63 | }; |
[email protected] | 6757060 | 2011-08-23 21:50:54 | [diff] [blame] | 64 | |
[email protected] | ec7db28 | 2011-01-29 01:11:36 | [diff] [blame] | 65 | ExtensionSet(); |
| 66 | ~ExtensionSet(); |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 67 | |
[email protected] | 2a521c5 | 2011-01-26 18:45:21 | [diff] [blame] | 68 | size_t size() const; |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 69 | bool is_empty() const; |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 70 | |
[email protected] | 7b6eba8 | 2011-10-16 03:08:11 | [diff] [blame] | 71 | // Iteration support. |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 72 | const_iterator begin() const { return const_iterator(extensions_.begin()); } |
| 73 | const_iterator end() const { return const_iterator(extensions_.end()); } |
[email protected] | 7b6eba8 | 2011-10-16 03:08:11 | [diff] [blame] | 74 | |
[email protected] | ec7db28 | 2011-01-29 01:11:36 | [diff] [blame] | 75 | // Returns true if the set contains the specified extension. |
[email protected] | 7b6eba8 | 2011-10-16 03:08:11 | [diff] [blame] | 76 | bool Contains(const std::string& id) const; |
[email protected] | ec7db28 | 2011-01-29 01:11:36 | [diff] [blame] | 77 | |
| 78 | // Adds the specified extension to the set. The set becomes an owner. Any |
| 79 | // previous extension with the same ID is removed. |
[email protected] | ecda2cf | 2013-04-22 09:57:09 | [diff] [blame] | 80 | // Returns true if there is no previous extension. |
[email protected] | 289c44b | 2013-12-17 03:26:57 | [diff] [blame] | 81 | bool Insert(const scoped_refptr<const Extension>& extension); |
[email protected] | 2a521c5 | 2011-01-26 18:45:21 | [diff] [blame] | 82 | |
[email protected] | 7f4308d | 2012-01-18 07:43:01 | [diff] [blame] | 83 | // Copies different items from |extensions| to the current set and returns |
| 84 | // whether anything changed. |
| 85 | bool InsertAll(const ExtensionSet& extensions); |
| 86 | |
[email protected] | 2a521c5 | 2011-01-26 18:45:21 | [diff] [blame] | 87 | // Removes the specified extension. |
[email protected] | 695b571 | 2012-12-06 23:55:28 | [diff] [blame] | 88 | // Returns true if the set contained the specified extnesion. |
| 89 | bool Remove(const std::string& id); |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 90 | |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 91 | // Removes all extensions. |
| 92 | void Clear(); |
| 93 | |
[email protected] | 69729ca | 2011-12-02 08:01:25 | [diff] [blame] | 94 | // Returns the extension ID, or empty if none. This includes web URLs that |
| 95 | // are part of an extension's web extent. |
[email protected] | be9915fb | 2013-07-18 09:28:55 | [diff] [blame] | 96 | std::string GetExtensionOrAppIDByURL(const GURL& url) const; |
[email protected] | 583d45c1 | 2010-08-31 02:48:12 | [diff] [blame] | 97 | |
[email protected] | 69729ca | 2011-12-02 08:01:25 | [diff] [blame] | 98 | // Returns the Extension, or NULL if none. This includes web URLs that are |
| 99 | // part of an extension's web extent. |
[email protected] | 583d45c1 | 2010-08-31 02:48:12 | [diff] [blame] | 100 | // NOTE: This can return NULL if called before UpdateExtensions receives |
| 101 | // bulk extension data (e.g. if called from |
| 102 | // EventBindings::HandleContextCreated) |
[email protected] | 289c44b | 2013-12-17 03:26:57 | [diff] [blame] | 103 | const Extension* GetExtensionOrAppByURL(const GURL& url) const; |
[email protected] | 615d88f | 2011-12-13 01:47:44 | [diff] [blame] | 104 | |
[email protected] | d685c565b | 2014-04-10 21:00:37 | [diff] [blame] | 105 | // Returns the app specified by the given |url|, if one exists. This will |
| 106 | // return NULL if there is no entry with |url|, or if the extension with |
| 107 | // |url| is not an app. |
| 108 | const Extension* GetAppByURL(const GURL& url) const; |
| 109 | |
[email protected] | 615d88f | 2011-12-13 01:47:44 | [diff] [blame] | 110 | // Returns the hosted app whose web extent contains the URL. |
[email protected] | 289c44b | 2013-12-17 03:26:57 | [diff] [blame] | 111 | const Extension* GetHostedAppByURL(const GURL& url) const; |
[email protected] | 615d88f | 2011-12-13 01:47:44 | [diff] [blame] | 112 | |
| 113 | // Returns a hosted app that contains any URL that overlaps with the given |
| 114 | // extent, if one exists. |
[email protected] | 289c44b | 2013-12-17 03:26:57 | [diff] [blame] | 115 | const Extension* GetHostedAppByOverlappingWebExtent( |
| 116 | const URLPatternSet& extent) const; |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 117 | |
| 118 | // Returns true if |new_url| is in the extent of the same extension as |
| 119 | // |old_url|. Also returns true if neither URL is in an app. |
[email protected] | 2a521c5 | 2011-01-26 18:45:21 | [diff] [blame] | 120 | bool InSameExtent(const GURL& old_url, const GURL& new_url) const; |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 121 | |
[email protected] | 2a521c5 | 2011-01-26 18:45:21 | [diff] [blame] | 122 | // Look up an Extension object by id. |
[email protected] | 289c44b | 2013-12-17 03:26:57 | [diff] [blame] | 123 | const Extension* GetByID(const std::string& id) const; |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 124 | |
[email protected] | 695b571 | 2012-12-06 23:55:28 | [diff] [blame] | 125 | // Gets the IDs of all extensions in the set. |
[email protected] | 289c44b | 2013-12-17 03:26:57 | [diff] [blame] | 126 | ExtensionIdSet GetIDs() const; |
[email protected] | 695b571 | 2012-12-06 23:55:28 | [diff] [blame] | 127 | |
[email protected] | 69729ca | 2011-12-02 08:01:25 | [diff] [blame] | 128 | // Returns true if |info| should get extension api bindings and be permitted |
[email protected] | 583d45c1 | 2010-08-31 02:48:12 | [diff] [blame] | 129 | // to make api calls. Note that this is independent of what extension |
| 130 | // permissions the given extension has been granted. |
[email protected] | be9915fb | 2013-07-18 09:28:55 | [diff] [blame] | 131 | bool ExtensionBindingsAllowed(const GURL& url) const; |
[email protected] | dbb2416 | 2012-06-06 01:41:22 | [diff] [blame] | 132 | |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 133 | private: |
[email protected] | 2a521c5 | 2011-01-26 18:45:21 | [diff] [blame] | 134 | ExtensionMap extensions_; |
| 135 | |
[email protected] | ec7db28 | 2011-01-29 01:11:36 | [diff] [blame] | 136 | DISALLOW_COPY_AND_ASSIGN(ExtensionSet); |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 137 | }; |
| 138 | |
[email protected] | 289c44b | 2013-12-17 03:26:57 | [diff] [blame] | 139 | } // namespace extensions |
| 140 | |
| 141 | #endif // EXTENSIONS_COMMON_EXTENSION_SET_H_ |