1 // Copyright 2014 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 NET_HTTP_DISK_CACHE_BASED_QUIC_SERVER_INFO_H_ 6 #define NET_HTTP_DISK_CACHE_BASED_QUIC_SERVER_INFO_H_ 7 8 #include <string> 9 10 #include "base/memory/ref_counted.h" 11 #include "base/memory/weak_ptr.h" 12 #include "base/threading/non_thread_safe.h" 13 #include "net/base/completion_callback.h" 14 #include "net/disk_cache/disk_cache.h" 15 #include "net/quic/crypto/quic_server_info.h" 16 17 namespace net { 18 19 class HttpCache; 20 class IOBuffer; 21 class QuicServerId; 22 23 // DiskCacheBasedQuicServerInfo fetches information about a QUIC server from 24 // our standard disk cache. Since the information is defined to be 25 // non-sensitive, it's ok for us to keep it on disk. 26 class NET_EXPORT_PRIVATE DiskCacheBasedQuicServerInfo 27 : public QuicServerInfo, 28 public NON_EXPORTED_BASE(base::NonThreadSafe) { 29 public: 30 DiskCacheBasedQuicServerInfo(const QuicServerId& server_id, 31 HttpCache* http_cache); 32 33 // QuicServerInfo implementation. 34 virtual void Start() OVERRIDE; 35 virtual int WaitForDataReady(const CompletionCallback& callback) OVERRIDE; 36 virtual bool IsDataReady() OVERRIDE; 37 virtual bool IsReadyToPersist() OVERRIDE; 38 virtual void Persist() OVERRIDE; 39 40 private: 41 struct CacheOperationDataShim; 42 enum State { 43 GET_BACKEND, 44 GET_BACKEND_COMPLETE, 45 OPEN, 46 OPEN_COMPLETE, 47 READ, 48 READ_COMPLETE, 49 WAIT_FOR_DATA_READY_DONE, 50 CREATE_OR_OPEN, 51 CREATE_OR_OPEN_COMPLETE, 52 WRITE, 53 WRITE_COMPLETE, 54 SET_DONE, 55 NONE, 56 }; 57 58 virtual ~DiskCacheBasedQuicServerInfo(); 59 60 std::string key() const; 61 62 // The |unused| parameter is a small hack so that we can have the 63 // CacheOperationDataShim object owned by the Callback that is created for 64 // this method. See comment above CacheOperationDataShim for details. 65 void OnIOComplete(CacheOperationDataShim* unused, int rv); 66 67 int DoLoop(int rv); 68 69 int DoGetBackendComplete(int rv); 70 int DoOpenComplete(int rv); 71 int DoReadComplete(int rv); 72 int DoWriteComplete(int rv); 73 int DoCreateOrOpenComplete(int rv); 74 75 int DoGetBackend(); 76 int DoOpen(); 77 int DoRead(); 78 int DoWrite(); 79 int DoCreateOrOpen(); 80 81 // DoWaitForDataReadyDone is the terminal state of the read operation. 82 int DoWaitForDataReadyDone(); 83 84 // DoSetDone is the terminal state of the write operation. 85 int DoSetDone(); 86 87 base::WeakPtrFactory<DiskCacheBasedQuicServerInfo> weak_factory_; 88 CacheOperationDataShim* data_shim_; // Owned by |io_callback_|. 89 CompletionCallback io_callback_; 90 State state_; 91 bool ready_; 92 bool found_entry_; // Controls the behavior of DoCreateOrOpen. 93 std::string new_data_; 94 const QuicServerId server_id_; 95 HttpCache* const http_cache_; 96 disk_cache::Backend* backend_; 97 disk_cache::Entry* entry_; 98 CompletionCallback user_callback_; 99 scoped_refptr<IOBuffer> read_buffer_; 100 scoped_refptr<IOBuffer> write_buffer_; 101 std::string data_; 102 }; 103 104 } // namespace net 105 106 #endif // NET_HTTP_DISK_CACHE_BASED_QUIC_SERVER_INFO_H_ 107