• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ExtensionSet()10 ExtensionSet::ExtensionSet() {
11 }
12 
~ExtensionSet()13 ExtensionSet::~ExtensionSet() {
14 }
15 
size() const16 size_t ExtensionSet::size() const {
17   return extensions_.size();
18 }
19 
Contains(const std::string & extension_id)20 bool ExtensionSet::Contains(const std::string& extension_id) {
21   return extensions_.find(extension_id) != extensions_.end();
22 }
23 
Insert(const scoped_refptr<const Extension> & extension)24 void ExtensionSet::Insert(const scoped_refptr<const Extension>& extension) {
25   extensions_[extension->id()] = extension;
26 }
27 
Remove(const std::string & id)28 void ExtensionSet::Remove(const std::string& id) {
29   extensions_.erase(id);
30 }
31 
GetIdByURL(const GURL & url) const32 std::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 
GetByURL(const GURL & url) const43 const 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 
InSameExtent(const GURL & old_url,const GURL & new_url) const56 bool ExtensionSet::InSameExtent(const GURL& old_url,
57                                 const GURL& new_url) const {
58   return GetByURL(old_url) == GetByURL(new_url);
59 }
60 
GetByID(const std::string & id) const61 const 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 
ExtensionBindingsAllowed(const GURL & url) const69 bool 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