Home | History | Annotate | Download | only in proxy
      1 // Copyright (c) 2013 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/network_proxy_resource.h"
      6 
      7 #include "base/bind.h"
      8 #include "ppapi/proxy/dispatch_reply_message.h"
      9 #include "ppapi/proxy/ppapi_messages.h"
     10 #include "ppapi/shared_impl/tracked_callback.h"
     11 #include "ppapi/shared_impl/var.h"
     12 
     13 namespace ppapi {
     14 namespace proxy {
     15 
     16 NetworkProxyResource::NetworkProxyResource(Connection connection,
     17                                            PP_Instance instance)
     18     : PluginResource(connection, instance) {
     19   SendCreate(BROWSER, PpapiHostMsg_NetworkProxy_Create());
     20 }
     21 
     22 NetworkProxyResource::~NetworkProxyResource() {
     23 }
     24 
     25 thunk::PPB_NetworkProxy_API* NetworkProxyResource::AsPPB_NetworkProxy_API() {
     26   return this;
     27 }
     28 
     29 int32_t NetworkProxyResource::GetProxyForURL(
     30       PP_Instance /* instance */,
     31       PP_Var url,
     32       PP_Var* proxy_string,
     33       scoped_refptr<TrackedCallback> callback) {
     34   StringVar* string_url = StringVar::FromPPVar(url);
     35   if (!string_url)
     36     return PP_ERROR_BADARGUMENT;
     37   Call<PpapiPluginMsg_NetworkProxy_GetProxyForURLReply>(
     38       BROWSER,
     39       PpapiHostMsg_NetworkProxy_GetProxyForURL(string_url->value()),
     40       base::Bind(&NetworkProxyResource::OnPluginMsgGetProxyForURLReply,
     41                  base::Unretained(this),
     42                  base::Unretained(proxy_string),
     43                  callback));
     44   return PP_OK_COMPLETIONPENDING;
     45 }
     46 
     47 void NetworkProxyResource::OnPluginMsgGetProxyForURLReply(
     48     PP_Var* proxy_string_out_param,
     49     scoped_refptr<TrackedCallback> callback,
     50     const ResourceMessageReplyParams& params,
     51     const std::string& proxy_string) {
     52   if (!TrackedCallback::IsPending(callback)) {
     53     // The callback should not have already been run. If this resource is
     54     // deleted, LastPluginRefWasReleased in PluginResource should abort the
     55     // callback and should not run this callback.
     56     NOTREACHED();
     57     return;
     58   }
     59   if (params.result() == PP_OK) {
     60     *proxy_string_out_param = (new StringVar(proxy_string))->GetPPVar();
     61   }
     62   callback->Run(params.result());
     63 }
     64 
     65 }  // namespace proxy
     66 }  // namespace ppapi
     67