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