[Author: andreip]
[google-gears.git] / gears / base / common / security_model.h
blobaea30f67420c8bcbe6ac1daed51e8573e7be4947
1 // Copyright 2006, Google Inc.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
11 // 3. Neither the name of Google Inc. nor the names of its contributors may be
12 // used to endorse or promote products derived from this software without
13 // specific prior written permission.
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef GEARS_BASE_COMMON_SECURITY_MODEL_H__
27 #define GEARS_BASE_COMMON_SECURITY_MODEL_H__
29 #include <assert.h>
30 #include "gears/base/common/string16.h"
32 // Value to use when the source domain cannot be determined.
33 extern const char16* kUnknownDomain;
34 extern const char* kUnknownDomainAscii;
36 //------------------------------------------------------------------------------
37 // Class that represents the origin of a URL. The origin includes the scheme,
38 // host, and port.
39 //------------------------------------------------------------------------------
40 class SecurityOrigin {
41 public:
42 SecurityOrigin() : initialized_(false), port_(0) {}
44 // Extacts the scheme, host, and port of the 'full_url'.
45 // Returns true if successful. Only "file","http", and "https"
46 // urls are supported. In the case of a file url, the host will
47 // be set to kUnknownDomain.
48 bool InitFromUrl(const char16 *full_url);
50 // A url that contains the information representative of the security
51 // origin and nothing more. The path is always empty. The port number is
52 // not included for for the default port case.
53 // Ex. http://host:99, http://host
54 const std::string16 &url() const
55 { assert(initialized_); return url_; }
57 // The full url the origin was initialized with.
58 // TODO(michaeln): Remove this convenience. Callers that need it should
59 // pass it around rather than having it piggyback on this class.
60 const std::string16 &full_url() const
61 { assert(initialized_); return full_url_; }
63 const std::string16 &scheme() const
64 { assert(initialized_); return scheme_; }
66 const std::string16 &host() const
67 { assert(initialized_); return host_; }
69 int port() const
70 { assert(initialized_); return port_; }
72 const std::string16 &port_string() const
73 { assert(initialized_); return port_string_; }
75 // Returns true if 'other' and 'this' represent the same origin. If
76 // either origin is not initalized, returns false.
77 bool IsSameOrigin(const SecurityOrigin &other) const {
78 assert(initialized_);
79 assert(other.initialized_);
80 return (port_ == other.port_) &&
81 (scheme_ == other.scheme_) &&
82 (host_ == other.host_);
85 // Returns true if 'full_url' is form the same origin as 'this'
86 bool IsSameOriginAsUrl(const char16 *full_url) const {
87 SecurityOrigin origin;
88 return origin.InitFromUrl(full_url) &&
89 IsSameOrigin(origin);
92 private:
93 friend bool TestSecurityModel();
95 bool Init(const char16 *full_url, const char16 *scheme,
96 const char16 *host, int port);
98 bool initialized_;
99 std::string16 url_;
100 std::string16 full_url_;
101 std::string16 scheme_;
102 std::string16 host_;
103 int port_;
104 std::string16 port_string_;
107 #endif // GEARS_BASE_COMMON_SECURITY_MODEL_H__