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#include "chrome/common/extensions/extension_set.h"
6
7#include "base/logging.h"
8#include "chrome/common/url_constants.h"
9
10ExtensionSet::ExtensionSet() {
11}
12
13ExtensionSet::~ExtensionSet() {
14}
15
16size_t ExtensionSet::size() const {
17  return extensions_.size();
18}
19
20bool ExtensionSet::Contains(const std::string& extension_id) {
21  return extensions_.find(extension_id) != extensions_.end();
22}
23
24void ExtensionSet::Insert(const scoped_refptr<const Extension>& extension) {
25  extensions_[extension->id()] = extension;
26}
27
28void ExtensionSet::Remove(const std::string& id) {
29  extensions_.erase(id);
30}
31
32std::string ExtensionSet::GetIdByURL(const GURL& url) const {
33  if (url.SchemeIs(chrome::kExtensionScheme))
34    return url.host();
35
36  const Extension* extension = GetByURL(url);
37  if (!extension)
38    return "";
39
40  return extension->id();
41}
42
43const Extension* ExtensionSet::GetByURL(const GURL& url) const {
44  if (url.SchemeIs(chrome::kExtensionScheme))
45    return GetByID(url.host());
46
47  ExtensionMap::const_iterator i = extensions_.begin();
48  for (; i != extensions_.end(); ++i) {
49    if (i->second->web_extent().ContainsURL(url))
50      return i->second.get();
51  }
52
53  return NULL;
54}
55
56bool ExtensionSet::InSameExtent(const GURL& old_url,
57                                const GURL& new_url) const {
58  return GetByURL(old_url) == GetByURL(new_url);
59}
60
61const Extension* ExtensionSet::GetByID(const std::string& id) const {
62  ExtensionMap::const_iterator i = extensions_.find(id);
63  if (i != extensions_.end())
64    return i->second.get();
65  else
66    return NULL;
67}
68
69bool ExtensionSet::ExtensionBindingsAllowed(const GURL& url) const {
70  if (url.SchemeIs(chrome::kExtensionScheme))
71    return true;
72
73  ExtensionMap::const_iterator i = extensions_.begin();
74  for (; i != extensions_.end(); ++i) {
75    if (i->second->location() == Extension::COMPONENT &&
76        i->second->web_extent().ContainsURL(url))
77      return true;
78  }
79
80  return false;
81}
82