1 // Copyright (c) 2011 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 "ppapi/cpp/dev/url_util_dev.h"
6
7 #include "ppapi/cpp/instance_handle.h"
8 #include "ppapi/cpp/module_impl.h"
9
10 namespace pp {
11
12 namespace {
13
interface_name()14 template <> const char* interface_name<PPB_URLUtil_Dev_0_6>() {
15 return PPB_URLUTIL_DEV_INTERFACE_0_6;
16 }
17
interface_name()18 template <> const char* interface_name<PPB_URLUtil_Dev_0_7>() {
19 return PPB_URLUTIL_DEV_INTERFACE_0_7;
20 }
21
22 } // namespace
23
24 // static
Get()25 const URLUtil_Dev* URLUtil_Dev::Get() {
26 static URLUtil_Dev util;
27 static bool tried_to_init = false;
28 static bool interface_available = false;
29
30 if (!tried_to_init) {
31 tried_to_init = true;
32 if (has_interface<PPB_URLUtil_Dev_0_7>() ||
33 has_interface<PPB_URLUtil_Dev_0_6>())
34 interface_available = true;
35 }
36
37 if (!interface_available)
38 return NULL;
39 return &util;
40 }
41
Canonicalize(const Var & url,PP_URLComponents_Dev * components) const42 Var URLUtil_Dev::Canonicalize(const Var& url,
43 PP_URLComponents_Dev* components) const {
44 if (has_interface<PPB_URLUtil_Dev_0_7>()) {
45 return Var(PASS_REF,
46 get_interface<PPB_URLUtil_Dev_0_7>()->Canonicalize(url.pp_var(),
47 components));
48 }
49 if (has_interface<PPB_URLUtil_Dev_0_6>()) {
50 return Var(PASS_REF,
51 get_interface<PPB_URLUtil_Dev_0_6>()->Canonicalize(url.pp_var(),
52 components));
53 }
54 return Var();
55 }
56
ResolveRelativeToURL(const Var & base_url,const Var & relative_string,PP_URLComponents_Dev * components) const57 Var URLUtil_Dev::ResolveRelativeToURL(const Var& base_url,
58 const Var& relative_string,
59 PP_URLComponents_Dev* components) const {
60 if (has_interface<PPB_URLUtil_Dev_0_7>()) {
61 return Var(PASS_REF,
62 get_interface<PPB_URLUtil_Dev_0_7>()->ResolveRelativeToURL(
63 base_url.pp_var(),
64 relative_string.pp_var(),
65 components));
66 }
67 if (has_interface<PPB_URLUtil_Dev_0_6>()) {
68 return Var(PASS_REF,
69 get_interface<PPB_URLUtil_Dev_0_6>()->ResolveRelativeToURL(
70 base_url.pp_var(),
71 relative_string.pp_var(),
72 components));
73 }
74 return Var();
75 }
76
ResolveRelativeToDocument(const InstanceHandle & instance,const Var & relative_string,PP_URLComponents_Dev * components) const77 Var URLUtil_Dev::ResolveRelativeToDocument(
78 const InstanceHandle& instance,
79 const Var& relative_string,
80 PP_URLComponents_Dev* components) const {
81 if (has_interface<PPB_URLUtil_Dev_0_7>()) {
82 return Var(PASS_REF,
83 get_interface<PPB_URLUtil_Dev_0_7>()->ResolveRelativeToDocument(
84 instance.pp_instance(),
85 relative_string.pp_var(),
86 components));
87 }
88 if (has_interface<PPB_URLUtil_Dev_0_6>()) {
89 return Var(PASS_REF,
90 get_interface<PPB_URLUtil_Dev_0_6>()->ResolveRelativeToDocument(
91 instance.pp_instance(),
92 relative_string.pp_var(),
93 components));
94 }
95 return Var();
96 }
97
IsSameSecurityOrigin(const Var & url_a,const Var & url_b) const98 bool URLUtil_Dev::IsSameSecurityOrigin(const Var& url_a,
99 const Var& url_b) const {
100 if (has_interface<PPB_URLUtil_Dev_0_7>()) {
101 return PP_ToBool(
102 get_interface<PPB_URLUtil_Dev_0_7>()->IsSameSecurityOrigin(
103 url_a.pp_var(),
104 url_b.pp_var()));
105 }
106 if (has_interface<PPB_URLUtil_Dev_0_6>()) {
107 return PP_ToBool(
108 get_interface<PPB_URLUtil_Dev_0_6>()->IsSameSecurityOrigin(
109 url_a.pp_var(),
110 url_b.pp_var()));
111 }
112 return false;
113 }
114
DocumentCanRequest(const InstanceHandle & instance,const Var & url) const115 bool URLUtil_Dev::DocumentCanRequest(const InstanceHandle& instance,
116 const Var& url) const {
117 if (has_interface<PPB_URLUtil_Dev_0_7>()) {
118 return PP_ToBool(
119 get_interface<PPB_URLUtil_Dev_0_7>()->DocumentCanRequest(
120 instance.pp_instance(),
121 url.pp_var()));
122 }
123 if (has_interface<PPB_URLUtil_Dev_0_6>()) {
124 return PP_ToBool(
125 get_interface<PPB_URLUtil_Dev_0_6>()->DocumentCanRequest(
126 instance.pp_instance(),
127 url.pp_var()));
128 }
129 return false;
130 }
131
DocumentCanAccessDocument(const InstanceHandle & active,const InstanceHandle & target) const132 bool URLUtil_Dev::DocumentCanAccessDocument(
133 const InstanceHandle& active,
134 const InstanceHandle& target) const {
135 if (has_interface<PPB_URLUtil_Dev_0_7>()) {
136 return PP_ToBool(
137 get_interface<PPB_URLUtil_Dev_0_7>()->DocumentCanAccessDocument(
138 active.pp_instance(),
139 target.pp_instance()));
140 }
141 if (has_interface<PPB_URLUtil_Dev_0_6>()) {
142 return PP_ToBool(
143 get_interface<PPB_URLUtil_Dev_0_6>()->DocumentCanAccessDocument(
144 active.pp_instance(),
145 target.pp_instance()));
146 }
147 return false;
148 }
149
GetDocumentURL(const InstanceHandle & instance,PP_URLComponents_Dev * components) const150 Var URLUtil_Dev::GetDocumentURL(const InstanceHandle& instance,
151 PP_URLComponents_Dev* components) const {
152 if (has_interface<PPB_URLUtil_Dev_0_7>()) {
153 return Var(PASS_REF,
154 get_interface<PPB_URLUtil_Dev_0_7>()->GetDocumentURL(
155 instance.pp_instance(),
156 components));
157 }
158 if (has_interface<PPB_URLUtil_Dev_0_6>()) {
159 return Var(PASS_REF,
160 get_interface<PPB_URLUtil_Dev_0_6>()->GetDocumentURL(
161 instance.pp_instance(),
162 components));
163 }
164 return Var();
165 }
166
GetPluginInstanceURL(const InstanceHandle & instance,PP_URLComponents_Dev * components) const167 Var URLUtil_Dev::GetPluginInstanceURL(const InstanceHandle& instance,
168 PP_URLComponents_Dev* components) const {
169 if (has_interface<PPB_URLUtil_Dev_0_7>()) {
170 return Var(PASS_REF,
171 get_interface<PPB_URLUtil_Dev_0_7>()->GetPluginInstanceURL(
172 instance.pp_instance(),
173 components));
174 }
175 if (has_interface<PPB_URLUtil_Dev_0_6>()) {
176 return Var(PASS_REF,
177 get_interface<PPB_URLUtil_Dev_0_6>()->GetPluginInstanceURL(
178 instance.pp_instance(),
179 components));
180 }
181 return Var();
182 }
183
GetPluginReferrerURL(const InstanceHandle & instance,PP_URLComponents_Dev * components) const184 Var URLUtil_Dev::GetPluginReferrerURL(const InstanceHandle& instance,
185 PP_URLComponents_Dev* components) const {
186 if (has_interface<PPB_URLUtil_Dev_0_7>()) {
187 return Var(PASS_REF,
188 get_interface<PPB_URLUtil_Dev_0_7>()->GetPluginReferrerURL(
189 instance.pp_instance(),
190 components));
191 }
192 return Var();
193 }
194
195 } // namespace pp
196