Home | History | Annotate | Download | only in extensions
      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 #include "chrome/common/extensions/extension_set.h"
      6 
      7 #include "base/callback.h"
      8 #include "base/logging.h"
      9 #include "base/stl_util.h"
     10 #include "chrome/common/url_constants.h"
     11 #include "extensions/common/constants.h"
     12 #include "extensions/common/extension.h"
     13 #include "extensions/common/manifest_handlers/sandboxed_page_info.h"
     14 
     15 using extensions::Extension;
     16 
     17 ExtensionSet::const_iterator::const_iterator() {}
     18 
     19 ExtensionSet::const_iterator::const_iterator(const const_iterator& other)
     20     : it_(other.it_) {
     21 }
     22 
     23 ExtensionSet::const_iterator::const_iterator(ExtensionMap::const_iterator it)
     24     : it_(it) {
     25 }
     26 
     27 ExtensionSet::const_iterator::~const_iterator() {}
     28 
     29 ExtensionSet::ExtensionSet() {
     30 }
     31 
     32 ExtensionSet::~ExtensionSet() {
     33 }
     34 
     35 size_t ExtensionSet::size() const {
     36   return extensions_.size();
     37 }
     38 
     39 bool ExtensionSet::is_empty() const {
     40   return extensions_.empty();
     41 }
     42 
     43 bool ExtensionSet::Contains(const std::string& extension_id) const {
     44   return extensions_.find(extension_id) != extensions_.end();
     45 }
     46 
     47 bool ExtensionSet::Insert(const scoped_refptr<const Extension>& extension) {
     48   bool was_present = ContainsKey(extensions_, extension->id());
     49   extensions_[extension->id()] = extension;
     50   if (!was_present && !modification_callback_.is_null())
     51     modification_callback_.Run(GetIDs());
     52   return !was_present;
     53 }
     54 
     55 bool ExtensionSet::InsertAll(const ExtensionSet& extensions) {
     56   size_t before = size();
     57   for (ExtensionSet::const_iterator iter = extensions.begin();
     58        iter != extensions.end(); ++iter) {
     59     Insert(*iter);
     60   }
     61   return size() != before;
     62 }
     63 
     64 bool ExtensionSet::Remove(const std::string& id) {
     65   bool was_present = extensions_.erase(id) > 0;
     66   if (was_present && !modification_callback_.is_null())
     67     modification_callback_.Run(GetIDs());
     68   return was_present;
     69 }
     70 
     71 void ExtensionSet::Clear() {
     72   extensions_.clear();
     73 }
     74 
     75 std::string ExtensionSet::GetExtensionOrAppIDByURL(const GURL& url) const {
     76   if (url.SchemeIs(extensions::kExtensionScheme))
     77     return url.host();
     78 
     79   const Extension* extension = GetExtensionOrAppByURL(url);
     80   if (!extension)
     81     return std::string();
     82 
     83   return extension->id();
     84 }
     85 
     86 const Extension* ExtensionSet::GetExtensionOrAppByURL(const GURL& url) const {
     87   if (url.SchemeIs(extensions::kExtensionScheme))
     88     return GetByID(url.host());
     89 
     90   return GetHostedAppByURL(url);
     91 }
     92 
     93 const Extension* ExtensionSet::GetHostedAppByURL(const GURL& url) const {
     94   for (ExtensionMap::const_iterator iter = extensions_.begin();
     95        iter != extensions_.end(); ++iter) {
     96     if (iter->second->web_extent().MatchesURL(url))
     97       return iter->second.get();
     98   }
     99 
    100   return NULL;
    101 }
    102 
    103 const Extension* ExtensionSet::GetHostedAppByOverlappingWebExtent(
    104     const extensions::URLPatternSet& extent) const {
    105   for (ExtensionMap::const_iterator iter = extensions_.begin();
    106        iter != extensions_.end(); ++iter) {
    107     if (iter->second->web_extent().OverlapsWith(extent))
    108       return iter->second.get();
    109   }
    110 
    111   return NULL;
    112 }
    113 
    114 bool ExtensionSet::InSameExtent(const GURL& old_url,
    115                                 const GURL& new_url) const {
    116   return GetExtensionOrAppByURL(old_url) ==
    117       GetExtensionOrAppByURL(new_url);
    118 }
    119 
    120 const Extension* ExtensionSet::GetByID(const std::string& id) const {
    121   ExtensionMap::const_iterator i = extensions_.find(id);
    122   if (i != extensions_.end())
    123     return i->second.get();
    124   else
    125     return NULL;
    126 }
    127 
    128 extensions::ExtensionIdSet ExtensionSet::GetIDs() const {
    129   extensions::ExtensionIdSet ids;
    130   for (ExtensionMap::const_iterator it = extensions_.begin();
    131        it != extensions_.end(); ++it) {
    132     ids.insert(it->first);
    133   }
    134   return ids;
    135 }
    136 
    137 bool ExtensionSet::ExtensionBindingsAllowed(const GURL& url) const {
    138   if (url.SchemeIs(extensions::kExtensionScheme))
    139     return true;
    140 
    141   ExtensionMap::const_iterator i = extensions_.begin();
    142   for (; i != extensions_.end(); ++i) {
    143     if (i->second->location() == extensions::Manifest::COMPONENT &&
    144         i->second->web_extent().MatchesURL(url))
    145       return true;
    146   }
    147 
    148   return false;
    149 }
    150