[email protected] | 7f4308d | 2012-01-18 07:43:01 | [diff] [blame] | 1 | // Copyright (c) 2012 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] | ec7db28 | 2011-01-29 01:11:36 | [diff] [blame] | 5 | #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_ |
| 6 | #define CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_ |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 7 | |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 8 | #include <iterator> |
[email protected] | 6757060 | 2011-08-23 21:50:54 | [diff] [blame] | 9 | #include <map> |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 10 | #include <string> |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 11 | |
| 12 | #include "base/gtest_prod_util.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
[email protected] | 583d45c1 | 2010-08-31 02:48:12 | [diff] [blame] | 14 | #include "chrome/common/extensions/extension.h" |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 15 | #include "googleurl/src/gurl.h" |
[email protected] | 69729ca | 2011-12-02 08:01:25 | [diff] [blame] | 16 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
| 17 | |
| 18 | class ExtensionURLInfo { |
| 19 | public: |
| 20 | // The extension system uses both a document's origin and its URL to |
| 21 | // grant permissions. Ideally, we would use only the origin, but because |
| 22 | // the web extent of a hosted app can be less than an entire origin, we |
| 23 | // take the URL into account as well |
| 24 | ExtensionURLInfo(WebKit::WebSecurityOrigin origin, const GURL& url); |
| 25 | |
| 26 | // WARNING! Using this constructor can miss important security checks if |
| 27 | // you're trying to find a running extension. For example, if the |
| 28 | // URL in question is being rendered inside an iframe sandbox, then |
| 29 | // we might incorrectly grant it access to powerful extension APIs. |
| 30 | explicit ExtensionURLInfo(const GURL& url); |
| 31 | |
| 32 | const WebKit::WebSecurityOrigin& origin() const { return origin_; } |
| 33 | const GURL& url() const { return url_; } |
| 34 | |
| 35 | private: |
| 36 | WebKit::WebSecurityOrigin origin_; |
| 37 | GURL url_; |
| 38 | }; |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 39 | |
[email protected] | ec7db28 | 2011-01-29 01:11:36 | [diff] [blame] | 40 | // The one true extension container. Extensions are identified by their id. |
| 41 | // Only one extension can be in the set with a given ID. |
| 42 | class ExtensionSet { |
[email protected] | dbb2416 | 2012-06-06 01:41:22 | [diff] [blame] | 43 | public: |
[email protected] | 6757060 | 2011-08-23 21:50:54 | [diff] [blame] | 44 | typedef std::pair<FilePath, std::string> ExtensionPathAndDefaultLocale; |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 45 | typedef std::map<std::string, scoped_refptr<const extensions::Extension> > |
| 46 | ExtensionMap; |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 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, |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 52 | scoped_refptr<const extensions::Extension> > { |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 53 | public: |
[email protected] | 23827ec | 2012-08-10 22:08:08 | [diff] [blame^] | 54 | const_iterator(); |
| 55 | const_iterator(const const_iterator& other); |
| 56 | explicit const_iterator(ExtensionMap::const_iterator it); |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 57 | const_iterator& operator++() { |
| 58 | ++it_; |
| 59 | return *this; |
| 60 | } |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 61 | const scoped_refptr<const extensions::Extension> operator*() { |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 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 | }; |
[email protected] | 6757060 | 2011-08-23 21:50:54 | [diff] [blame] | 70 | |
[email protected] | ec7db28 | 2011-01-29 01:11:36 | [diff] [blame] | 71 | ExtensionSet(); |
| 72 | ~ExtensionSet(); |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 73 | |
[email protected] | 2a521c5 | 2011-01-26 18:45:21 | [diff] [blame] | 74 | size_t size() const; |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 75 | bool is_empty() const; |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 76 | |
[email protected] | 7b6eba8 | 2011-10-16 03:08:11 | [diff] [blame] | 77 | // Iteration support. |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 78 | const_iterator begin() const { return const_iterator(extensions_.begin()); } |
| 79 | const_iterator end() const { return const_iterator(extensions_.end()); } |
[email protected] | 7b6eba8 | 2011-10-16 03:08:11 | [diff] [blame] | 80 | |
[email protected] | ec7db28 | 2011-01-29 01:11:36 | [diff] [blame] | 81 | // Returns true if the set contains the specified extension. |
[email protected] | 7b6eba8 | 2011-10-16 03:08:11 | [diff] [blame] | 82 | bool Contains(const std::string& id) const; |
[email protected] | ec7db28 | 2011-01-29 01:11:36 | [diff] [blame] | 83 | |
| 84 | // Adds the specified extension to the set. The set becomes an owner. Any |
| 85 | // previous extension with the same ID is removed. |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 86 | void Insert(const scoped_refptr<const extensions::Extension>& extension); |
[email protected] | 2a521c5 | 2011-01-26 18:45:21 | [diff] [blame] | 87 | |
[email protected] | 7f4308d | 2012-01-18 07:43:01 | [diff] [blame] | 88 | // Copies different items from |extensions| to the current set and returns |
| 89 | // whether anything changed. |
| 90 | bool InsertAll(const ExtensionSet& extensions); |
| 91 | |
[email protected] | 2a521c5 | 2011-01-26 18:45:21 | [diff] [blame] | 92 | // Removes the specified extension. |
| 93 | void Remove(const std::string& id); |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 94 | |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 95 | // Removes all extensions. |
| 96 | void Clear(); |
| 97 | |
[email protected] | 69729ca | 2011-12-02 08:01:25 | [diff] [blame] | 98 | // Returns the extension ID, or empty if none. This includes web URLs that |
| 99 | // are part of an extension's web extent. |
[email protected] | 615d88f | 2011-12-13 01:47:44 | [diff] [blame] | 100 | std::string GetExtensionOrAppIDByURL(const ExtensionURLInfo& info) const; |
[email protected] | 583d45c1 | 2010-08-31 02:48:12 | [diff] [blame] | 101 | |
[email protected] | 69729ca | 2011-12-02 08:01:25 | [diff] [blame] | 102 | // Returns the Extension, or NULL if none. This includes web URLs that are |
| 103 | // part of an extension's web extent. |
[email protected] | 583d45c1 | 2010-08-31 02:48:12 | [diff] [blame] | 104 | // NOTE: This can return NULL if called before UpdateExtensions receives |
| 105 | // bulk extension data (e.g. if called from |
| 106 | // EventBindings::HandleContextCreated) |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 107 | const extensions::Extension* GetExtensionOrAppByURL( |
| 108 | const ExtensionURLInfo& info) const; |
[email protected] | 615d88f | 2011-12-13 01:47:44 | [diff] [blame] | 109 | |
| 110 | // Returns the hosted app whose web extent contains the URL. |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 111 | const extensions::Extension* GetHostedAppByURL( |
| 112 | const ExtensionURLInfo& info) const; |
[email protected] | 615d88f | 2011-12-13 01:47:44 | [diff] [blame] | 113 | |
| 114 | // Returns a hosted app that contains any URL that overlaps with the given |
| 115 | // extent, if one exists. |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 116 | const extensions::Extension* GetHostedAppByOverlappingWebExtent( |
[email protected] | 615d88f | 2011-12-13 01:47:44 | [diff] [blame] | 117 | const URLPatternSet& extent) const; |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 118 | |
| 119 | // Returns true if |new_url| is in the extent of the same extension as |
| 120 | // |old_url|. Also returns true if neither URL is in an app. |
[email protected] | 2a521c5 | 2011-01-26 18:45:21 | [diff] [blame] | 121 | bool InSameExtent(const GURL& old_url, const GURL& new_url) const; |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 122 | |
[email protected] | 2a521c5 | 2011-01-26 18:45:21 | [diff] [blame] | 123 | // Look up an Extension object by id. |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 124 | const extensions::Extension* GetByID(const std::string& id) const; |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 125 | |
[email protected] | 69729ca | 2011-12-02 08:01:25 | [diff] [blame] | 126 | // Returns true if |info| should get extension api bindings and be permitted |
[email protected] | 583d45c1 | 2010-08-31 02:48:12 | [diff] [blame] | 127 | // to make api calls. Note that this is independent of what extension |
| 128 | // permissions the given extension has been granted. |
[email protected] | 69729ca | 2011-12-02 08:01:25 | [diff] [blame] | 129 | bool ExtensionBindingsAllowed(const ExtensionURLInfo& info) const; |
[email protected] | 583d45c1 | 2010-08-31 02:48:12 | [diff] [blame] | 130 | |
[email protected] | dbb2416 | 2012-06-06 01:41:22 | [diff] [blame] | 131 | // Returns true if |info| is an extension page that is to be served in a |
| 132 | // unique sandboxed origin. |
| 133 | bool IsSandboxedPage(const ExtensionURLInfo& info) const; |
| 134 | |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 135 | private: |
[email protected] | ec7db28 | 2011-01-29 01:11:36 | [diff] [blame] | 136 | FRIEND_TEST_ALL_PREFIXES(ExtensionSetTest, ExtensionSet); |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 137 | |
[email protected] | 2a521c5 | 2011-01-26 18:45:21 | [diff] [blame] | 138 | ExtensionMap extensions_; |
| 139 | |
[email protected] | ec7db28 | 2011-01-29 01:11:36 | [diff] [blame] | 140 | DISALLOW_COPY_AND_ASSIGN(ExtensionSet); |
[email protected] | 5351dbc | 2010-08-27 15:22:11 | [diff] [blame] | 141 | }; |
| 142 | |
[email protected] | ec7db28 | 2011-01-29 01:11:36 | [diff] [blame] | 143 | #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_ |