15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef NET_HTTP_DISK_CACHE_BASED_QUIC_SERVER_INFO_H_
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define NET_HTTP_DISK_CACHE_BASED_QUIC_SERVER_INFO_H_
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <string>
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/memory/ref_counted.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/memory/weak_ptr.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/threading/non_thread_safe.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/base/completion_callback.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/disk_cache/disk_cache.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/quic/crypto/quic_server_info.h"
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace net {
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class HttpCache;
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class IOBuffer;
21e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochclass QuicServerId;
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// DiskCacheBasedQuicServerInfo fetches information about a QUIC server from
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// our standard disk cache. Since the information is defined to be
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// non-sensitive, it's ok for us to keep it on disk.
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class NET_EXPORT_PRIVATE DiskCacheBasedQuicServerInfo
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : public QuicServerInfo,
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      public NON_EXPORTED_BASE(base::NonThreadSafe) {
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
30e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  DiskCacheBasedQuicServerInfo(const QuicServerId& server_id,
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               HttpCache* http_cache);
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // QuicServerInfo implementation.
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void Start() OVERRIDE;
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual int WaitForDataReady(const CompletionCallback& callback) OVERRIDE;
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual bool IsDataReady() OVERRIDE;
3723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  virtual bool IsReadyToPersist() OVERRIDE;
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void Persist() OVERRIDE;
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  struct CacheOperationDataShim;
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  enum State {
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    GET_BACKEND,
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    GET_BACKEND_COMPLETE,
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    OPEN,
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    OPEN_COMPLETE,
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    READ,
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    READ_COMPLETE,
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    WAIT_FOR_DATA_READY_DONE,
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CREATE_OR_OPEN,
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CREATE_OR_OPEN_COMPLETE,
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    WRITE,
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    WRITE_COMPLETE,
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    SET_DONE,
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    NONE,
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~DiskCacheBasedQuicServerInfo();
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string key() const;
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The |unused| parameter is a small hack so that we can have the
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // CacheOperationDataShim object owned by the Callback that is created for
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // this method.  See comment above CacheOperationDataShim for details.
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void OnIOComplete(CacheOperationDataShim* unused, int rv);
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int DoLoop(int rv);
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int DoGetBackendComplete(int rv);
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int DoOpenComplete(int rv);
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int DoReadComplete(int rv);
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int DoWriteComplete(int rv);
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int DoCreateOrOpenComplete(int rv);
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int DoGetBackend();
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int DoOpen();
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int DoRead();
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int DoWrite();
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int DoCreateOrOpen();
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // DoWaitForDataReadyDone is the terminal state of the read operation.
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int DoWaitForDataReadyDone();
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // DoSetDone is the terminal state of the write operation.
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int DoSetDone();
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CacheOperationDataShim* data_shim_;  // Owned by |io_callback_|.
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CompletionCallback io_callback_;
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  State state_;
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool ready_;
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool found_entry_;  // Controls the behavior of DoCreateOrOpen.
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string new_data_;
93e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  const QuicServerId server_id_;
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  HttpCache* const http_cache_;
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  disk_cache::Backend* backend_;
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  disk_cache::Entry* entry_;
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CompletionCallback user_callback_;
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_refptr<IOBuffer> read_buffer_;
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_refptr<IOBuffer> write_buffer_;
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string data_;
1011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  base::WeakPtrFactory<DiskCacheBasedQuicServerInfo> weak_factory_;
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace net
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // NET_HTTP_DISK_CACHE_BASED_QUIC_SERVER_INFO_H_
108