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)>; |
| 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 |
| 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|. Note that this method will |
| 64 // not necessarily write data back to the cache if the incoming data is |
| 65 // equivalent to the existing cached data. See the source of this function for |
| 66 // details about how this function drives the state machine. |
| 67 net::Error MaybeWriteHeaders(HttpResponseInfoIOBuffer* headers, |
| 68 const OnWriteCompleteCallback& callback); |
| 69 |
| 70 // Writes the supplied body data |data| back to the cache. Returns |
| 71 // ERR_IO_PENDING if the write will complete asynchronously, in which case |
| 72 // |callback| will be called when it completes. Otherwise, returns a code |
| 73 // other than ERR_IO_PENDING and does not invoke |callback|. Note that this |
| 74 // method will not necessarily write data back to the cache if the incoming |
| 75 // data is equivalent to the existing cached data. See the source of this |
| 76 // function for details about how this function drives the state machine. |
| 77 net::Error MaybeWriteData(net::IOBuffer* buf, |
| 78 size_t buf_size, |
| 79 const OnWriteCompleteCallback& callback); |
| 80 |
| 81 // Returns a count of bytes written back to the cache. |
| 82 size_t bytes_written() const { return bytes_written_; } |
| 83 bool did_replace() const { return did_replace_; } |
| 84 |
| 85 private: |
| 86 // Drives this class's state machine. This function steps the state machine |
| 87 // until one of: |
| 88 // a) One of the state functions returns an error |
| 89 // b) One of the state functions pauses the machine |
| 90 // c) The state machine reaches STATE_DONE |
| 91 // A successful value (net::OK or greater) indicates that the requested |
| 92 // operation completed synchronously. A return value of ERR_IO_PENDING |
| 93 // indicates that some step had to submit asynchronous IO for later |
| 94 // completion, and the state machine will resume running (via AsyncDoLoop) |
| 95 // when that asynchronous IO completes. Any other return value indicates that |
| 96 // the requested operation failed synchronously. |
| 97 int DoLoop(int result); |
| 98 |
| 99 // State handlers. See function comments in the corresponding source file for |
| 100 // details on these. |
| 101 int DoStart(int* next_state, bool* pause, int result); |
| 102 int DoReadHeadersForCompare(int* next_state, bool* pause, int result); |
| 103 int DoReadHeadersForCompareDone(int* next_state, bool* pause, int result); |
| 104 int DoReadDataForCompare(int* next_state, bool* pause, int result); |
| 105 int DoReadDataForCompareDone(int* next_state, bool* pause, int result); |
| 106 int DoReadHeadersForCopy(int* next_state, bool* pause, int result); |
| 107 int DoReadHeadersForCopyDone(int* next_state, bool* pause, int result); |
| 108 int DoWriteHeadersForCopy(int* next_state, bool* pause, int result); |
| 109 int DoWriteHeadersForCopyDone(int* next_state, bool* pause, int result); |
| 110 int DoReadDataForCopy(int* next_state, bool* pause, int result); |
| 111 int DoReadDataForCopyDone(int* next_state, bool* pause, int result); |
| 112 int DoWriteDataForCopy(int* next_state, bool* pause, int result); |
| 113 int DoWriteDataForCopyDone(int* next_state, bool* pause, int result); |
| 114 int DoWriteHeadersForPassthrough(int* next_state, bool* pause, int result); |
| 115 int DoWriteHeadersForPassthroughDone(int* next_state, |
| 116 bool* pause, |
| 117 int result); |
| 118 int DoWriteDataForPassthrough(int* next_state, bool* pause, int result); |
| 119 int DoWriteDataForPassthroughDone(int* next_state, bool* pause, int result); |
| 120 int DoDone(int* next_state, bool* pause, int result); |
| 121 |
| 122 // Wrappers for asynchronous calls. These are responsible for scheduling a |
| 123 // callback to drive the state machine if needed. These either: |
| 124 // a) Return ERR_IO_PENDING, and schedule a callback to run the state |
| 125 // machine's Run() later, or |
| 126 // b) Return some other value and do not schedule a callback. |
| 127 int ReadInfoHelper(const scoped_ptr<ServiceWorkerResponseReader>& reader, |
| 128 HttpResponseInfoIOBuffer* buf); |
| 129 int ReadDataHelper(const scoped_ptr<ServiceWorkerResponseReader>& reader, |
| 130 net::IOBuffer* buf, |
| 131 int buf_len); |
| 132 int WriteInfoHelper(const scoped_ptr<ServiceWorkerResponseWriter>& writer, |
| 133 HttpResponseInfoIOBuffer* buf); |
| 134 int WriteDataHelper(const scoped_ptr<ServiceWorkerResponseWriter>& writer, |
| 135 net::IOBuffer* buf, |
| 136 int buf_len); |
| 137 |
| 138 // Callback used by the above helpers. |
| 139 void AsyncDoLoop(int result); |
| 140 |
| 141 int state_; |
| 142 |
| 143 scoped_refptr<HttpResponseInfoIOBuffer> headers_to_read_; |
| 144 scoped_refptr<HttpResponseInfoIOBuffer> headers_to_write_; |
| 145 scoped_refptr<net::IOBuffer> data_to_read_; |
| 146 int len_to_read_; |
| 147 scoped_refptr<net::IOBuffer> data_to_copy_; |
| 148 scoped_refptr<net::IOBuffer> data_to_write_; |
| 149 int len_to_write_; |
| 150 OnWriteCompleteCallback pending_callback_; |
| 151 |
| 152 size_t cached_length_; |
| 153 |
| 154 size_t bytes_compared_; |
| 155 size_t bytes_copied_; |
| 156 size_t bytes_written_; |
| 157 |
| 158 bool did_replace_; |
| 159 |
| 160 size_t compare_offset_; |
| 161 |
| 162 ResponseReaderCreator reader_creator_; |
| 163 ResponseWriterCreator writer_creator_; |
| 164 scoped_ptr<ServiceWorkerResponseReader> compare_reader_; |
| 165 scoped_ptr<ServiceWorkerResponseReader> copy_reader_; |
| 166 scoped_ptr<ServiceWorkerResponseWriter> writer_; |
| 167 base::WeakPtrFactory<ServiceWorkerCacheWriter> weak_factory_; |
| 168 }; |
| 169 |
| 170 } // namespace content |
| 171 |
| 172 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_WRITER_H_ |
OLD | NEW |