summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/ppb_url_response_info_proxy.cc
blob: 575e0162394df0e183e1cb48a25e5efd38db199e (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
// 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_response_info_proxy.h"

#include "ppapi/c/ppb_url_response_info.h"
#include "ppapi/proxy/host_dispatcher.h"
#include "ppapi/proxy/plugin_dispatcher.h"
#include "ppapi/proxy/plugin_resource.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/proxy/ppb_file_ref_proxy.h"
#include "ppapi/proxy/serialized_var.h"

namespace pp {
namespace proxy {

class URLResponseInfo : public PluginResource {
 public:
  URLResponseInfo(const HostResource& resource)
      : PluginResource(resource) {
  }
  virtual ~URLResponseInfo() {}

  // Resource overrides.
  virtual URLResponseInfo* AsURLResponseInfo() { return this; }

 private:
  DISALLOW_COPY_AND_ASSIGN(URLResponseInfo);
};

namespace {

PP_Bool IsURLResponseInfo(PP_Resource resource) {
  URLResponseInfo* object = PluginResource::GetAs<URLResponseInfo>(resource);
  return BoolToPPBool(!!object);
}

PP_Var GetProperty(PP_Resource response, PP_URLResponseProperty property) {
  URLResponseInfo* object = PluginResource::GetAs<URLResponseInfo>(response);
  if (!object)
    return PP_MakeUndefined();
  PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(
      object->instance());
  if (!dispatcher)
    return PP_MakeUndefined();

  ReceiveSerializedVarReturnValue result;
  dispatcher->Send(new PpapiHostMsg_PPBURLResponseInfo_GetProperty(
      INTERFACE_ID_PPB_URL_RESPONSE_INFO, object->host_resource(), property,
      &result));
  return result.Return(dispatcher);
}

PP_Resource GetBodyAsFileRef(PP_Resource response) {
  URLResponseInfo* object = PluginResource::GetAs<URLResponseInfo>(response);
  if (!object)
    return 0;
  PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(
      object->instance());
  if (!dispatcher)
    return 0;

  // This could be more efficient by having the host automatically send us the
  // file ref when the request is streaming to a file and it's in the state
  // where the file is ready. This will prevent us from having to do this sync
  // IPC here.
  PPBFileRef_CreateInfo create_info;
  dispatcher->Send(new PpapiHostMsg_PPBURLResponseInfo_GetBodyAsFileRef(
      INTERFACE_ID_PPB_URL_RESPONSE_INFO,
      object->host_resource(), &create_info));
  return PPB_FileRef_Proxy::DeserializeFileRef(create_info);
}

const PPB_URLResponseInfo urlresponseinfo_interface = {
  &IsURLResponseInfo,
  &GetProperty,
  &GetBodyAsFileRef
};

InterfaceProxy* CreateURLResponseInfoProxy(Dispatcher* dispatcher,
                                           const void* target_interface) {
  return new PPB_URLResponseInfo_Proxy(dispatcher, target_interface);
}

}  // namespace

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

PPB_URLResponseInfo_Proxy::~PPB_URLResponseInfo_Proxy() {
}

// static
const InterfaceProxy::Info* PPB_URLResponseInfo_Proxy::GetInfo() {
  static const Info info = {
    &urlresponseinfo_interface,
    PPB_URLRESPONSEINFO_INTERFACE,
    INTERFACE_ID_PPB_URL_RESPONSE_INFO,
    false,
    &CreateURLResponseInfoProxy,
  };
  return &info;
}

// static
PP_Resource PPB_URLResponseInfo_Proxy::CreateResponseForResource(
    const HostResource& resource) {
  linked_ptr<URLResponseInfo> object(new URLResponseInfo(resource));
  return PluginResourceTracker::GetInstance()->AddResource(object);
}

bool PPB_URLResponseInfo_Proxy::OnMessageReceived(const IPC::Message& msg) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(PPB_URLResponseInfo_Proxy, msg)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBURLResponseInfo_GetProperty,
                        OnMsgGetProperty)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBURLResponseInfo_GetBodyAsFileRef,
                        OnMsgGetBodyAsFileRef)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  // TODO(brettw): handle bad messages.
  return handled;
}

void PPB_URLResponseInfo_Proxy::OnMsgGetProperty(
    HostResource response,
    int32_t property,
    SerializedVarReturnValue result) {
  result.Return(dispatcher(), ppb_url_response_info_target()->GetProperty(
      response.host_resource(), static_cast<PP_URLResponseProperty>(property)));
}

void PPB_URLResponseInfo_Proxy::OnMsgGetBodyAsFileRef(
    HostResource response,
    PPBFileRef_CreateInfo* result) {
  PP_Resource file_ref = ppb_url_response_info_target()->GetBodyAsFileRef(
          response.host_resource());

  // Use the FileRef proxy to serialize.
  DCHECK(!dispatcher()->IsPlugin());
  HostDispatcher* host_disp = static_cast<HostDispatcher*>(dispatcher());
  PPB_FileRef_Proxy* file_ref_proxy = static_cast<PPB_FileRef_Proxy*>(
      host_disp->GetOrCreatePPBInterfaceProxy(INTERFACE_ID_PPB_FILE_REF));
  file_ref_proxy->SerializeFileRef(file_ref, result);
}

}  // namespace proxy
}  // namespace pp