1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef EXTENSIONS_COMMON_EXTENSION_SET_H_
6#define EXTENSIONS_COMMON_EXTENSION_SET_H_
7
8#include <stddef.h>
9
10#include <iterator>
11#include <map>
12#include <string>
13
14#include "base/callback.h"
15#include "base/gtest_prod_util.h"
16#include "base/macros.h"
17#include "base/memory/ref_counted.h"
18#include "extensions/common/extension.h"
19#include "url/gurl.h"
20
21namespace extensions {
22
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.
25class ExtensionSet {
26 public:
27 typedef std::pair<base::FilePath, std::string> ExtensionPathAndDefaultLocale;
28 typedef std::map<std::string, scoped_refptr<const Extension> > ExtensionMap;
29
30 // Iteration over the values of the map (given that it's an ExtensionSet,
31 // it should iterate like a set iterator).
32 class const_iterator : public std::iterator<std::input_iterator_tag,
33 scoped_refptr<const Extension> > {
34 public:
35 const_iterator();
36 const_iterator(const const_iterator& other);
37 explicit const_iterator(ExtensionMap::const_iterator it);
38 ~const_iterator();
39 const_iterator& operator++() {
40 ++it_;
41 return *this;
42 }
43 const_iterator operator++(int) {
44 const const_iterator old(*this);
45 ++it_;
46 return old;
47 }
48 const scoped_refptr<const Extension>& operator*() const {
49 return it_->second;
50 }
51 const scoped_refptr<const Extension>* operator->() const {
52 return &it_->second;
53 }
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 }
60
61 private:
62 ExtensionMap::const_iterator it_;
63 };
64
65 ExtensionSet();
66 ~ExtensionSet();
67
68 size_t size() const;
69 bool is_empty() const;
70
71 // Iteration support.
72 const_iterator begin() const { return const_iterator(extensions_.begin()); }
73 const_iterator end() const { return const_iterator(extensions_.end()); }
74
75 // Returns true if the set contains the specified extension.
76 bool Contains(const std::string& id) const;
77
78 // Adds the specified extension to the set. The set becomes an owner. Any
79 // previous extension with the same ID is removed.
80 // Returns true if there is no previous extension.
81 bool Insert(const scoped_refptr<const Extension>& extension);
82
83 // Copies different items from |extensions| to the current set and returns
84 // whether anything changed.
85 bool InsertAll(const ExtensionSet& extensions);
86
87 // Removes the specified extension.
88 // Returns true if the set contained the specified extension.
89 bool Remove(const std::string& id);
90
91 // Removes all extensions.
92 void Clear();
93
94 // Returns the extension ID, or empty if none. This includes web URLs that
95 // are part of an extension's web extent.
96 std::string GetExtensionOrAppIDByURL(const GURL& url) const;
97
98 // Returns the Extension, or NULL if none. This includes web URLs that are
99 // part of an extension's web extent.
100 // NOTE: This can return NULL if called before UpdateExtensions receives
101 // bulk extension data (e.g. if called from
102 // EventBindings::HandleContextCreated)
103 const Extension* GetExtensionOrAppByURL(const GURL& url) const;
104
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
110 // Returns the hosted app whose web extent contains the URL.
111 const Extension* GetHostedAppByURL(const GURL& url) const;
112
113 // Returns a hosted app that contains any URL that overlaps with the given
114 // extent, if one exists.
115 const Extension* GetHostedAppByOverlappingWebExtent(
116 const URLPatternSet& extent) const;
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.
120 bool InSameExtent(const GURL& old_url, const GURL& new_url) const;
121
122 // Look up an Extension object by id.
123 const Extension* GetByID(const std::string& id) const;
124
125 // Gets the IDs of all extensions in the set.
126 ExtensionIdSet GetIDs() const;
127
128 // Returns true if |info| should get extension api bindings and be permitted
129 // to make api calls. Note that this is independent of what extension
130 // permissions the given extension has been granted.
131 bool ExtensionBindingsAllowed(const GURL& url) const;
132
133 private:
134 ExtensionMap extensions_;
135
136 DISALLOW_COPY_AND_ASSIGN(ExtensionSet);
137};
138
139} // namespace extensions
140
141#endif // EXTENSIONS_COMMON_EXTENSION_SET_H_
142