summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/ppb_url_util_proxy.cc
blob: b59ef3c653e83d8cfcab9116677d03a924d79535 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright (c) 2011 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 "ppapi/proxy/ppb_url_util_proxy.h"

#include "base/basictypes.h"
#include "ppapi/c/dev/ppb_url_util_dev.h"
#include "ppapi/c/dev/ppb_var_deprecated.h"
#include "ppapi/c/ppb_core.h"
#include "ppapi/proxy/plugin_dispatcher.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/shared_impl/url_util_impl.h"

namespace pp {
namespace proxy {

using ppapi::URLUtilImpl;

namespace {

URLUtilImpl::VarFromUtf8 GetVarFromUtf8() {
  const PPB_Var_Deprecated* var_deprecated =
      static_cast<const PPB_Var_Deprecated*>(
          PluginDispatcher::GetInterfaceFromDispatcher(
              PPB_VAR_DEPRECATED_INTERFACE));
  return var_deprecated->VarFromUtf8;
}

const std::string* GetStringFromVar(PP_Var var) {
  return PluginVarTracker::GetInstance()->GetExistingString(var);
}

PP_Var Canonicalize(PP_Var url,
                    PP_URLComponents_Dev* components) {
  return URLUtilImpl::Canonicalize(&GetStringFromVar, GetVarFromUtf8(),
                                   0, url, components);
}

// Helper function for the functions below that optionally take a components
// structure. It's annoying to serialze the large PP_URLComponents structure
// and this data often isn't needed.
//
// To avoid this, we instead just parse the result again in the plugin, which
// this function does if the given URL is valid and the components are
// non-NULL. The URL var will be returned.
PP_Var ConvertComponentsAndReturnURL(PP_Var url,
                                     PP_URLComponents_Dev* components) {
  if (!components)
    return url;  // Common case - plugin doesn't care about parsing.

  const std::string* url_string = GetStringFromVar(url);
  if (!url_string)
    return url;

  PP_Var result = Canonicalize(url, components);
  PluginVarTracker::GetInstance()->Release(url);
  return result;
}

PP_Var ResolveRelativeToURL(PP_Var base_url,
                            PP_Var relative,
                            PP_URLComponents_Dev* components) {
  return URLUtilImpl::ResolveRelativeToURL(&GetStringFromVar, GetVarFromUtf8(),
                                           0, base_url, relative, components);
}

PP_Var ResolveRelativeToDocument(PP_Instance instance,
                                 PP_Var relative_string,
                                 PP_URLComponents_Dev* components) {
  PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
  if (!dispatcher)
    return PP_MakeNull();

  ReceiveSerializedVarReturnValue result;
  dispatcher->Send(new PpapiHostMsg_PPBURLUtil_ResolveRelativeToDocument(
      INTERFACE_ID_PPB_URL_UTIL, instance,
      SerializedVarSendInput(dispatcher, relative_string),
      &result));
  return ConvertComponentsAndReturnURL(result.Return(dispatcher), components);
}

PP_Bool IsSameSecurityOrigin(PP_Var url_a, PP_Var url_b) {
  return URLUtilImpl::IsSameSecurityOrigin(&GetStringFromVar, url_a, url_b);
}

PP_Bool DocumentCanRequest(PP_Instance instance, PP_Var url) {
  PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
  if (!dispatcher)
    return PP_FALSE;

  PP_Bool result = PP_FALSE;
  dispatcher->Send(new PpapiHostMsg_PPBURLUtil_DocumentCanRequest(
      INTERFACE_ID_PPB_URL_UTIL, instance,
      SerializedVarSendInput(dispatcher, url),
      &result));
  return result;
}

PP_Bool DocumentCanAccessDocument(PP_Instance active, PP_Instance target) {
  PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(active);
  if (!dispatcher)
    return PP_FALSE;

  PP_Bool result = PP_FALSE;
  dispatcher->Send(new PpapiHostMsg_PPBURLUtil_DocumentCanAccessDocument(
      INTERFACE_ID_PPB_URL_UTIL, active, target, &result));
  return result;
}

PP_Var GetDocumentURL(PP_Instance instance,
                      struct PP_URLComponents_Dev* components) {
  PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
  if (!dispatcher)
    return PP_MakeNull();

