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