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