blob: 54405ea7a789eeaa298e34b72d962a57f0dbb021 [file] [log] [blame]
[email protected]289c44b2013-12-17 03:26:571// Copyright 2013 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]289c44b2013-12-17 03:26:575#ifndef EXTENSIONS_COMMON_EXTENSION_SET_H_
6#define EXTENSIONS_COMMON_EXTENSION_SET_H_
[email protected]5351dbc2010-08-27 15:22:117
avi2d124c02015-12-23 06:36:428#include <stddef.h>
9
[email protected]84df8332011-12-06 18:22:4610#include <iterator>
[email protected]67570602011-08-23 21:50:5411#include <map>
[email protected]5351dbc2010-08-27 15:22:1112#include <string>
[email protected]5351dbc2010-08-27 15:22:1113
[email protected]076ebeda2014-06-06 21:47:2614#include "base/callback.h"
[email protected]5351dbc2010-08-27 15:22:1115#include "base/gtest_prod_util.h"
avi2d124c02015-12-23 06:36:4216#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1517#include "base/memory/ref_counted.h"
[email protected]e4452d32013-11-15 23:07:4118#include "extensions/common/extension.h"
[email protected]5f5ef802013-07-04 16:11:1319#include "url/gurl.h"
[email protected]69729ca2011-12-02 08:01:2520
[email protected]289c44b2013-12-17 03:26:5721namespace extensions {
22
[email protected]ec7db282011-01-29 01:11:3623// The one true extension container. Extensions are identified by their id.
24// Only one extension can be in the set with a given ID.
25class ExtensionSet {
[email protected]dbb24162012-06-06 01:41:2226 public:
[email protected]a7329162013-02-07 19:21:4827 typedef std::pair<base::FilePath, std::string> ExtensionPathAndDefaultLocale;
[email protected]289c44b2013-12-17 03:26:5728 typedef std::map<std::string, scoped_refptr<const Extension> > ExtensionMap;
[email protected]84df8332011-12-06 18:22:4629
30 // Iteration over the values of the map (given that it's an ExtensionSet,
31 // it should iterate like a set iterator).
[email protected]289c44b2013-12-17 03:26:5732 class const_iterator : public std::iterator<std::input_iterator_tag,
33 scoped_refptr<const Extension> > {
[email protected]84df8332011-12-06 18:22:4634 public:
[email protected]23827ec2012-08-10 22:08:0835 const_iterator();
36 const_iterator(const const_iterator& other);
37 explicit const_iterator(ExtensionMap::const_iterator it);
[email protected]a8d94b42013-12-10 18:52:2238 ~const_iterator();
[email protected]84df8332011-12-06 18:22:4639 const_iterator& operator++() {
40 ++it_;
41 return *this;
42 }
[email protected]d478a1d2013-10-10 22:09:5043 const_iterator operator++(int) {
44 const const_iterator old(*this);
45 ++it_;
46 return old;
47 }
[email protected]289c44b2013-12-17 03:26:5748 const scoped_refptr<const Extension>& operator*() const {
[email protected]84df8332011-12-06 18:22:4649 return it_->second;
50 }
[email protected]289c44b2013-12-17 03:26:5751 const scoped_refptr<const Extension>* operator->() const {
[email protected]cadac622013-06-11 16:46:3652 return &it_->second;
53 }
[email protected]d478a1d2013-10-10 22:09:5054 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]84df8332011-12-06 18:22:4660
61 private:
62 ExtensionMap::const_iterator it_;
63 };
[email protected]67570602011-08-23 21:50:5464
[email protected]ec7db282011-01-29 01:11:3665 ExtensionSet();
66 ~ExtensionSet();
[email protected]5351dbc2010-08-27 15:22:1167
[email protected]2a521c52011-01-26 18:45:2168 size_t size() const;
[email protected]84df8332011-12-06 18:22:4669 bool is_empty() const;
[email protected]5351dbc2010-08-27 15:22:1170
[email protected]7b6eba82011-10-16 03:08:1171 // Iteration support.
[email protected]84df8332011-12-06 18:22:4672 const_iterator begin() const { return const_iterator(extensions_.begin()); }
73 const_iterator end() const { return const_iterator(extensions_.end()); }
[email protected]7b6eba82011-10-16 03:08:1174
[email protected]ec7db282011-01-29 01:11:3675 // Returns true if the set contains the specified extension.
[email protected]7b6eba82011-10-16 03:08:1176 bool Contains(const std::string& id) const;
[email protected]ec7db282011-01-29 01:11:3677
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]ecda2cf2013-04-22 09:57:0980 // Returns true if there is no previous extension.
[email protected]289c44b2013-12-17 03:26:5781 bool Insert(const scoped_refptr<const Extension>& extension);
[email protected]2a521c52011-01-26 18:45:2182
[email protected]7f4308d2012-01-18 07:43:0183 // Copies different items from |extensions| to the current set and returns
84 // whether anything changed.
85 bool InsertAll(const ExtensionSet& extensions);
86
[email protected]2a521c52011-01-26 18:45:2187 // Removes the specified extension.
[email protected]695b5712012-12-06 23:55:2888 // Returns true if the set contained the specified extnesion.
89 bool Remove(const std::string& id);
[email protected]5351dbc2010-08-27 15:22:1190
[email protected]84df8332011-12-06 18:22:4691 // Removes all extensions.
92 void Clear();
93
[email protected]69729ca2011-12-02 08:01:2594 // Returns the extension ID, or empty if none. This includes web URLs that
95 // are part of an extension's web extent.
[email protected]be9915fb2013-07-18 09:28:5596 std::string GetExtensionOrAppIDByURL(const GURL& url) const;
[email protected]583d45c12010-08-31 02:48:1297
[email protected]69729ca2011-12-02 08:01:2598 // Returns the Extension, or NULL if none. This includes web URLs that are
99 // part of an extension's web extent.
[email protected]583d45c12010-08-31 02:48:12100 // NOTE: This can return NULL if called before UpdateExtensions receives
101 // bulk extension data (e.g. if called from
102 // EventBindings::HandleContextCreated)
[email protected]289c44b2013-12-17 03:26:57103 const Extension* GetExtensionOrAppByURL(const GURL& url) const;
[email protected]615d88f2011-12-13 01:47:44104
[email protected]d685c565b2014-04-10 21:00:37105 // 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]615d88f2011-12-13 01:47:44110 // Returns the hosted app whose web extent contains the URL.
[email protected]289c44b2013-12-17 03:26:57111 const Extension* GetHostedAppByURL(const GURL& url) const;
[email protected]615d88f2011-12-13 01:47:44112
113 // Returns a hosted app that contains any URL that overlaps with the given
114 // extent, if one exists.
[email protected]289c44b2013-12-17 03:26:57115 const Extension* GetHostedAppByOverlappingWebExtent(
116 const URLPatternSet& extent) const;
[email protected]5351dbc2010-08-27 15:22:11117
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]2a521c52011-01-26 18:45:21120 bool InSameExtent(const GURL& old_url, const GURL& new_url) const;
[email protected]5351dbc2010-08-27 15:22:11121
[email protected]2a521c52011-01-26 18:45:21122 // Look up an Extension object by id.
[email protected]289c44b2013-12-17 03:26:57123 const Extension* GetByID(const std::string& id) const;
[email protected]5351dbc2010-08-27 15:22:11124
[email protected]695b5712012-12-06 23:55:28125 // Gets the IDs of all extensions in the set.
[email protected]289c44b2013-12-17 03:26:57126 ExtensionIdSet GetIDs() const;
[email protected]695b5712012-12-06 23:55:28127
[email protected]69729ca2011-12-02 08:01:25128 // Returns true if |info| should get extension api bindings and be permitted
[email protected]583d45c12010-08-31 02:48:12129 // to make api calls. Note that this is independent of what extension
130 // permissions the given extension has been granted.
[email protected]be9915fb2013-07-18 09:28:55131 bool ExtensionBindingsAllowed(const GURL& url) const;
[email protected]dbb24162012-06-06 01:41:22132
[email protected]5351dbc2010-08-27 15:22:11133 private:
[email protected]2a521c52011-01-26 18:45:21134 ExtensionMap extensions_;
135
[email protected]ec7db282011-01-29 01:11:36136 DISALLOW_COPY_AND_ASSIGN(ExtensionSet);
[email protected]5351dbc2010-08-27 15:22:11137};
138
[email protected]289c44b2013-12-17 03:26:57139} // namespace extensions
140
141#endif // EXTENSIONS_COMMON_EXTENSION_SET_H_