OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/public/common/origin_util.h" | 5 #include "content/public/common/origin_util.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "content/public/common/content_client.h" | 9 #include "content/public/common/content_client.h" |
10 #include "net/base/net_util.h" | 10 #include "net/base/net_util.h" |
11 #include "url/gurl.h" | 11 #include "url/gurl.h" |
12 | 12 |
13 namespace content { | 13 namespace content { |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 class SecureSchemeAndOriginSet { | 17 class CustomSchemeAndOriginSet { |
18 public: | 18 public: |
19 SecureSchemeAndOriginSet() { Reset(); } | 19 CustomSchemeAndOriginSet() { Reset(); } |
20 ~SecureSchemeAndOriginSet() {} | 20 ~CustomSchemeAndOriginSet() {} |
21 | 21 |
22 void Reset() { | 22 void Reset() { |
23 GetContentClient()->AddSecureSchemesAndOrigins(&schemes_, &origins_); | 23 GetContentClient()->AddSecureSchemesAndOrigins(&secure_schemes_, |
| 24 &secure_origins_); |
| 25 GetContentClient()->AddServiceWorkerSchemes(&service_worker_schemes_); |
24 } | 26 } |
25 | 27 |
26 const std::set<std::string>& schemes() const { return schemes_; } | 28 const std::set<std::string>& secure_schemes() const { |
27 const std::set<GURL>& origins() const { return origins_; } | 29 return secure_schemes_; |
| 30 } |
| 31 const std::set<GURL>& secure_origins() const { return secure_origins_; } |
| 32 const std::set<std::string>& service_worker_schemes() const { |
| 33 return service_worker_schemes_; |
| 34 } |
28 | 35 |
29 private: | 36 private: |
30 std::set<std::string> schemes_; | 37 std::set<std::string> secure_schemes_; |
31 std::set<GURL> origins_; | 38 std::set<GURL> secure_origins_; |
32 DISALLOW_COPY_AND_ASSIGN(SecureSchemeAndOriginSet); | 39 std::set<std::string> service_worker_schemes_; |
| 40 DISALLOW_COPY_AND_ASSIGN(CustomSchemeAndOriginSet); |
33 }; | 41 }; |
34 | 42 |
35 base::LazyInstance<SecureSchemeAndOriginSet>::Leaky g_trustworthy_whitelist = | 43 base::LazyInstance<CustomSchemeAndOriginSet>::Leaky g_trustworthy_whitelist = |
36 LAZY_INSTANCE_INITIALIZER; | 44 LAZY_INSTANCE_INITIALIZER; |
37 | 45 |
38 } // namespace | 46 } // namespace |
39 | 47 |
40 bool IsOriginSecure(const GURL& url) { | 48 bool IsOriginSecure(const GURL& url) { |
41 if (url.SchemeIsCryptographic() || url.SchemeIsFile()) | 49 if (url.SchemeIsCryptographic() || url.SchemeIsFile()) |
42 return true; | 50 return true; |
43 | 51 |
44 if (url.SchemeIsFileSystem() && url.inner_url() && | 52 if (url.SchemeIsFileSystem() && url.inner_url() && |
45 IsOriginSecure(*url.inner_url())) { | 53 IsOriginSecure(*url.inner_url())) { |
46 return true; | 54 return true; |
47 } | 55 } |
48 | 56 |
49 std::string hostname = url.HostNoBrackets(); | 57 std::string hostname = url.HostNoBrackets(); |
50 if (net::IsLocalhost(hostname)) | 58 if (net::IsLocalhost(hostname)) |
51 return true; | 59 return true; |
52 | 60 |
53 if (ContainsKey(g_trustworthy_whitelist.Get().schemes(), url.scheme())) | 61 if (ContainsKey(g_trustworthy_whitelist.Get().secure_schemes(), url.scheme())) |
54 return true; | 62 return true; |
55 | 63 |
56 if (ContainsKey(g_trustworthy_whitelist.Get().origins(), url.GetOrigin())) | 64 if (ContainsKey(g_trustworthy_whitelist.Get().secure_origins(), |
| 65 url.GetOrigin())) |
57 return true; | 66 return true; |
58 | 67 |
59 return false; | 68 return false; |
60 } | 69 } |
61 | 70 |
62 void ResetSecureSchemesAndOriginsForTesting() { | 71 bool OriginCanAccessServiceWorkers(const GURL& url) { |
| 72 if (url.SchemeIsHTTPOrHTTPS() && IsOriginSecure(url)) |
| 73 return true; |
| 74 |
| 75 if (ContainsKey(g_trustworthy_whitelist.Get().service_worker_schemes(), |
| 76 url.scheme())) |
| 77 return true; |
| 78 |
| 79 return false; |
| 80 } |
| 81 |
| 82 void ResetCustomSchemesAndOriginsForTesting() { |
63 g_trustworthy_whitelist.Get().Reset(); | 83 g_trustworthy_whitelist.Get().Reset(); |
64 } | 84 } |
65 | 85 |
66 } // namespace content | 86 } // namespace content |
OLD | NEW |