Home | History | Annotate | Download | only in extensions
      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 
     10 ExtensionSet::ExtensionSet() {
     11 }
     12 
     13 ExtensionSet::~ExtensionSet() {
     14 }
     15 
     16 size_t ExtensionSet::size() const {
     17   return extensions_.size();
     18 }
     19 
     20 bool ExtensionSet::Contains(const std::string& extension_id) {
     21   return extensions_.find(extension_id) != extensions_.end();
     22 }
     23 
     24 void ExtensionSet::Insert(const scoped_refptr<const Extension>& extension) {
     25   extensions_[extension->id()] = extension;
     26 }
     27 
     28 void ExtensionSet::Remove(const std::string& id) {
     29   extensions_.erase(id);
     30 }
     31 
     32 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 
     43 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 
     56 bool ExtensionSet::InSameExtent(const GURL& old_url,
     57                                 const GURL& new_url) const {
     58   return GetByURL(old_url) == GetByURL(new_url);
     59 }
     60 
     61 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 
     69 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