Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/common/extension_set.h" | 5 #include "extensions/common/extension_set.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "extensions/common/constants.h" | 10 #include "extensions/common/constants.h" |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 37 | 37 |
| 38 bool ExtensionSet::is_empty() const { | 38 bool ExtensionSet::is_empty() const { |
| 39 return extensions_.empty(); | 39 return extensions_.empty(); |
| 40 } | 40 } |
| 41 | 41 |
| 42 bool ExtensionSet::Contains(const std::string& extension_id) const { | 42 bool ExtensionSet::Contains(const std::string& extension_id) const { |
| 43 return extensions_.find(extension_id) != extensions_.end(); | 43 return extensions_.find(extension_id) != extensions_.end(); |
| 44 } | 44 } |
| 45 | 45 |
| 46 bool ExtensionSet::Insert(const scoped_refptr<const Extension>& extension) { | 46 bool ExtensionSet::Insert(const scoped_refptr<const Extension>& extension) { |
| 47 base::AutoLock auto_lock(runtime_lock_); | |
|
not at google - send to devlin
2015/08/13 23:33:55
I think we'll need the lock over every method, not
| |
| 47 bool was_present = ContainsKey(extensions_, extension->id()); | 48 bool was_present = ContainsKey(extensions_, extension->id()); |
| 48 extensions_[extension->id()] = extension; | 49 extensions_[extension->id()] = extension; |
| 49 if (!was_present && !modification_callback_.is_null()) | 50 if (!was_present && !modification_callback_.is_null()) |
| 50 modification_callback_.Run(GetIDs()); | 51 modification_callback_.Run(GetIDs()); |
| 51 return !was_present; | 52 return !was_present; |
| 52 } | 53 } |
| 53 | 54 |
| 54 bool ExtensionSet::InsertAll(const ExtensionSet& extensions) { | 55 bool ExtensionSet::InsertAll(const ExtensionSet& extensions) { |
| 55 size_t before = size(); | 56 size_t before = size(); |
| 56 for (ExtensionSet::const_iterator iter = extensions.begin(); | 57 for (ExtensionSet::const_iterator iter = extensions.begin(); |
| 57 iter != extensions.end(); ++iter) { | 58 iter != extensions.end(); ++iter) { |
| 58 Insert(*iter); | 59 Insert(*iter); |
| 59 } | 60 } |
| 60 return size() != before; | 61 return size() != before; |
| 61 } | 62 } |
| 62 | 63 |
| 63 bool ExtensionSet::Remove(const std::string& id) { | 64 bool ExtensionSet::Remove(const std::string& id) { |
| 65 base::AutoLock auto_lock(runtime_lock_); | |
| 64 bool was_present = extensions_.erase(id) > 0; | 66 bool was_present = extensions_.erase(id) > 0; |
| 65 if (was_present && !modification_callback_.is_null()) | 67 if (was_present && !modification_callback_.is_null()) |
| 66 modification_callback_.Run(GetIDs()); | 68 modification_callback_.Run(GetIDs()); |
| 67 return was_present; | 69 return was_present; |
| 68 } | 70 } |
| 69 | 71 |
| 70 void ExtensionSet::Clear() { | 72 void ExtensionSet::Clear() { |
| 73 base::AutoLock auto_lock(runtime_lock_); | |
| 71 extensions_.clear(); | 74 extensions_.clear(); |
| 72 } | 75 } |
| 73 | 76 |
| 74 std::string ExtensionSet::GetExtensionOrAppIDByURL(const GURL& url) const { | 77 std::string ExtensionSet::GetExtensionOrAppIDByURL(const GURL& url) const { |
| 75 if (url.SchemeIs(kExtensionScheme)) | 78 if (url.SchemeIs(kExtensionScheme)) |
| 76 return url.host(); | 79 return url.host(); |
| 77 | 80 |
| 78 const Extension* extension = GetHostedAppByURL(url); | 81 const Extension* extension = GetHostedAppByURL(url); |
| 79 if (!extension) | 82 if (!extension) |
| 80 return std::string(); | 83 return std::string(); |
| 81 | 84 |
| 82 return extension->id(); | 85 return extension->id(); |
| 83 } | 86 } |
| 84 | 87 |
| 85 const Extension* ExtensionSet::GetExtensionOrAppByURL(const GURL& url) const { | 88 const Extension* ExtensionSet::GetExtensionOrAppByURL(const GURL& url) const { |
| 86 if (url.SchemeIs(kExtensionScheme)) | 89 if (url.SchemeIs(kExtensionScheme)) |
| 87 return GetByID(url.host()); | 90 return GetByID(url.host()); |
| 88 | 91 |
| 89 return GetHostedAppByURL(url); | 92 return GetHostedAppByURL(url); |
| 90 } | 93 } |
| 91 | 94 |
| 92 const Extension* ExtensionSet::GetAppByURL(const GURL& url) const { | 95 const Extension* ExtensionSet::GetAppByURL(const GURL& url) const { |
| 93 const Extension* extension = GetExtensionOrAppByURL(url); | 96 const Extension* extension = GetExtensionOrAppByURL(url); |
| 94 return (extension && extension->is_app()) ? extension : NULL; | 97 return (extension && extension->is_app()) ? extension : NULL; |
| 95 } | 98 } |
| 96 | 99 |
| 97 const Extension* ExtensionSet::GetHostedAppByURL(const GURL& url) const { | 100 const Extension* ExtensionSet::GetHostedAppByURL(const GURL& url) const { |
| 101 base::AutoLock auto_lock(runtime_lock_); | |
| 98 for (ExtensionMap::const_iterator iter = extensions_.begin(); | 102 for (ExtensionMap::const_iterator iter = extensions_.begin(); |
| 99 iter != extensions_.end(); ++iter) { | 103 iter != extensions_.end(); ++iter) { |
| 100 if (iter->second->web_extent().MatchesURL(url)) | 104 if (iter->second->web_extent().MatchesURL(url)) |
| 101 return iter->second.get(); | 105 return iter->second.get(); |
| 102 } | 106 } |
| 103 | 107 |
| 104 return NULL; | 108 return NULL; |
| 105 } | 109 } |
| 106 | 110 |
| 107 const Extension* ExtensionSet::GetHostedAppByOverlappingWebExtent( | 111 const Extension* ExtensionSet::GetHostedAppByOverlappingWebExtent( |
| 108 const URLPatternSet& extent) const { | 112 const URLPatternSet& extent) const { |
| 113 base::AutoLock auto_lock(runtime_lock_); | |
| 109 for (ExtensionMap::const_iterator iter = extensions_.begin(); | 114 for (ExtensionMap::const_iterator iter = extensions_.begin(); |
| 110 iter != extensions_.end(); ++iter) { | 115 iter != extensions_.end(); ++iter) { |
| 111 if (iter->second->web_extent().OverlapsWith(extent)) | 116 if (iter->second->web_extent().OverlapsWith(extent)) |
| 112 return iter->second.get(); | 117 return iter->second.get(); |
| 113 } | 118 } |
| 114 | 119 |
| 115 return NULL; | 120 return NULL; |
| 116 } | 121 } |
| 117 | 122 |
| 118 bool ExtensionSet::InSameExtent(const GURL& old_url, | 123 bool ExtensionSet::InSameExtent(const GURL& old_url, |
| 119 const GURL& new_url) const { | 124 const GURL& new_url) const { |
| 120 return GetExtensionOrAppByURL(old_url) == | 125 return GetExtensionOrAppByURL(old_url) == |
| 121 GetExtensionOrAppByURL(new_url); | 126 GetExtensionOrAppByURL(new_url); |
| 122 } | 127 } |
| 123 | 128 |
| 124 const Extension* ExtensionSet::GetByID(const std::string& id) const { | 129 const Extension* ExtensionSet::GetByID(const std::string& id) const { |
| 130 base::AutoLock auto_lock(runtime_lock_); | |
| 125 ExtensionMap::const_iterator i = extensions_.find(id); | 131 ExtensionMap::const_iterator i = extensions_.find(id); |
| 126 if (i != extensions_.end()) | 132 if (i != extensions_.end()) |
| 127 return i->second.get(); | 133 return i->second.get(); |
| 128 else | 134 else |
| 129 return NULL; | 135 return NULL; |
| 130 } | 136 } |
| 131 | 137 |
| 132 ExtensionIdSet ExtensionSet::GetIDs() const { | 138 ExtensionIdSet ExtensionSet::GetIDs() const { |
| 139 base::AutoLock auto_lock(runtime_lock_); | |
| 133 ExtensionIdSet ids; | 140 ExtensionIdSet ids; |
| 134 for (ExtensionMap::const_iterator it = extensions_.begin(); | 141 for (ExtensionMap::const_iterator it = extensions_.begin(); |
| 135 it != extensions_.end(); ++it) { | 142 it != extensions_.end(); ++it) { |
| 136 ids.insert(it->first); | 143 ids.insert(it->first); |
| 137 } | 144 } |
| 138 return ids; | 145 return ids; |
| 139 } | 146 } |
| 140 | 147 |
| 141 bool ExtensionSet::ExtensionBindingsAllowed(const GURL& url) const { | 148 bool ExtensionSet::ExtensionBindingsAllowed(const GURL& url) const { |
| 149 base::AutoLock auto_lock(runtime_lock_); | |
| 142 if (url.SchemeIs(kExtensionScheme)) | 150 if (url.SchemeIs(kExtensionScheme)) |
| 143 return true; | 151 return true; |
| 144 | 152 |
| 145 for (ExtensionMap::const_iterator it = extensions_.begin(); | 153 for (ExtensionMap::const_iterator it = extensions_.begin(); |
| 146 it != extensions_.end(); ++it) { | 154 it != extensions_.end(); ++it) { |
| 147 if (it->second->location() == Manifest::COMPONENT && | 155 if (it->second->location() == Manifest::COMPONENT && |
| 148 it->second->web_extent().MatchesURL(url)) | 156 it->second->web_extent().MatchesURL(url)) |
| 149 return true; | 157 return true; |
| 150 } | 158 } |
| 151 | 159 |
| 152 return false; | 160 return false; |
| 153 } | 161 } |
| 154 | 162 |
| 155 } // namespace extensions | 163 } // namespace extensions |
| OLD | NEW |