1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "webkit/glue/plugins/pepper_url_util.h"
#include "googleurl/src/gurl.h"
#include "ppapi/c/dev/ppb_url_util_dev.h"
#include "third_party/WebKit/WebKit/chromium/public/WebDocument.h"
#include "third_party/WebKit/WebKit/chromium/public/WebElement.h"
#include "third_party/WebKit/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/WebKit/chromium/public/WebNode.h"
#include "third_party/WebKit/WebKit/chromium/public/WebPluginContainer.h"
#include "third_party/WebKit/WebKit/chromium/public/WebSecurityOrigin.h"
#include "third_party/WebKit/WebKit/chromium/public/WebURL.h"
#include "webkit/glue/plugins/pepper_common.h"
#include "webkit/glue/plugins/pepper_plugin_instance.h"
#include "webkit/glue/plugins/pepper_string.h"
#include "webkit/glue/plugins/pepper_var.h"
namespace pepper {
namespace {
void ConvertComponent(const url_parse::Component& input,
PP_UrlComponent_Dev* output) {
output->begin = input.begin;
output->len = input.len;
}
// Output can be NULL to specify "do nothing." This rule is followed by all the
// url util functions, so we implement it once here.
void ConvertComponents(const url_parse::Parsed& input,
PP_UrlComponents_Dev* output) {
if (!output)
return;
ConvertComponent(input.scheme, &output->scheme);
ConvertComponent(input.username, &output->username);
ConvertComponent(input.password, &output->password);
ConvertComponent(input.host, &output->host);
ConvertComponent(input.port, &output->port);
ConvertComponent(input.path, &output->path);
ConvertComponent(input.query, &output->query);
ConvertComponent(input.ref, &output->ref);
}
// Used for returning the given GURL from a PPAPI function, with an optional
// out param indicating the components.
PP_Var GenerateUrlReturn(PluginModule* module, const GURL& url,
PP_UrlComponents_Dev* components) {
if (!url.is_valid())
return PP_MakeNull();
ConvertComponents(url.parsed_for_possibly_invalid_spec(), components);
return StringVar::StringToPPVar(module, url.possibly_invalid_spec());
}
// Sets |*security_origin| to be the WebKit security origin associated with the
// document containing the given plugin instance. On success, returns true. If
// the instance is invalid, returns false and |*security_origin| will be
// unchanged.
bool SecurityOriginForInstance(PP_Instance instance_id,
WebKit::WebSecurityOrigin* security_origin) {
PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id);
if (!instance)
return false;
WebKit::WebElement plugin_element = instance->container()->element();
WebKit::WebFrame* plugin_frame = plugin_element.document().frame();
if (!plugin_frame)
return false;
*security_origin = plugin_frame->securityOrigin();
return true;
}
PP_Var Canonicalize(PP_Var url, PP_UrlComponents_Dev* components) {
scoped_refptr<StringVar> url_string(StringVar::FromPPVar(url));
if (!url_string)
return PP_MakeNull();
return GenerateUrlReturn(url_string->module(),
GURL(url_string->value()), components);
}
PP_Var ResolveRelativeToUrl(PP_Var base_url,
PP_Var relative,
PP_UrlComponents_Dev* components) {
scoped_refptr<StringVar> base_url_string(StringVar::FromPPVar(base_url));
scoped_refptr<StringVar> relative_string(StringVar::FromPPVar(relative));
if (!base_url_string || !relative_string)
return PP_MakeNull();
GURL base_gurl(base_url_string->value());
if (!base_gurl.is_valid())
return PP_MakeNull();
return GenerateUrlReturn(base_url_string->module(),
base_gurl.Resolve(relative_string->value()),
components);
}
PP_Var ResolveRelativeToDocument(PP_Instance instance_id,
PP_Var relative,
PP_UrlComponents_Dev* components) {
PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id);
if (!instance)
return PP_MakeNull();
scoped_refptr<StringVar> relative_string(StringVar::FromPPVar(relative));
if (!relative_string)
return PP_MakeNull();
WebKit::WebElement plugin_element = instance->container()->element();
GURL document_url = plugin_element.document().baseURL();
return GenerateUrlReturn(instance->module(),
document_url.Resolve(relative_string->value()),
components);
}
PP_Bool IsSameSecurityOrigin(PP_Var url_a, PP_Var url_b) {
scoped_refptr<StringVar> url_a_string(StringVar::FromPPVar(url_a));
scoped_refptr<StringVar> url_b_string(StringVar::FromPPVar(url_b));
if (!url_a_string || !url_b_string)
return PP_FALSE;
GURL gurl_a(url_a_string->value());
GURL gurl_b(url_b_string->value());
if (!gurl_a.is_valid() || !gurl_b.is_valid())
return PP_FALSE;
return BoolToPPBool(gurl_a.GetOrigin() == gurl_b.GetOrigin());
}
PP_Bool DocumentCanRequest(PP_Instance instance, PP_Var url) {
scoped_refptr<StringVar> url_string(StringVar::FromPPVar(url));
if (!url_string)
return PP_FALSE;
WebKit::WebSecurityOrigin security_origin;
if (!SecurityOriginForInstance(instance, &security_origin))
return PP_FALSE;
GURL gurl(url_string->value());
if (!gurl.is_valid())
return PP_FALSE;
return BoolToPPBool(security_origin.canRequest(gurl));
}
PP_Bool DocumentCanAccessDocument(PP_Instance active, PP_Instance target) {
WebKit::WebSecurityOrigin active_origin;
if (!SecurityOriginForInstance(active, &active_origin))
return PP_FALSE;
WebKit::WebSecurityOrigin target_origin;
if (!SecurityOriginForInstance(active, &target_origin))
return PP_FALSE;
return BoolToPPBool(active_origin.canAccess(target_origin));
}
} // namespace
const PPB_UrlUtil_Dev ppb_url_util = {
&Canonicalize,
&ResolveRelativeToUrl,
&ResolveRelativeToDocument,
&IsSameSecurityOrigin,
&DocumentCanRequest,
&DocumentCanAccessDocument
};
// static
const PPB_UrlUtil_Dev* UrlUtil::GetInterface() {
return &ppb_url_util;
}
} // namespace pepper
|