blob: 43635ba1654b4fc992c047acec02be16ec462827 [file] [log] [blame]
[email protected]7f4308d2012-01-18 07:43:011// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]5351dbc2010-08-27 15:22:112// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]ec7db282011-01-29 01:11:365#ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_
6#define CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_
[email protected]5351dbc2010-08-27 15:22:117
[email protected]84df8332011-12-06 18:22:468#include <iterator>
[email protected]67570602011-08-23 21:50:549#include <map>
[email protected]5351dbc2010-08-27 15:22:1110#include <string>
[email protected]5351dbc2010-08-27 15:22:1111
12#include "base/gtest_prod_util.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/ref_counted.h"
[email protected]583d45c12010-08-31 02:48:1214#include "chrome/common/extensions/extension.h"
[email protected]5351dbc2010-08-27 15:22:1115#include "googleurl/src/gurl.h"
[email protected]69729ca2011-12-02 08:01:2516#include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
17
18class 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]5351dbc2010-08-27 15:22:1139
[email protected]ec7db282011-01-29 01:11:3640// The one true extension container. Extensions are identified by their id.
41// Only one extension can be in the set with a given ID.
42class ExtensionSet {
[email protected]dbb24162012-06-06 01:41:2243 public:
[email protected]67570602011-08-23 21:50:5444 typedef std::pair<FilePath, std::string> ExtensionPathAndDefaultLocale;
[email protected]1c321ee52012-05-21 03:02:3445 typedef std::map<std::string, scoped_refptr<const extensions::Extension> >
46 ExtensionMap;
[email protected]84df8332011-12-06 18:22:4647
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]1c321ee52012-05-21 03:02:3452 scoped_refptr<const extensions::Extension> > {
[email protected]84df8332011-12-06 18:22:4653 public:
[email protected]23827ec2012-08-10 22:08:0854 const_iterator();
55 const_iterator(const const_iterator& other);
56 explicit const_iterator(ExtensionMap::const_iterator it);
[email protected]84df8332011-12-06 18:22:4657 const_iterator& operator++() {
58 ++it_;
59 return *this;
60 }
[email protected]1c321ee52012-05-21 03:02:3461 const scoped_refptr<const extensions::Extension> operator*() {
[email protected]84df8332011-12-06 18:22:4662 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]67570602011-08-23 21:50:5470
[email protected]ec7db282011-01-29 01:11:3671 ExtensionSet();
72 ~ExtensionSet();
[email protected]5351dbc2010-08-27 15:22:1173
[email protected]2a521c52011-01-26 18:45:2174 size_t size() const;
[email protected]84df8332011-12-06 18:22:4675 bool is_empty() const;
[email protected]5351dbc2010-08-27 15:22:1176
[email protected]7b6eba82011-10-16 03:08:1177 // Iteration support.
[email protected]84df8332011-12-06 18:22:4678 const_iterator begin() const { return const_iterator(extensions_.begin()); }
79 const_iterator end() const { return const_iterator(extensions_.end()); }
[email protected]7b6eba82011-10-16 03:08:1180
[email protected]ec7db282011-01-29 01:11:3681 // Returns true if the set contains the specified extension.
[email protected]7b6eba82011-10-16 03:08:1182 bool Contains(const std::string& id) const;
[email protected]ec7db282011-01-29 01:11:3683
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]1c321ee52012-05-21 03:02:3486 void Insert(const scoped_refptr<const extensions::Extension>& extension);
[email protected]2a521c52011-01-26 18:45:2187
[email protected]7f4308d2012-01-18 07:43:0188 // Copies different items from |extensions| to the current set and returns
89 // whether anything changed.
90 bool InsertAll(const ExtensionSet& extensions);
91
[email protected]2a521c52011-01-26 18:45:2192 // Removes the specified extension.
93 void Remove(const std::string& id);
[email protected]5351dbc2010-08-27 15:22:1194
[email protected]84df8332011-12-06 18:22:4695 // Removes all extensions.
96 void Clear();
97
[email protected]69729ca2011-12-02 08:01:2598 // Returns the extension ID, or empty if none. This includes web URLs that
99 // are part of an extension's web extent.
[email protected]615d88f2011-12-13 01:47:44100 std::string GetExtensionOrAppIDByURL(const ExtensionURLInfo& info) const;
[email protected]583d45c12010-08-31 02:48:12101
[email protected]69729ca2011-12-02 08:01:25102 // Returns the Extension, or NULL if none. This includes web URLs that are
103 // part of an extension's web extent.
[email protected]583d45c12010-08-31 02:48:12104 // NOTE: This can return NULL if called before UpdateExtensions receives
105 // bulk extension data (e.g. if called from
106 // EventBindings::HandleContextCreated)
[email protected]1c321ee52012-05-21 03:02:34107 const extensions::Extension* GetExtensionOrAppByURL(
108 const ExtensionURLInfo& info) const;
[email protected]615d88f2011-12-13 01:47:44109
110 // Returns the hosted app whose web extent contains the URL.
[email protected]1c321ee52012-05-21 03:02:34111 const extensions::Extension* GetHostedAppByURL(
112 const ExtensionURLInfo& info) const;
[email protected]615d88f2011-12-13 01:47:44113
114 // Returns a hosted app that contains any URL that overlaps with the given
115 // extent, if one exists.
[email protected]1c321ee52012-05-21 03:02:34116 const extensions::Extension* GetHostedAppByOverlappingWebExtent(
[email protected]615d88f2011-12-13 01:47:44117 const URLPatternSet& extent) const;
[email protected]5351dbc2010-08-27 15:22:11118
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]2a521c52011-01-26 18:45:21121 bool InSameExtent(const GURL& old_url, const GURL& new_url) const;
[email protected]5351dbc2010-08-27 15:22:11122
[email protected]2a521c52011-01-26 18:45:21123 // Look up an Extension object by id.
[email protected]1c321ee52012-05-21 03:02:34124 const extensions::Extension* GetByID(const std::string& id) const;
[email protected]5351dbc2010-08-27 15:22:11125
[email protected]69729ca2011-12-02 08:01:25126 // Returns true if |info| should get extension api bindings and be permitted
[email protected]583d45c12010-08-31 02:48:12127 // to make api calls. Note that this is independent of what extension
128 // permissions the given extension has been granted.
[email protected]69729ca2011-12-02 08:01:25129 bool ExtensionBindingsAllowed(const ExtensionURLInfo& info) const;
[email protected]583d45c12010-08-31 02:48:12130
[email protected]dbb24162012-06-06 01:41:22131 // 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]5351dbc2010-08-27 15:22:11135 private:
[email protected]ec7db282011-01-29 01:11:36136 FRIEND_TEST_ALL_PREFIXES(ExtensionSetTest, ExtensionSet);
[email protected]5351dbc2010-08-27 15:22:11137
[email protected]2a521c52011-01-26 18:45:21138 ExtensionMap extensions_;
139
[email protected]ec7db282011-01-29 01:11:36140 DISALLOW_COPY_AND_ASSIGN(ExtensionSet);
[email protected]5351dbc2010-08-27 15:22:11141};
142
[email protected]ec7db282011-01-29 01:11:36143#endif // CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_