Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / public / platform / WebSecurityOrigin.h
blobb5a7edb8e7d68377dd7c8bddd26de13010b7206b
1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef WebSecurityOrigin_h
32 #define WebSecurityOrigin_h
34 #include "public/platform/WebCommon.h"
35 #include "public/platform/WebString.h"
37 #if INSIDE_BLINK
38 #include "wtf/PassRefPtr.h"
39 #else
40 #include <url/origin.h>
41 #endif
43 namespace blink {
45 class SecurityOrigin;
46 class WebSecurityOriginPrivate;
47 class WebURL;
49 class WebSecurityOrigin {
50 public:
51 ~WebSecurityOrigin() { reset(); }
53 WebSecurityOrigin() : m_private(0) { }
54 WebSecurityOrigin(const WebSecurityOrigin& s) : m_private(0) { assign(s); }
55 WebSecurityOrigin& operator=(const WebSecurityOrigin& s)
57 assign(s);
58 return *this;
61 BLINK_PLATFORM_EXPORT static WebSecurityOrigin createFromDatabaseIdentifier(const WebString& databaseIdentifier);
62 BLINK_PLATFORM_EXPORT static WebSecurityOrigin createFromString(const WebString&);
63 BLINK_PLATFORM_EXPORT static WebSecurityOrigin create(const WebURL&);
64 BLINK_PLATFORM_EXPORT static WebSecurityOrigin createUnique();
66 BLINK_PLATFORM_EXPORT void reset();
67 BLINK_PLATFORM_EXPORT void assign(const WebSecurityOrigin&);
69 bool isNull() const { return !m_private; }
71 BLINK_PLATFORM_EXPORT WebString protocol() const;
72 BLINK_PLATFORM_EXPORT WebString host() const;
73 BLINK_PLATFORM_EXPORT unsigned short port() const;
75 // |port()| will return 0 if the port is the default for an origin. This method
76 // instead returns the effective port, even if it is the default port
77 // (e.g. "http" => 80).
78 BLINK_PLATFORM_EXPORT unsigned short effectivePort() const;
80 // A unique WebSecurityOrigin is the least privileged WebSecurityOrigin.
81 BLINK_PLATFORM_EXPORT bool isUnique() const;
83 // Returns true if this WebSecurityOrigin can script objects in the given
84 // SecurityOrigin. For example, call this function before allowing
85 // script from one security origin to read or write objects from
86 // another SecurityOrigin.
87 BLINK_PLATFORM_EXPORT bool canAccess(const WebSecurityOrigin&) const;
89 // Returns true if this WebSecurityOrigin can read content retrieved from
90 // the given URL. For example, call this function before allowing script
91 // from a given security origin to receive contents from a given URL.
92 BLINK_PLATFORM_EXPORT bool canRequest(const WebURL&) const;
94 // Returns true if the origin loads resources either from the local
95 // machine or over the network from a
96 // cryptographically-authenticated origin, as described in
97 // https://w3c.github.io/webappsec/specs/powerfulfeatures/#is-origin-trustworthy.
98 BLINK_PLATFORM_EXPORT bool isPotentiallyTrustworthy(WebString& errorMessage) const;
100 // Returns a string representation of the WebSecurityOrigin. The empty
101 // WebSecurityOrigin is represented by "null". The representation of a
102 // non-empty WebSecurityOrigin resembles a standard URL.
103 BLINK_PLATFORM_EXPORT WebString toString() const;
105 // Returns a string representation of this WebSecurityOrigin that can
106 // be used as a file. Should be used in storage APIs only.
107 BLINK_PLATFORM_EXPORT WebString databaseIdentifier() const;
109 // Returns true if this WebSecurityOrigin can access usernames and
110 // passwords stored in password manager.
111 BLINK_PLATFORM_EXPORT bool canAccessPasswordManager() const;
113 // Allows this WebSecurityOrigin access to local resources.
114 BLINK_PLATFORM_EXPORT void grantLoadLocalResources() const;
116 #if INSIDE_BLINK
117 BLINK_PLATFORM_EXPORT WebSecurityOrigin(const WTF::PassRefPtr<SecurityOrigin>&);
118 BLINK_PLATFORM_EXPORT WebSecurityOrigin& operator=(const WTF::PassRefPtr<SecurityOrigin>&);
119 BLINK_PLATFORM_EXPORT operator WTF::PassRefPtr<SecurityOrigin>() const;
120 BLINK_PLATFORM_EXPORT SecurityOrigin* get() const;
121 #else
122 // TODO(mkwst): A number of properties don't survive a round-trip ('document.domain', for instance).
123 // We'll need to fix that for OOPI-enabled embedders: https://crbug.com/490074.
124 operator url::Origin() const
126 return isUnique()
127 ? url::Origin()
128 : url::Origin::UnsafelyCreateOriginWithoutNormalization(protocol().utf8(), host().utf8(), effectivePort());
131 WebSecurityOrigin(const url::Origin& origin)
132 : m_private(0)
134 if (origin.unique()) {
135 assign(WebSecurityOrigin::createUnique());
136 return;
139 // TODO(mkwst): This might open up issues by double-canonicalizing the host.
140 assign(WebSecurityOrigin::createFromTuple(WebString::fromUTF8(origin.scheme()),
141 WebString::fromUTF8(origin.host()),
142 origin.port()));
144 #endif
146 private:
147 // Present only to facilitate conversion from 'url::Origin'; this constructor shouldn't be used anywhere else.
148 BLINK_PLATFORM_EXPORT static WebSecurityOrigin createFromTuple(const WebString& protocol, const WebString& host, int port);
150 void assign(WebSecurityOriginPrivate*);
151 WebSecurityOriginPrivate* m_private;
154 } // namespace blink
156 #endif