OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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_SERVICE_WORKER_SERVICE_WORKER_CACHE_WRITER_H_ | |
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_WRITER_H_ | |
7 | |
8 #include <map> | |
9 #include <set> | |
10 | |
11 #include "base/callback.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "net/base/io_buffer.h" | |
15 #include "net/base/net_errors.h" | |
16 | |
17 namespace content { | |
18 | |
19 struct HttpResponseInfoIOBuffer; | |
20 class ServiceWorkerCacheWriterCore; | |
21 class ServiceWorkerResponseReader; | |
22 class ServiceWorkerResponseWriter; | |
23 class ServiceWorkerStorage; | |
24 | |
25 // This class is responsible for possibly updating the ServiceWorker script | |
26 // cache for an installed ServiceWorker main script. If there is no existing | |
27 // cache entry, this class always writes supplied data back to the cache; if | |
28 // there is an existing cache entry, this class only writes supplied data back | |
29 // if there is a cache mismatch. | |
30 // | |
31 // Note that writes done by this class cannot be "short" - ie, if they succeed, | |
32 // they always write all the supplied data back. Therefore completions are | |
33 // signalled with net::Error without a count of bytes written. | |
34 // | |
35 // This class's behavior is modelled as a state machine; see the DoLoop function | |
36 // for comments about this. | |
37 class ServiceWorkerCacheWriter { | |
38 public: | |
39 using OnWriteCompleteCallback = base::Callback<void(net::Error)>; | |
Randy Smith (Not in Mondays)
2015/09/01 20:35:42
Why using rather than typedef?
Elly Fong-Jones
2015/09/09 13:36:08
They are recommended instead of typedef here: http
Randy Smith (Not in Mondays)
2015/09/13 20:56:35
Huh. Learn something new every day. Thanks for t
| |
40 | |
41 // The types for the factory functions passed into the constructor. These are | |
42 // responsible for creating readers from the existing cache entry and writers | |
43 // to the new cache entry when called. These are passed in as factories | |
44 // instead of passing readers and writers in directly to avoid creating | |
45 // writers to entries that won't be updated, and because this class may need | |
Randy Smith (Not in Mondays)
2015/09/01 20:35:42
What's the cost of creating writers to entries tha
Elly Fong-Jones
2015/09/09 13:36:08
This isn't done because of cost, it's done because
Randy Smith (Not in Mondays)
2015/09/13 20:56:35
Oy vais. Ok.
| |
46 // multiple readers internally. | |
47 using ResponseReaderCreator = | |
48 base::Callback<scoped_ptr<ServiceWorkerResponseReader>(void)>; | |
49 using ResponseWriterCreator = | |
50 base::Callback<scoped_ptr<ServiceWorkerResponseWriter>(void)>; | |
51 | |
52 // The existing reader may be null, in which case this instance will | |
53 // unconditionally write back data supplied to |MaybeWriteHeaders| and | |
54 // |MaybeWriteData|. | |
55 ServiceWorkerCacheWriter(const ResponseReaderCreator& reader_creator, | |
56 const ResponseWriterCreator& writer_creator); | |
57 | |
58 virtual ~ServiceWorkerCacheWriter(); | |
59 | |
60 // Writes the supplied |headers| back to the cache. Returns ERR_IO_PENDING if | |
61 // the write will complete asynchronously, in which case |callback| will be | |
62 // called when it completes. Otherwise, returns a code other than | |
63 // ERR_IO_PENDING and does not invoke |callback|. | |
falken
2015/09/01 14:43:52
nit: maybe nice for the comment to explain why it'
Elly Fong-Jones
2015/09/09 13:36:08
Done.
| |
64 net::Error MaybeWriteHeaders(HttpResponseInfoIOBuffer* headers, | |
65 const OnWriteCompleteCallback& callback); | |
66 | |
67 // Writes the supplied body data |data| back to the cache. Returns | |
68 // ERR_IO_PENDING if the write will complete asynchronously, in which case | |
69 // |callback| will be called when it completes. Otherwise, returns a code | |
70 // other than ERR_IO_PENDING and does not invoke |callback|. | |
falken
2015/09/01 14:43:52
ditto
Elly Fong-Jones
2015/09/09 13:36:08
Done.
| |
71 net::Error MaybeWriteData(net::IOBuffer* buf, | |
72 size_t buf_size, | |
73 const OnWriteCompleteCallback& callback); | |
74 | |
75 // Returns a count of bytes written back to the cache. | |
76 size_t bytes_written() const { return bytes_written_; } | |
77 | |
78 private: | |
79 // Drives this class's state machine. This function steps the state machine | |
80 // until one of: | |
81 // a) One of the state functions returns an error | |
82 // b) One of the state functions pauses the machine | |
83 // c) The state machine reaches STATE_DONE | |
84 // A successful value (net::OK or greater) indicates that the requested | |
85 // operation completed synchronously. A return value of ERR_IO_PENDING | |
86 // indicates that some step had to submit asynchronous IO for later | |
87 // completion, and the state machine will resume running (via AsyncDoLoop) | |
88 // when that asynchronous IO completes. Any other return value indicates that | |
89 // the requested operation failed synchronously. | |
Randy Smith (Not in Mondays)
2015/09/01 20:35:42
Based on my documentation CL, I think the usual st
Elly Fong-Jones
2015/09/09 13:36:08
I've added these DCHECKs in MaybeWriteHeaders and
| |
90 int DoLoop(int result); | |
91 | |
92 // State handlers. See function comments in the corresponding source file for | |
93 // details on these. | |
Randy Smith (Not in Mondays)
2015/09/01 20:35:42
I think especially for state machine implementatio
Elly Fong-Jones
2015/09/09 13:36:08
Done.
| |
94 int Start(int* next_state, bool* pause, int result); | |
95 int ReadHeadersForCompare(int* next_state, bool* pause, int result); | |
96 int ReadHeadersForCompareDone(int* next_state, bool* pause, int result); | |
97 int ReadDataForCompare(int* next_state, bool* pause, int result); | |
98 int ReadDataForCompareDone(int* next_state, bool* pause, int result); | |
99 int ReadHeadersForCopy(int* next_state, bool* pause, int result); | |
100 int ReadHeadersForCopyDone(int* next_state, bool* pause, int result); | |
101 int WriteHeadersForCopy(int* next_state, bool* pause, int result); | |
102 int WriteHeadersForCopyDone(int* next_state, bool* pause, int result); | |
103 int ReadDataForCopy(int* next_state, bool* pause, int result); | |
104 int ReadDataForCopyDone(int* next_state, bool* pause, int result); | |
105 int WriteDataForCopy(int* next_state, bool* pause, int result); | |
106 int WriteDataForCopyDone(int* next_state, bool* pause, int result); | |
107 int WriteHeadersForPassthrough(int* next_state, bool* pause, int result); | |
108 int WriteHeadersForPassthroughDone(int* next_state, bool* pause, int result); | |
109 int WriteDataForPassthrough(int* next_state, bool* pause, int result); | |
110 int WriteDataForPassthroughDone(int* next_state, bool* pause, int result); | |
111 int Done(int* next_state, bool* pause, int result); | |
112 | |
113 // Wrappers for asynchronous calls. These are responsible for scheduling a | |
114 // callback to drive the state machine if needed. These either: | |
115 // a) Return ERR_IO_PENDING, and schedule a callback to run the state | |
116 // machine's Run() later, or | |
117 // b) Return some other value and do not schedule a callback. | |
118 int ReadInfoHelper(const scoped_ptr<ServiceWorkerResponseReader>& reader, | |
119 HttpResponseInfoIOBuffer* buf); | |
120 int ReadDataHelper(const scoped_ptr<ServiceWorkerResponseReader>& reader, | |
121 net::IOBuffer* buf, | |
122 int buf_len); | |
123 int WriteInfoHelper(const scoped_ptr<ServiceWorkerResponseWriter>& writer, | |
124 HttpResponseInfoIOBuffer* buf); | |
125 int WriteDataHelper(const scoped_ptr<ServiceWorkerResponseWriter>& writer, | |
126 net::IOBuffer* buf, | |
127 int buf_len); | |
128 | |
129 // Callback used by the above helpers. | |
130 void AsyncDoLoop(int result); | |
131 | |
132 int state_; | |
133 | |
134 scoped_refptr<HttpResponseInfoIOBuffer> headers_to_read_; | |
135 scoped_refptr<HttpResponseInfoIOBuffer> headers_to_write_; | |
136 scoped_refptr<net::IOBuffer> data_to_read_; | |
137 int len_to_read_; | |
138 scoped_refptr<net::IOBuffer> data_to_copy_; | |
139 scoped_refptr<net::IOBuffer> data_to_write_; | |
140 int len_to_write_; | |
141 OnWriteCompleteCallback pending_callback_; | |
142 | |
143 size_t cached_length_; | |
144 size_t net_length_; | |
145 | |
146 size_t bytes_compared_; | |
147 size_t bytes_copied_; | |
148 size_t bytes_written_; | |
149 | |
150 size_t compare_offset_; | |
151 | |
152 ResponseReaderCreator reader_creator_; | |
153 ResponseWriterCreator writer_creator_; | |
154 scoped_ptr<ServiceWorkerResponseReader> compare_reader_; | |
155 scoped_ptr<ServiceWorkerResponseReader> copy_reader_; | |
156 scoped_ptr<ServiceWorkerResponseWriter> writer_; | |
157 base::WeakPtrFactory<ServiceWorkerCacheWriter> weak_factory_; | |
158 }; | |
159 | |
160 } // namespace content | |
161 | |
162 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_WRITER_H_ | |
OLD | NEW |