summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/url_loader_resource.h
blob: f96c1aba2e44ecb047d7c0460418a393d4d835e5 (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
// Copyright (c) 2013 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.

#ifndef PPAPI_PROXY_URL_LOADER_RESOURCE_H_
#define PPAPI_PROXY_URL_LOADER_RESOURCE_H_

#include <deque>

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "ppapi/c/trusted/ppb_url_loader_trusted.h"
#include "ppapi/proxy/plugin_resource.h"
#include "ppapi/proxy/ppapi_proxy_export.h"
#include "ppapi/shared_impl/url_request_info_data.h"
#include "ppapi/thunk/ppb_url_loader_api.h"

namespace ppapi {
namespace proxy {

class URLResponseInfoResource;

class PPAPI_PROXY_EXPORT URLLoaderResource
    : public PluginResource,
      public NON_EXPORTED_BASE(thunk::PPB_URLLoader_API) {
 public:
  // Constructor for plugin-initiated loads.
  URLLoaderResource(Connection connection,
                    PP_Instance instance);

  // Constructor for renderer-initiated (document) loads. The loader ID is the
  // pending host ID for the already-created host in the renderer, and the
  // response data is the response for the already-opened connection.
  URLLoaderResource(Connection connection,
                    PP_Instance instance,
                    int pending_main_document_loader_id,
                    const URLResponseInfoData& data);

  ~URLLoaderResource() override;

  // Resource override.
  thunk::PPB_URLLoader_API* AsPPB_URLLoader_API() override;

  // PPB_URLLoader_API implementation.
  int32_t Open(PP_Resource request_id,
               scoped_refptr<TrackedCallback> callback) override;
  int32_t Open(const URLRequestInfoData& data,
               int requestor_pid,
               scoped_refptr<TrackedCallback> callback) override;
  int32_t FollowRedirect(scoped_refptr<TrackedCallback> callback) override;
  PP_Bool GetUploadProgress(int64_t* bytes_sent,
                            int64_t* total_bytes_to_be_sent) override;
  PP_Bool GetDownloadProgress(
      int64_t* bytes_received,
      int64_t* total_bytes_to_be_received) override;
  PP_Resource GetResponseInfo() override;
  int32_t ReadResponseBody(
      void* buffer,
      int32_t bytes_to_read,
      scoped_refptr<TrackedCallback> callback) override;
  int32_t FinishStreamingToFile(
      scoped_refptr<TrackedCallback> callback) override;
  void Close() override;
  void GrantUniversalAccess() override;
  void RegisterStatusCallback(
      PP_URLLoaderTrusted_StatusCallback callback) override;

  // PluginResource implementation.
  void OnReplyReceived(const ResourceMessageReplyParams& params,
                       const IPC::Message& msg) override;

 private:
  enum Mode {
    // The plugin has not called Open() yet.
    MODE_WAITING_TO_OPEN,

    // The plugin is waiting for the Open() or FollowRedirect callback.
    MODE_OPENING,

    // We've started to receive data and may receive more.
    MODE_STREAMING_DATA,

    // All data has been streamed or there was an error.
    MODE_LOAD_COMPLETE
  };

  // IPC message handlers.
  void OnPluginMsgReceivedResponse(const ResourceMessageReplyParams& params,
                                   const URLResponseInfoData& data);
  void OnPluginMsgSendData(const ResourceMessageReplyParams& params,
                           const IPC::Message& message);
  void OnPluginMsgFinishedLoading(const ResourceMessageReplyParams& params,
                                  int32_t result);
  void OnPluginMsgUpdateProgress(const ResourceMessageReplyParams& params,
                                 int64_t bytes_sent,
                                 int64_t total_bytes_to_be_sent,
                                 int64_t bytes_received,
                                 int64_t total_bytes_to_be_received);

  // Sends the defers loading message to the renderer to block or unblock the
  // load.
  void SetDefersLoading(bool defers_loading);

  int32_t ValidateCallback(scoped_refptr<TrackedCallback> callback);

  // Sets up |callback| as the pending callback. This should only be called once
  // it is certain that |PP_OK_COMPLETIONPENDING| will be returned.
  void RegisterCallback(scoped_refptr<TrackedCallback> callback);

  void RunCallback(int32_t result);

  // Saves the given response info to response_info_, handling file refs if
  // necessary. This does not issue any callbacks.
  void SaveResponseInfo(const URLResponseInfoData& data);

  int32_t FillUserBuffer();

  Mode mode_;
  URLRequestInfoData request_data_;

  scoped_refptr<TrackedCallback> pending_callback_;

  PP_URLLoaderTrusted_StatusCallback status_callback_;

  std::deque<char> buffer_;
  int64_t bytes_sent_;
  int64_t total_bytes_to_be_sent_;
  int64_t bytes_received_;
  int64_t total_bytes_to_be_received_;
  char* user_buffer_;
  size_t user_buffer_size_;
  int32_t done_status_;
  bool is_streaming_to_file_;
  bool is_asynchronous_load_suspended_;

  // The response info if we've received it.
  scoped_refptr<URLResponseInfoResource> response_info_;

  DISALLOW_COPY_AND_ASSIGN(URLLoaderResource);
};

}  // namespace proxy
}  // namespace ppapi

#endif  // PPAPI_PROXY_URL_LOADER_RESOURCE_H_