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.
4
5#ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_
6#define CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_
7
8#include <iterator>
9#include <map>
10#include <string>
11
12#include "base/gtest_prod_util.h"
13#include "base/memory/ref_counted.h"
14#include "chrome/common/extensions/extension.h"
15#include "url/gurl.h"
16
17// The one true extension container. Extensions are identified by their id.
18// Only one extension can be in the set with a given ID.
19class ExtensionSet {
20 public:
21  typedef std::pair<base::FilePath, std::string> ExtensionPathAndDefaultLocale;
22  typedef std::map<std::string, scoped_refptr<const extensions::Extension> >
23      ExtensionMap;
24
25  // Iteration over the values of the map (given that it's an ExtensionSet,
26  // it should iterate like a set iterator).
27  class const_iterator :
28      public std::iterator<std::input_iterator_tag,
29                           scoped_refptr<const extensions::Extension> > {
30   public:
31    const_iterator();
32    const_iterator(const const_iterator& other);
33    explicit const_iterator(ExtensionMap::const_iterator it);
34    const_iterator& operator++() {
35      ++it_;
36      return *this;
37    }
38    const scoped_refptr<const extensions::Extension> operator*() {
39      return it_->second;
40    }
41    const scoped_refptr<const extensions::Extension>* operator->() {
42      return &it_->second;
43    }
44    bool operator!=(const const_iterator& other) { return it_ != other.it_; }
45    bool operator==(const const_iterator& other) { return it_ == other.it_; }
46
47   private:
48    ExtensionMap::const_iterator it_;
49  };
50
51  ExtensionSet();
52  ~ExtensionSet();
53
54  size_t size() const;
55  bool is_empty() const;
56
57  // Iteration support.
58  const_iterator begin() const { return const_iterator(extensions_.begin()); }
59  const_iterator end() const { return const_iterator(extensions_.end()); }
60
61  // Returns true if the set contains the specified extension.
62  bool Contains(const std::string& id) const;
63
64  // Adds the specified extension to the set. The set becomes an owner. Any
65  // previous extension with the same ID is removed.
66  // Returns true if there is no previous extension.
67  bool Insert(const scoped_refptr<const extensions::Extension>& extension);
68
69  // Copies different items from |extensions| to the current set and returns
70  // whether anything changed.
71  bool InsertAll(const ExtensionSet& extensions);
72
73  // Removes the specified extension.
74  // Returns true if the set contained the specified extnesion.
75  bool Remove(const std::string& id);
76
77  // Removes all extensions.
78  void Clear();
79
80  // Returns the extension ID, or empty if none. This includes web URLs that
81  // are part of an extension's web extent.
82  std::string GetExtensionOrAppIDByURL(const GURL& url) const;
83
84  // Returns the Extension, or NULL if none.  This includes web URLs that are
85  // part of an extension's web extent.
86  // NOTE: This can return NULL if called before UpdateExtensions receives
87  // bulk extension data (e.g. if called from
88  // EventBindings::HandleContextCreated)
89  const extensions::Extension* GetExtensionOrAppByURL(const GURL& url) const;
90
91  // Returns the hosted app whose web extent contains the URL.
92  const extensions::Extension* GetHostedAppByURL(const GURL& url) const;
93
94  // Returns a hosted app that contains any URL that overlaps with the given
95  // extent, if one exists.
96  const extensions::Extension* GetHostedAppByOverlappingWebExtent(
97      const extensions::URLPatternSet& extent) const;
98
99  // Returns true if |new_url| is in the extent of the same extension as
100  // |old_url|.  Also returns true if neither URL is in an app.
101  bool InSameExtent(const GURL& old_url, const GURL& new_url) const;
102
103  // Look up an Extension object by id.
104  const extensions::Extension* GetByID(const std::string& id) const;
105
106  // Gets the IDs of all extensions in the set.
107  std::set<std::string> GetIDs() const;
108
109  // Returns true if |info| should get extension api bindings and be permitted
110  // to make api calls. Note that this is independent of what extension
111  // permissions the given extension has been granted.
112  bool ExtensionBindingsAllowed(const GURL& url) const;
113
114 private:
115  FRIEND_TEST_ALL_PREFIXES(ExtensionSetTest, ExtensionSet);
116
117  ExtensionMap extensions_;
118
119  DISALLOW_COPY_AND_ASSIGN(ExtensionSet);
120};
121
122#endif  // CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_
123