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 const_iterator()17ExtensionSet::const_iterator::const_iterator() {} 18 const_iterator(const const_iterator & other)19ExtensionSet::const_iterator::const_iterator(const const_iterator& other) 20 : it_(other.it_) { 21 } 22 const_iterator(ExtensionMap::const_iterator it)23ExtensionSet::const_iterator::const_iterator(ExtensionMap::const_iterator it) 24 : it_(it) { 25 } 26 ~const_iterator()27ExtensionSet::const_iterator::~const_iterator() {} 28 ExtensionSet()29ExtensionSet::ExtensionSet() { 30 } 31 ~ExtensionSet()32ExtensionSet::~ExtensionSet() { 33 } 34 size() const35size_t ExtensionSet::size() const { 36 return extensions_.size(); 37 } 38 is_empty() const39bool ExtensionSet::is_empty() const { 40 return extensions_.empty(); 41 } 42 Contains(const std::string & extension_id) const43bool ExtensionSet::Contains(const std::string& extension_id) const { 44 return extensions_.find(extension_id) != extensions_.end(); 45 } 46 Insert(const scoped_refptr<const Extension> & extension)47bool 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 InsertAll(const ExtensionSet & extensions)55bool 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 Remove(const std::string & id)64bool 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 Clear()71void ExtensionSet::Clear() { 72 extensions_.clear(); 73 } 74 GetExtensionOrAppIDByURL(const GURL & url) const75std::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 GetExtensionOrAppByURL(const GURL & url) const86const 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 GetHostedAppByURL(const GURL & url) const93const 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 GetHostedAppByOverlappingWebExtent(const extensions::URLPatternSet & extent) const103const 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 InSameExtent(const GURL & old_url,const GURL & new_url) const114bool ExtensionSet::InSameExtent(const GURL& old_url, 115 const GURL& new_url) const { 116 return GetExtensionOrAppByURL(old_url) == 117 GetExtensionOrAppByURL(new_url); 118 } 119 GetByID(const std::string & id) const120const 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 GetIDs() const128extensions::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 ExtensionBindingsAllowed(const GURL & url) const137bool 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