OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_BUFFER_H_ | |
6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_BUFFER_H_ | |
7 #pragma once | |
8 | |
9 #include <vector> | |
10 | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/synchronization/lock.h" | |
14 #include "content/common/content_export.h" | |
15 | |
16 namespace net { | |
17 class IOBuffer; | |
18 } | |
19 | |
20 namespace content { | |
21 | |
22 typedef std::pair<scoped_refptr<net::IOBuffer>, size_t> ContentElement; | |
23 typedef std::vector<ContentElement> ContentVector; | |
24 | |
25 // Helper function for ContentVector. | |
26 // Assembles the data from |contents| and returns it in an |IOBuffer|. | |
27 // The number of bytes in the |IOBuffer| is returned in |*num_bytes| | |
28 // (if |num_bytes| is not NULL). | |
29 // If |contents| is empty, returns NULL as an |IOBuffer| is not allowed | |
30 // to have 0 bytes. | |
31 CONTENT_EXPORT net::IOBuffer* AssembleData(const ContentVector& contents, | |
32 size_t* num_bytes); | |
33 | |
34 // |DownloadBuffer| is a thread-safe wrapper around |ContentVector|. | |
35 // | |
36 // It is created and populated on the IO thread, and passed to the | |
37 // FILE thread for writing. In order to avoid flooding the FILE thread with | |
38 // too many small write messages, each write is appended to the | |
39 // |DownloadBuffer| while waiting for the task to run on the FILE | |
40 // thread. Access to the write buffers is synchronized via the lock. | |
41 class CONTENT_EXPORT DownloadBuffer : | |
42 public base::RefCountedThreadSafe<DownloadBuffer> { | |
43 public: | |
44 DownloadBuffer(); | |
45 | |
46 // Adds data to the buffers. | |
47 // Returns the number of |IOBuffer|s in the ContentVector. | |
48 size_t AddData(net::IOBuffer* io_buffer, size_t byte_count); | |
49 | |
50 // Retrieves the ContentVector of buffers, clearing the contents. | |
51 // The caller takes ownership. | |
52 ContentVector* ReleaseContents(); | |
53 | |
54 // Gets the number of |IOBuffers| we have. | |
55 size_t size() const; | |
56 | |
57 private: | |
58 friend class base::RefCountedThreadSafe<DownloadBuffer>; | |
59 | |
60 // Do not allow explicit destruction by anyone else. | |
61 ~DownloadBuffer(); | |
62 | |
63 mutable base::Lock lock_; | |
64 ContentVector contents_; | |
65 | |
66 DISALLOW_COPY_AND_ASSIGN(DownloadBuffer); | |
67 }; | |
68 | |
69 } // namespace content | |
70 | |
71 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_BUFFER_H_ | |
OLD | NEW |