1// Copyright (c) 2011 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#pragma once
8
9#include <string>
10#include <vector>
11
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
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  ExtensionSet();
22  ~ExtensionSet();
23
24  // Gets the number of extensions contained.
25  size_t size() const;
26
27  // Returns true if the set contains the specified extension.
28  bool Contains(const std::string& id);
29
30  // Adds the specified extension to the set. The set becomes an owner. Any
31  // previous extension with the same ID is removed.
32  void Insert(const scoped_refptr<const Extension>& extension);
33
34  // Removes the specified extension.
35  void Remove(const std::string& id);
36
37  // Returns the extension ID that the given URL is a part of, or empty if
38  // none. This includes web URLs that are part of an extension's web extent.
39  std::string GetIdByURL(const GURL& url) const;
40
41  // Returns the Extension that the given URL is a part of, or NULL if none.
42  // This includes web URLs that are part of an extension's web extent.
43  // NOTE: This can return NULL if called before UpdateExtensions receives
44  // bulk extension data (e.g. if called from
45  // EventBindings::HandleContextCreated)
46  const Extension* GetByURL(const GURL& url) const;
47
48  // Returns true if |new_url| is in the extent of the same extension as
49  // |old_url|.  Also returns true if neither URL is in an app.
50  bool InSameExtent(const GURL& old_url, const GURL& new_url) const;
51
52  // Look up an Extension object by id.
53  const Extension* GetByID(const std::string& id) const;
54
55  // Returns true if |url| should get extension api bindings and be permitted
56  // to make api calls. Note that this is independent of what extension
57  // permissions the given extension has been granted.
58  bool ExtensionBindingsAllowed(const GURL& url) const;
59
60 private:
61  FRIEND_TEST_ALL_PREFIXES(ExtensionSetTest, ExtensionSet);
62
63  // static
64  typedef std::map<std::string, scoped_refptr<const Extension> > ExtensionMap;
65  ExtensionMap extensions_;
66
67  DISALLOW_COPY_AND_ASSIGN(ExtensionSet);
68};
69
70#endif  // CHROME_COMMON_EXTENSIONS_EXTENSION_SET_H_
71