OLD | NEW |
(Empty) | |
| 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/proxy/ppb_url_util_proxy.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "ppapi/c/dev/ppb_url_util_dev.h" |
| 9 #include "ppapi/c/dev/ppb_var_deprecated.h" |
| 10 #include "ppapi/c/ppb_core.h" |
| 11 #include "ppapi/proxy/plugin_dispatcher.h" |
| 12 #include "ppapi/proxy/ppapi_messages.h" |
| 13 #include "ppapi/shared_impl/url_util_impl.h" |
| 14 |
| 15 namespace pp { |
| 16 namespace proxy { |
| 17 |
| 18 using pp::shared_impl::URLUtilImpl; |
| 19 |
| 20 namespace { |
| 21 |
| 22 URLUtilImpl::VarFromUtf8 GetVarFromUtf8() { |
| 23 const PPB_Var_Deprecated* var_deprecated = |
| 24 static_cast<const PPB_Var_Deprecated*>( |
| 25 PluginDispatcher::GetInterfaceFromDispatcher( |
| 26 PPB_VAR_DEPRECATED_INTERFACE)); |
| 27 return var_deprecated->VarFromUtf8; |
| 28 } |
| 29 |
| 30 const std::string* GetStringFromVar(PP_Var var) { |
| 31 return PluginVarTracker::GetInstance()->GetExistingString(var); |
| 32 } |
| 33 |
| 34 PP_Var Canonicalize(PP_Var url, |
| 35 PP_URLComponents_Dev* components) { |
| 36 return URLUtilImpl::Canonicalize(&GetStringFromVar, GetVarFromUtf8(), |
| 37 0, url, components); |
| 38 } |
| 39 |
| 40 // Helper function for the functions below that optionally take a components |
| 41 // structure. It's annoying to serialze the large PP_URLComponents structure |
| 42 // and this data often isn't needed. |
| 43 // |
| 44 // To avoid this, we instead just parse the result again in the plugin, which |
| 45 // this function does if the given URL is valid and the components are |
| 46 // non-NULL. The URL var will be returned. |
| 47 PP_Var ConvertComponentsAndReturnURL(PP_Var url, |
| 48 PP_URLComponents_Dev* components) { |
| 49 if (!components) |
| 50 return url; // Common case - plugin doesn't care about parsing. |
| 51 |
| 52 const std::string* url_string = GetStringFromVar(url); |
| 53 if (!url_string) |
| 54 return url; |
| 55 |
| 56 PP_Var result = Canonicalize(url, components); |
| 57 PluginVarTracker::GetInstance()->Release(url); |
| 58 return result; |
| 59 } |
| 60 |
| 61 PP_Var ResolveRelativeToURL(PP_Var base_url, |
| 62 PP_Var relative, |
| 63 PP_URLComponents_Dev* components) { |
| 64 return URLUtilImpl::ResolveRelativeToURL(&GetStringFromVar, GetVarFromUtf8(), |
| 65 0, base_url, relative, components); |
| 66 } |
| 67 |
| 68 PP_Var ResolveRelativeToDocument(PP_Instance instance, |
| 69 PP_Var relative_string, |
| 70 PP_URLComponents_Dev* components) { |
| 71 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
| 72 if (!dispatcher) |
| 73 return PP_MakeNull(); |
| 74 |
| 75 ReceiveSerializedVarReturnValue result; |
| 76 dispatcher->Send(new PpapiHostMsg_PPBURLUtil_ResolveRelativeToDocument( |
| 77 INTERFACE_ID_PPB_URL_UTIL, instance, |
| 78 SerializedVarSendInput(dispatcher, relative_string), |
| 79 &result)); |
| 80 return ConvertComponentsAndReturnURL(result.Return(dispatcher), components); |
| 81 } |
| 82 |
| 83 PP_Bool IsSameSecurityOrigin(PP_Var url_a, PP_Var url_b) { |
| 84 return URLUtilImpl::IsSameSecurityOrigin(&GetStringFromVar, url_a, url_b); |
| 85 } |
| 86 |
| 87 PP_Bool DocumentCanRequest(PP_Instance instance, PP_Var url) { |
| 88 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
| 89 if (!dispatcher) |
| 90 return PP_FALSE; |
| 91 |
| 92 PP_Bool result = PP_FALSE; |
| 93 dispatcher->Send(new PpapiHostMsg_PPBURLUtil_DocumentCanRequest( |
| 94 INTERFACE_ID_PPB_URL_UTIL, instance, |
| 95 SerializedVarSendInput(dispatcher, url), |
| 96 &result)); |
| 97 return result; |
| 98 } |
| 99 |
| 100 PP_Bool DocumentCanAccessDocument(PP_Instance active, PP_Instance target) { |
| 101 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(active); |
| 102 if (!dispatcher) |
| 103 return PP_FALSE; |
| 104 |
| 105 PP_Bool result = PP_FALSE; |
| 106 dispatcher->Send(new PpapiHostMsg_PPBURLUtil_DocumentCanAccessDocument( |
| 107 INTERFACE_ID_PPB_URL_UTIL, active, target, &result)); |
| 108 return result; |
| 109 } |
| 110 |
| 111 PP_Var GetDocumentURL(PP_Instance instance, |
| 112 struct PP_URLComponents_Dev* components) { |
| 113 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
| 114 if (!dispatcher) |
| 115 return PP_MakeNull(); |
| 116 |
| 117 ReceiveSerializedVarReturnValue result; |
| 118 dispatcher->Send(new PpapiHostMsg_PPBURLUtil_GetDocumentURL( |
| 119 INTERFACE_ID_PPB_URL_UTIL, instance, &result)); |
| 120 return ConvertComponentsAndReturnURL(result.Return(dispatcher), components); |
| 121 } |
| 122 |
| 123 const PPB_URLUtil_Dev url_util_interface = { |
| 124 &Canonicalize, |
| 125 &ResolveRelativeToURL, |
| 126 &ResolveRelativeToDocument, |
| 127 &IsSameSecurityOrigin, |
| 128 &DocumentCanRequest, |
| 129 &DocumentCanAccessDocument, |
| 130 &GetDocumentURL |
| 131 }; |
| 132 |
| 133 InterfaceProxy* CreateURLUtilProxy(Dispatcher* dispatcher, |
| 134 const void* target_interface) { |
| 135 return new PPB_URLUtil_Proxy(dispatcher, target_interface); |
| 136 } |
| 137 |
| 138 } // namespace |
| 139 |
| 140 PPB_URLUtil_Proxy::PPB_URLUtil_Proxy(Dispatcher* dispatcher, |
| 141 const void* target_interface) |
| 142 : InterfaceProxy(dispatcher, target_interface) { |
| 143 } |
| 144 |
| 145 PPB_URLUtil_Proxy::~PPB_URLUtil_Proxy() { |
| 146 } |
| 147 |
| 148 // static |
| 149 const InterfaceProxy::Info* PPB_URLUtil_Proxy::GetInfo() { |
| 150 static const Info info = { |
| 151 &url_util_interface, |
| 152 PPB_URLUTIL_DEV_INTERFACE, |
| 153 INTERFACE_ID_PPB_URL_UTIL, |
| 154 false, |
| 155 &CreateURLUtilProxy, |
| 156 }; |
| 157 return &info; |
| 158 } |
| 159 |
| 160 bool PPB_URLUtil_Proxy::OnMessageReceived(const IPC::Message& msg) { |
| 161 bool handled = true; |
| 162 IPC_BEGIN_MESSAGE_MAP(PPB_URLUtil_Proxy, msg) |
| 163 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBURLUtil_ResolveRelativeToDocument, |
| 164 OnMsgResolveRelativeToDocument) |
| 165 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBURLUtil_DocumentCanRequest, |
| 166 OnMsgDocumentCanRequest) |
| 167 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBURLUtil_DocumentCanAccessDocument, |
| 168 OnMsgDocumentCanAccessDocument) |
| 169 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBURLUtil_GetDocumentURL, |
| 170 OnMsgGetDocumentURL) |
| 171 IPC_MESSAGE_UNHANDLED(handled = false) |
| 172 IPC_END_MESSAGE_MAP() |
| 173 return handled; |
| 174 } |
| 175 |
| 176 void PPB_URLUtil_Proxy::OnMsgResolveRelativeToDocument( |
| 177 PP_Instance instance, |
| 178 SerializedVarReceiveInput relative, |
| 179 SerializedVarReturnValue result) { |
| 180 result.Return(dispatcher(), |
| 181 ppb_url_util_target()->ResolveRelativeToDocument( |
| 182 instance, relative.Get(dispatcher()), NULL)); |
| 183 } |
| 184 |
| 185 void PPB_URLUtil_Proxy::OnMsgDocumentCanRequest(PP_Instance instance, |
| 186 SerializedVarReceiveInput url, |
| 187 PP_Bool* result) { |
| 188 *result = ppb_url_util_target()->DocumentCanRequest(instance, |
| 189 url.Get(dispatcher())); |
| 190 } |
| 191 |
| 192 void PPB_URLUtil_Proxy::OnMsgDocumentCanAccessDocument(PP_Instance active, |
| 193 PP_Instance target, |
| 194 PP_Bool* result) { |
| 195 *result = ppb_url_util_target()->DocumentCanAccessDocument( |
| 196 active, target); |
| 197 } |
| 198 |
| 199 void PPB_URLUtil_Proxy::OnMsgGetDocumentURL(PP_Instance instance, |
| 200 SerializedVarReturnValue result) { |
| 201 result.Return(dispatcher(), |
| 202 ppb_url_util_target()->GetDocumentURL(instance, NULL)); |
| 203 } |
| 204 |
| 205 } // namespace proxy |
| 206 } // namespace pp |
| 207 |
OLD | NEW |