Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / chrome / common / extensions / extension_set.h
blob43635ba1654b4fc992c047acec02be16ec462827
1 // Copyright (c) 2012 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.
5 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_
6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_
8 #include <iterator>
9 #include <map>
10 #include <string>
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/ref_counted.h"
14 #include "chrome/common/extensions/extension.h"
15 #include "googleurl/src/gurl.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
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);
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);
32 const WebKit::WebSecurityOrigin& origin() const { return origin_; }
33 const GURL& url() const { return url_; }
35 private:
36 WebKit::WebSecurityOrigin origin_;
37 GURL url_;
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 {
43 public:
44 typedef std::pair<FilePath, std::string> ExtensionPathAndDefaultLocale;
45 typedef std::map<std::string, scoped_refptr<const extensions::Extension> >
46 ExtensionMap;
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,
52 scoped_refptr<const extensions::Extension> > {
53 public:
54 const_iterator();
55 const_iterator(const const_iterator& other);
56 explicit const_iterator(ExtensionMap::const_iterator it);
57 const_iterator& operator++() {
58 ++it_;
59 return *this;
61 const scoped_refptr<const extensions::Extension> operator*() {
62 return it_->second;
64 bool operator!=(const const_iterator& other) { return it_ != other.it_; }
65 bool operator==(const const_iterator& other) { return it_ == other.it_; }
67 private:
68 ExtensionMap::const_iterator it_;
71 ExtensionSet();
72 ~ExtensionSet();
74 size_t size() const;
75 bool is_empty() const;
77 // Iteration support.
78 const_iterator begin() const { return const_iterator(extensions_.begin()); }
79 const_iterator end() const { return const_iterator(extensions_.end()); }
81 // Returns true if the set contains the specified extension.
82 bool Contains(const std::string& id) const;
84 // Adds the specified extension to the set. The set becomes an owner. Any
85 // previous extension with the same ID is removed.
86 void Insert(const scoped_refptr<const extensions::Extension>& extension);
88 // Copies different items from |extensions| to the current set and returns
89 // whether anything changed.
90 bool InsertAll(const ExtensionSet& extensions);
92 // Removes the specified extension.
93 void Remove(const std::string& id);
95 // Removes all extensions.
96 void Clear();
98 // Returns the extension ID, or empty if none. This includes web URLs that
99 // are part of an extension's web extent.
100 std::string GetExtensionOrAppIDByURL(const ExtensionURLInfo& info) const;
102 // Returns the Extension, or NULL if none. This includes web URLs that are
103 // part of an extension's web extent.
104 // NOTE: This can return NULL if called before UpdateExtensions receives
105 // bulk extension data (e.g. if called from
106 // EventBindings::HandleContextCreated)
107 const extensions::Extension* GetExtensionOrAppByURL(
108 const ExtensionURLInfo& info) const;
110 // Returns the hosted app whose web extent contains the URL.
111 const extensions::Extension* GetHostedAppByURL(
112 const ExtensionURLInfo& info) const;
114 // Returns a hosted app that contains any URL that overlaps with the given
115 // extent, if one exists.
116 const extensions::Extension* GetHostedAppByOverlappingWebExtent(
117 const URLPatternSet& extent) const;
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.
121 bool InSameExtent(const GURL& old_url, const GURL& new_url) const;
123 // Look up an Extension object by id.
124 const extensions::Extension* GetByID(const std::string& id) const;
126 // Returns true if |info| should get extension api bindings and be permitted
127 // to make api calls. Note that this is independent of what extension
128 // permissions the given extension has been granted.
129 bool ExtensionBindingsAllowed(const ExtensionURLInfo& info) const;
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;
135 private:
136 FRIEND_TEST_ALL_PREFIXES(ExtensionSetTest, ExtensionSet);
138 ExtensionMap extensions_;
140 DISALLOW_COPY_AND_ASSIGN(ExtensionSet);
143 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_