  ReceiveSerializedVarReturnValue result;
  dispatcher->Send(new PpapiHostMsg_PPBURLUtil_GetDocumentURL(
      INTERFACE_ID_PPB_URL_UTIL, instance, &result));
  return ConvertComponentsAndReturnURL(result.Return(dispatcher), components);
}

PP_Var GetPluginInstanceURL(PP_Instance instance,
                            struct PP_URLComponents_Dev* components) {
  PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
  if (!dispatcher)
    return PP_MakeNull();

  ReceiveSerializedVarReturnValue result;
  dispatcher->Send(new PpapiHostMsg_PPBURLUtil_GetPluginInstanceURL(
      INTERFACE_ID_PPB_URL_UTIL, instance, &result));
  return ConvertComponentsAndReturnURL(result.Return(dispatcher), components);
}

const PPB_URLUtil_Dev url_util_interface = {
  &Canonicalize,
  &ResolveRelativeToURL,
  &ResolveRelativeToDocument,
  &IsSameSecurityOrigin,
  &DocumentCanRequest,
  &DocumentCanAccessDocument,
  &GetDocumentURL,
  &GetPluginInstanceURL
};

InterfaceProxy* CreateURLUtilProxy(Dispatcher* dispatcher,
                                   const void* target_interface) {
  return new PPB_URLUtil_Proxy(dispatcher, target_interface);
}

}  // namespace

PPB_URLUtil_Proxy::PPB_URLUtil_Proxy(Dispatcher* dispatcher,
                                     const void* target_interface)
    : InterfaceProxy(dispatcher, target_interface) {
}

PPB_URLUtil_Proxy::~PPB_URLUtil_Proxy() {
}

// static
const InterfaceProxy::Info* PPB_URLUtil_Proxy::GetInfo() {
  static const Info info = {
    &url_util_interface,
    PPB_URLUTIL_DEV_INTERFACE,
    INTERFACE_ID_PPB_URL_UTIL,
    false,
    &CreateURLUtilProxy,
  };
  return &info;
}

bool PPB_URLUtil_Proxy::OnMessageReceived(const IPC::Message& msg) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(PPB_URLUtil_Proxy, msg)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBURLUtil_ResolveRelativeToDocument,
                        OnMsgResolveRelativeToDocument)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBURLUtil_DocumentCanRequest,
                        OnMsgDocumentCanRequest)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBURLUtil_DocumentCanAccessDocument,
                        OnMsgDocumentCanAccessDocument)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBURLUtil_GetDocumentURL,
                        OnMsgGetDocumentURL)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBURLUtil_GetPluginInstanceURL,
                        OnMsgGetPluginInstanceURL)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

void PPB_URLUtil_Proxy::OnMsgResolveRelativeToDocument(
    PP_Instance instance,
    SerializedVarReceiveInput relative,
    SerializedVarReturnValue result) {
  result.Return(dispatcher(),
                ppb_url_util_target()->ResolveRelativeToDocument(
                    instance, relative.Get(dispatcher()), NULL));
}

void PPB_URLUtil_Proxy::OnMsgDocumentCanRequest(PP_Instance instance,
                                                SerializedVarReceiveInput url,
                                                PP_Bool* result) {
  *result = ppb_url_util_target()->DocumentCanRequest(instance,
                                                      url.Get(dispatcher()));
}

void PPB_URLUtil_Proxy::OnMsgDocumentCanAccessDocument(PP_Instance active,
                                                       PP_Instance target,
                                                       PP_Bool* result) {
  *result = ppb_url_util_target()->DocumentCanAccessDocument(
      active, target);
}

void PPB_URLUtil_Proxy::OnMsgGetDocumentURL(PP_Instance instance,
                                            SerializedVarReturnValue result) {
  result.Return(dispatcher(),
                ppb_url_util_target()->GetDocumentURL(instance, NULL));
}

void PPB_URLUtil_Proxy::OnMsgGetPluginInstanceURL(
    PP_Instance instance, SerializedVarReturnValue result) {
  result.Return(dispatcher(),
                ppb_url_util_target()->GetPluginInstanceURL(instance, NULL));
}

}  // namespace proxy
}  // namespace pp