OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/html_viewer/html_frame_properties.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <utility> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/pickle.h" | |
12 #include "mojo/common/common_type_converters.h" | |
13 #include "third_party/WebKit/public/platform/WebString.h" | |
14 #include "third_party/WebKit/public/web/WebDocument.h" | |
15 #include "third_party/WebKit/public/web/WebFrame.h" | |
16 #include "third_party/WebKit/public/web/WebSandboxFlags.h" | |
17 #include "url/origin.h" | |
18 #include "url/url_util.h" | |
19 | |
20 namespace html_viewer { | |
21 | |
22 const char kPropertyFrameTreeScope[] = "html-viewer-frame-tree-scope"; | |
23 const char kPropertyFrameOrigin[] = "html-viewer-replicated-frame-origin"; | |
24 const char kPropertyFrameName[] = "html-viewer-replicated-frame-name"; | |
25 const char kPropertyFrameSandboxFlags[] = "html-viewer-sandbox-flags"; | |
26 | |
27 namespace { | |
28 | |
29 mojo::Array<uint8_t> IntToClientPropertyArray(int value) { | |
30 mojo::Array<uint8_t> value_array(sizeof(value)); | |
31 memcpy(&(value_array.front()), &value, sizeof(value)); | |
32 return value_array; | |
33 } | |
34 | |
35 bool IntFromClientPropertyArray(const mojo::Array<uint8_t>& value_array, | |
36 int* value) { | |
37 if (value_array.is_null()) | |
38 return false; | |
39 | |
40 CHECK(value_array.size() == sizeof(int)); | |
41 memcpy(value, &(value_array.front()), sizeof(int)); | |
42 return true; | |
43 } | |
44 | |
45 } // namespace | |
46 | |
47 mojo::Array<uint8_t> FrameNameToClientProperty(const blink::WebString& name) { | |
48 mojo::String mojo_name; | |
49 if (name.isNull()) | |
50 return mojo::Array<uint8_t>(); | |
51 return mojo::Array<uint8_t>::From<std::string>(name.utf8()); | |
52 } | |
53 | |
54 blink::WebString FrameNameFromClientProperty( | |
55 const mojo::Array<uint8_t>& new_data) { | |
56 if (new_data.is_null()) | |
57 return blink::WebString(); | |
58 | |
59 return blink::WebString::fromUTF8(new_data.To<std::string>()); | |
60 } | |
61 | |
62 mojo::Array<uint8_t> FrameTreeScopeToClientProperty( | |
63 blink::WebTreeScopeType scope_type) { | |
64 return IntToClientPropertyArray(static_cast<int>(scope_type)); | |
65 } | |
66 | |
67 bool FrameTreeScopeFromClientProperty(const mojo::Array<uint8_t>& new_data, | |
68 blink::WebTreeScopeType* scope) { | |
69 if (new_data.is_null()) | |
70 return false; | |
71 | |
72 int scope_as_int; | |
73 CHECK(IntFromClientPropertyArray(new_data, &scope_as_int)); | |
74 CHECK(scope_as_int >= 0 && | |
75 scope_as_int <= static_cast<int>(blink::WebTreeScopeType::Last)); | |
76 *scope = static_cast<blink::WebTreeScopeType>(scope_as_int); | |
77 return true; | |
78 } | |
79 | |
80 mojo::Array<uint8_t> FrameSandboxFlagsToClientProperty( | |
81 blink::WebSandboxFlags flags) { | |
82 return IntToClientPropertyArray(static_cast<int>(flags)); | |
83 } | |
84 | |
85 bool FrameSandboxFlagsFromClientProperty(const mojo::Array<uint8_t>& new_data, | |
86 blink::WebSandboxFlags* flags) { | |
87 if (new_data.is_null()) | |
88 return false; | |
89 | |
90 // blink::WebSandboxFlags is a bitmask, so not error checking. | |
91 int flags_as_int; | |
92 CHECK(IntFromClientPropertyArray(new_data, &flags_as_int)); | |
93 *flags = static_cast<blink::WebSandboxFlags>(flags_as_int); | |
94 return true; | |
95 } | |
96 | |
97 url::Origin FrameOrigin(blink::WebFrame* frame) { | |
98 std::string scheme = frame->document().securityOrigin().protocol().utf8(); | |
99 if (!url::IsStandard(scheme.c_str(), | |
100 url::Component(0, static_cast<int>(scheme.length())))) { | |
101 return url::Origin(); | |
102 } | |
103 return frame->document().securityOrigin(); | |
104 } | |
105 | |
106 mojo::Array<uint8_t> FrameOriginToClientProperty(blink::WebFrame* frame) { | |
107 const url::Origin origin = FrameOrigin(frame); | |
108 base::Pickle pickle; | |
109 pickle.WriteBool(origin.unique()); | |
110 pickle.WriteString(origin.scheme()); | |
111 pickle.WriteString(origin.host()); | |
112 pickle.WriteUInt16(origin.port()); | |
113 mojo::Array<uint8_t> origin_array(pickle.size()); | |
114 memcpy(&(origin_array.front()), pickle.data(), pickle.size()); | |
115 return origin_array; | |
116 } | |
117 | |
118 url::Origin FrameOriginFromClientProperty(const mojo::Array<uint8_t>& data) { | |
119 if (data.is_null()) | |
120 return url::Origin(); | |
121 | |
122 CHECK(data.size()); | |
123 CHECK(data.size() < static_cast<size_t>(std::numeric_limits<int>::max())); | |
124 static_assert(sizeof(uint8_t) == sizeof(unsigned char), | |
125 "uint8_t must be the same size as unsigned char"); | |
126 const base::Pickle pickle(reinterpret_cast<const char*>(&(data.front())), | |
127 static_cast<int>(data.size())); | |
128 CHECK(pickle.data()); | |
129 base::PickleIterator iter(pickle); | |
130 bool unique = false; | |
131 std::string scheme; | |
132 std::string host; | |
133 uint16_t port = 0; | |
134 CHECK(iter.ReadBool(&unique)); | |
135 CHECK(iter.ReadString(&scheme)); | |
136 CHECK(iter.ReadString(&host)); | |
137 CHECK(iter.ReadUInt16(&port)); | |
138 const url::Origin result = | |
139 unique ? url::Origin() | |
140 : url::Origin::UnsafelyCreateOriginWithoutNormalization( | |
141 scheme, host, port); | |
142 // If a unique origin was created, but the unique flag wasn't set, then | |
143 // the values provided to 'UnsafelyCreateOriginWithoutNormalization' were | |
144 // invalid; kill the renderer. | |
145 CHECK(!(!unique && result.unique())); | |
146 return result; | |
147 } | |
148 | |
149 void AddToClientPropertiesIfValid( | |
150 const std::string& name, | |
151 mojo::Array<uint8_t> value, | |
152 mojo::Map<mojo::String, mojo::Array<uint8_t>>* client_properties) { | |
153 if (!value.is_null()) | |
154 (*client_properties)[name] = std::move(value); | |
155 } | |
156 | |
157 mojo::Array<uint8_t> GetValueFromClientProperties( | |
158 const std::string& name, | |
159 const mojo::Map<mojo::String, mojo::Array<uint8_t>>& properties) { | |
160 auto iter = properties.find(name); | |
161 return iter == properties.end() ? mojo::Array<uint8_t>() | |
162 : iter.GetValue().Clone(); | |
163 } | |
164 | |
165 } // namespace html_viewer | |
OLD | NEW |