OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/loader/stream_resource_handler.h" | 5 #include "content/browser/loader/stream_resource_handler.h" |
6 | 6 |
7 #include "base/guid.h" | |
8 #include "base/logging.h" | 7 #include "base/logging.h" |
9 #include "content/browser/streams/stream.h" | |
10 #include "content/browser/streams/stream_registry.h" | |
11 #include "content/public/browser/resource_controller.h" | |
12 #include "net/base/io_buffer.h" | |
13 #include "net/url_request/url_request_status.h" | |
14 #include "url/url_constants.h" | |
15 | 8 |
16 namespace content { | 9 namespace content { |
17 | 10 |
18 StreamResourceHandler::StreamResourceHandler(net::URLRequest* request, | 11 StreamResourceHandler::StreamResourceHandler(net::URLRequest* request, |
19 StreamRegistry* registry, | 12 StreamRegistry* registry, |
20 const GURL& origin) | 13 const GURL& origin) |
21 : ResourceHandler(request), | 14 : ResourceHandler(request) { |
22 read_buffer_(NULL) { | 15 writer_.InitializeStream(registry, origin); |
23 // TODO(tyoshino): Find a way to share this with the blob URL creation in | |
24 // WebKit. | |
25 GURL url(std::string(url::kBlobScheme) + ":" + origin.spec() + | |
26 base::GenerateGUID()); | |
27 stream_ = new Stream(registry, this, url); | |
28 } | 16 } |
29 | 17 |
30 StreamResourceHandler::~StreamResourceHandler() { | 18 StreamResourceHandler::~StreamResourceHandler() { |
31 stream_->RemoveWriteObserver(this); | 19 } |
| 20 |
| 21 void StreamResourceHandler::SetController(ResourceController* controller) { |
| 22 writer_.set_controller(controller); |
| 23 ResourceHandler::SetController(controller); |
32 } | 24 } |
33 | 25 |
34 bool StreamResourceHandler::OnUploadProgress(uint64 position, | 26 bool StreamResourceHandler::OnUploadProgress(uint64 position, |
35 uint64 size) { | 27 uint64 size) { |
36 return true; | 28 return true; |
37 } | 29 } |
38 | 30 |
39 bool StreamResourceHandler::OnRequestRedirected( | 31 bool StreamResourceHandler::OnRequestRedirected( |
40 const net::RedirectInfo& redirect_info, | 32 const net::RedirectInfo& redirect_info, |
41 ResourceResponse* resp, | 33 ResourceResponse* resp, |
(...skipping 10 matching lines...) Expand all Loading... |
52 return true; | 44 return true; |
53 } | 45 } |
54 | 46 |
55 bool StreamResourceHandler::OnBeforeNetworkStart(const GURL& url, bool* defer) { | 47 bool StreamResourceHandler::OnBeforeNetworkStart(const GURL& url, bool* defer) { |
56 return true; | 48 return true; |
57 } | 49 } |
58 | 50 |
59 bool StreamResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 51 bool StreamResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
60 int* buf_size, | 52 int* buf_size, |
61 int min_size) { | 53 int min_size) { |
62 static const int kReadBufSize = 32768; | 54 writer_.OnWillRead(buf, buf_size, min_size); |
63 | |
64 DCHECK(buf && buf_size); | |
65 if (!read_buffer_.get()) | |
66 read_buffer_ = new net::IOBuffer(kReadBufSize); | |
67 *buf = read_buffer_.get(); | |
68 *buf_size = kReadBufSize; | |
69 | |
70 return true; | 55 return true; |
71 } | 56 } |
72 | 57 |
73 bool StreamResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | 58 bool StreamResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { |
74 if (!bytes_read) | 59 writer_.OnReadCompleted(bytes_read, defer); |
75 return true; | |
76 | |
77 // We have more data to read. | |
78 DCHECK(read_buffer_.get()); | |
79 | |
80 // Release the ownership of the buffer, and store a reference | |
81 // to it. A new one will be allocated in OnWillRead(). | |
82 scoped_refptr<net::IOBuffer> buffer; | |
83 read_buffer_.swap(buffer); | |
84 stream_->AddData(buffer, bytes_read); | |
85 | |
86 if (!stream_->can_add_data()) | |
87 *defer = true; | |
88 | |
89 return true; | 60 return true; |
90 } | 61 } |
91 | 62 |
92 void StreamResourceHandler::OnResponseCompleted( | 63 void StreamResourceHandler::OnResponseCompleted( |
93 const net::URLRequestStatus& status, | 64 const net::URLRequestStatus& status, |
94 const std::string& sec_info, | 65 const std::string& sec_info, |
95 bool* defer) { | 66 bool* defer) { |
96 stream_->Finalize(); | 67 writer_.Finalize(); |
97 } | 68 } |
98 | 69 |
99 void StreamResourceHandler::OnDataDownloaded(int bytes_downloaded) { | 70 void StreamResourceHandler::OnDataDownloaded(int bytes_downloaded) { |
100 NOTREACHED(); | 71 NOTREACHED(); |
101 } | 72 } |
102 | 73 |
103 void StreamResourceHandler::OnSpaceAvailable(Stream* stream) { | |
104 controller()->Resume(); | |
105 } | |
106 | |
107 void StreamResourceHandler::OnClose(Stream* stream) { | |
108 controller()->Cancel(); | |
109 } | |
110 | |
111 } // namespace content | 74 } // namespace content |
OLD | NEW |