Update V8 to version 4.7.42.
[chromium-blink-merge.git] / content / common / origin_util.cc
blobd1e5bf0cc13f29289d6096bcb35374724db69ec5
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
3 // found in the LICENSE file.
5 #include "content/public/common/origin_util.h"
7 #include "base/lazy_instance.h"
8 #include "base/stl_util.h"
9 #include "content/public/common/content_client.h"
10 #include "net/base/net_util.h"
11 #include "url/gurl.h"
13 namespace content {
15 namespace {
17 class SchemeAndOriginWhitelist {
18 public:
19 SchemeAndOriginWhitelist() { Reset(); }
20 ~SchemeAndOriginWhitelist() {}
22 void Reset() {
23 GetContentClient()->AddSecureSchemesAndOrigins(&secure_schemes_,
24 &secure_origins_);
25 GetContentClient()->AddServiceWorkerSchemes(&service_worker_schemes_);
28 const std::set<std::string>& secure_schemes() const {
29 return secure_schemes_;
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_;
36 private:
37 std::set<std::string> secure_schemes_;
38 std::set<GURL> secure_origins_;
39 std::set<std::string> service_worker_schemes_;
40 DISALLOW_COPY_AND_ASSIGN(SchemeAndOriginWhitelist);
43 base::LazyInstance<SchemeAndOriginWhitelist>::Leaky g_trustworthy_whitelist =
44 LAZY_INSTANCE_INITIALIZER;
46 } // namespace
48 bool IsOriginSecure(const GURL& url) {
49 if (url.SchemeIsCryptographic() || url.SchemeIsFile())
50 return true;
52 if (url.SchemeIsFileSystem() && url.inner_url() &&
53 IsOriginSecure(*url.inner_url())) {
54 return true;
57 std::string hostname = url.HostNoBrackets();
58 if (net::IsLocalhost(hostname))
59 return true;
61 if (ContainsKey(g_trustworthy_whitelist.Get().secure_schemes(), url.scheme()))
62 return true;
64 if (ContainsKey(g_trustworthy_whitelist.Get().secure_origins(),
65 url.GetOrigin())) {
66 return true;
69 return false;
72 bool OriginCanAccessServiceWorkers(const GURL& url) {
73 if (url.SchemeIsHTTPOrHTTPS() && IsOriginSecure(url))
74 return true;
76 if (ContainsKey(g_trustworthy_whitelist.Get().service_worker_schemes(),
77 url.scheme())) {
78 return true;
81 return false;
84 void ResetSchemesAndOriginsWhitelistForTesting() {
85 g_trustworthy_whitelist.Get().Reset();
88 } // namespace content