OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/common/extensions/extension_set.h" | 5 #include "chrome/common/extensions/extension_set.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "chrome/common/url_constants.h" | 8 #include "chrome/common/url_constants.h" |
9 | 9 |
| 10 using WebKit::WebSecurityOrigin; |
| 11 |
| 12 ExtensionURLInfo::ExtensionURLInfo(WebSecurityOrigin origin, const GURL& url) |
| 13 : origin_(origin), |
| 14 url_(url) { |
| 15 DCHECK(!origin_.isNull()); |
| 16 } |
| 17 |
| 18 ExtensionURLInfo::ExtensionURLInfo(const GURL& url) |
| 19 : url_(url) { |
| 20 } |
| 21 |
10 ExtensionSet::ExtensionSet() { | 22 ExtensionSet::ExtensionSet() { |
11 } | 23 } |
12 | 24 |
13 ExtensionSet::~ExtensionSet() { | 25 ExtensionSet::~ExtensionSet() { |
14 } | 26 } |
15 | 27 |
16 size_t ExtensionSet::size() const { | 28 size_t ExtensionSet::size() const { |
17 return extensions_.size(); | 29 return extensions_.size(); |
18 } | 30 } |
19 | 31 |
20 bool ExtensionSet::Contains(const std::string& extension_id) const { | 32 bool ExtensionSet::Contains(const std::string& extension_id) const { |
21 return extensions_.find(extension_id) != extensions_.end(); | 33 return extensions_.find(extension_id) != extensions_.end(); |
22 } | 34 } |
23 | 35 |
24 void ExtensionSet::Insert(const scoped_refptr<const Extension>& extension) { | 36 void ExtensionSet::Insert(const scoped_refptr<const Extension>& extension) { |
25 extensions_[extension->id()] = extension; | 37 extensions_[extension->id()] = extension; |
26 } | 38 } |
27 | 39 |
28 void ExtensionSet::Remove(const std::string& id) { | 40 void ExtensionSet::Remove(const std::string& id) { |
29 extensions_.erase(id); | 41 extensions_.erase(id); |
30 } | 42 } |
31 | 43 |
32 std::string ExtensionSet::GetIdByURL(const GURL& url) const { | 44 std::string ExtensionSet::GetIdByURL(const ExtensionURLInfo& info) const { |
33 if (url.SchemeIs(chrome::kExtensionScheme)) | 45 DCHECK(!info.origin().isNull()); |
34 return url.host(); | |
35 | 46 |
36 const Extension* extension = GetByURL(url); | 47 if (info.url().SchemeIs(chrome::kExtensionScheme)) |
| 48 return info.origin().isUnique() ? "" : info.url().host(); |
| 49 |
| 50 const Extension* extension = GetByURL(info); |
37 if (!extension) | 51 if (!extension) |
38 return ""; | 52 return ""; |
39 | 53 |
40 return extension->id(); | 54 return extension->id(); |
41 } | 55 } |
42 | 56 |
43 const Extension* ExtensionSet::GetByURL(const GURL& url) const { | 57 const Extension* ExtensionSet::GetByURL(const ExtensionURLInfo& info) const { |
44 if (url.SchemeIs(chrome::kExtensionScheme)) | 58 // In the common case, the document's origin will correspond to its URL, |
45 return GetByID(url.host()); | 59 // but in some rare cases involving sandboxing, the two will be different. |
| 60 // We catch those cases by checking whether the document's origin is unique. |
| 61 // If that's not the case, then we conclude that the document's security |
| 62 // context is well-described by its URL and proceed to use only the URL. |
| 63 if (!info.origin().isNull() && info.origin().isUnique()) |
| 64 return NULL; |
| 65 |
| 66 if (info.url().SchemeIs(chrome::kExtensionScheme)) |
| 67 return GetByID(info.url().host()); |
46 | 68 |
47 ExtensionMap::const_iterator i = extensions_.begin(); | 69 ExtensionMap::const_iterator i = extensions_.begin(); |
48 for (; i != extensions_.end(); ++i) { | 70 for (; i != extensions_.end(); ++i) { |
49 if (i->second->web_extent().MatchesURL(url)) | 71 if (i->second->web_extent().MatchesURL(info.url())) |
50 return i->second.get(); | 72 return i->second.get(); |
51 } | 73 } |
52 | 74 |
53 return NULL; | 75 return NULL; |
54 } | 76 } |
55 | 77 |
56 bool ExtensionSet::InSameExtent(const GURL& old_url, | 78 bool ExtensionSet::InSameExtent(const GURL& old_url, |
57 const GURL& new_url) const { | 79 const GURL& new_url) const { |
58 return GetByURL(old_url) == GetByURL(new_url); | 80 return GetByURL(ExtensionURLInfo(old_url)) == |
| 81 GetByURL(ExtensionURLInfo(new_url)); |
59 } | 82 } |
60 | 83 |
61 const Extension* ExtensionSet::GetByID(const std::string& id) const { | 84 const Extension* ExtensionSet::GetByID(const std::string& id) const { |
62 ExtensionMap::const_iterator i = extensions_.find(id); | 85 ExtensionMap::const_iterator i = extensions_.find(id); |
63 if (i != extensions_.end()) | 86 if (i != extensions_.end()) |
64 return i->second.get(); | 87 return i->second.get(); |
65 else | 88 else |
66 return NULL; | 89 return NULL; |
67 } | 90 } |
68 | 91 |
69 bool ExtensionSet::ExtensionBindingsAllowed(const GURL& url) const { | 92 bool ExtensionSet::ExtensionBindingsAllowed( |
70 if (url.SchemeIs(chrome::kExtensionScheme)) | 93 const ExtensionURLInfo& info) const { |
| 94 if (info.origin().isUnique()) |
| 95 return false; |
| 96 |
| 97 if (info.url().SchemeIs(chrome::kExtensionScheme)) |
71 return true; | 98 return true; |
72 | 99 |
73 ExtensionMap::const_iterator i = extensions_.begin(); | 100 ExtensionMap::const_iterator i = extensions_.begin(); |
74 for (; i != extensions_.end(); ++i) { | 101 for (; i != extensions_.end(); ++i) { |
75 if (i->second->location() == Extension::COMPONENT && | 102 if (i->second->location() == Extension::COMPONENT && |
76 i->second->web_extent().MatchesURL(url)) | 103 i->second->web_extent().MatchesURL(info.url())) |
77 return true; | 104 return true; |
78 } | 105 } |
79 | 106 |
80 return false; | 107 return false; |
81 } | 108 } |
OLD | NEW |