Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(583)

Side by Side Diff: remoting/client/plugin/pepper_url_request.cc

Issue 1679023009: Add remoting::UrlRequest interface with 2 implementations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/client/plugin/pepper_url_request.h ('k') | remoting/remoting_nacl.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 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 "remoting/client/plugin/pepper_url_request.h"
6
7 #include "base/callback_helpers.h"
8 #include "base/logging.h"
9 #include "ppapi/cpp/url_response_info.h"
10
11 // Read buffer we allocate per read when reading response from
12 // URLLoader.
13 static const int kReadSize = 1024;
14
15 namespace remoting {
16
17 PepperUrlRequest::PepperUrlRequest(pp::InstanceHandle pp_instance,
18 const std::string& url)
19 : request_info_(pp_instance),
20 url_loader_(pp_instance),
21 url_(url),
22 callback_factory_(this) {
23 request_info_.SetMethod("GET");
24 request_info_.SetURL(url);
25 }
26
27 PepperUrlRequest::~PepperUrlRequest() {}
28
29 void PepperUrlRequest::AddHeader(const std::string& value) {
30 headers_ += value + "\n\r";
31 }
32
33 void PepperUrlRequest::Start(const OnResultCallback& on_result_callback) {
34 on_result_callback_ = on_result_callback;
35
36 request_info_.SetHeaders(headers_);
37 int result = url_loader_.Open(
38 request_info_,
39 callback_factory_.NewCallback(&PepperUrlRequest::OnUrlOpened));
40 DCHECK_EQ(result, PP_OK_COMPLETIONPENDING);
41 }
42
43 void PepperUrlRequest::OnUrlOpened(int32_t result) {
44 if (result == PP_ERROR_ABORTED) {
45 return;
46 }
47
48 if (result < 0) {
49 LOG(WARNING) << "pp::URLLoader for " << url_ << " failed: " << result;
50 base::ResetAndReturn(&on_result_callback_).Run(Result::Failed());
51 return;
52 }
53
54 ReadResponseBody();
55 }
56
57 void PepperUrlRequest::ReadResponseBody() {
58 int pos = response_.size();
59 response_.resize(pos + kReadSize);
60 int result = url_loader_.ReadResponseBody(
61 &response_[pos], kReadSize,
62 callback_factory_.NewCallback(&PepperUrlRequest::OnResponseBodyRead));
63 DCHECK_EQ(result, PP_OK_COMPLETIONPENDING);
64 }
65
66 void PepperUrlRequest::OnResponseBodyRead(int32_t result) {
67 if (result == PP_ERROR_ABORTED)
68 return;
69
70 if (result < 0) {
71 LOG(WARNING) << "Failed to read HTTP response body when fetching "
72 << url_ << ", error: " << result;
73 base::ResetAndReturn(&on_result_callback_).Run(Result::Failed());
74 return;
75 }
76
77 // Resize the buffer in case we've read less than was requested.
78 CHECK_LE(result, kReadSize);
79 CHECK_GE(static_cast<int>(response_.size()), kReadSize);
80 response_.resize(response_.size() - kReadSize + result);
81
82 // Try to read again if there is more data to read.
83 if (result > 0) {
84 ReadResponseBody();
85 return;
86 }
87
88 base::ResetAndReturn(&on_result_callback_)
89 .Run(Result(url_loader_.GetResponseInfo().GetStatusCode(), response_));
90 }
91
92 PepperUrlRequestFactory::PepperUrlRequestFactory(pp::InstanceHandle pp_instance)
93 : pp_instance_(pp_instance) {}
94 PepperUrlRequestFactory::~PepperUrlRequestFactory() {}
95
96 scoped_ptr<UrlRequest> PepperUrlRequestFactory::CreateUrlRequest(
97 const std::string& url) {
98 return make_scoped_ptr(new PepperUrlRequest(pp_instance_, url));
99 }
100
101 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/client/plugin/pepper_url_request.h ('k') | remoting/remoting_nacl.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698