15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// See net/disk_cache/disk_cache.h for the public interface of the cache.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NET_DISK_CACHE_FILE_H_
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define NET_DISK_CACHE_FILE_H_
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/platform_file.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_export.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace base {
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FilePath;
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace disk_cache {
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This interface is used to support asynchronous ReadData and WriteData calls.
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FileIOCallback {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Notified of the actual number of bytes read or written. This value is
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // negative if an error occurred.
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnFileIOComplete(int bytes_copied) = 0;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~FileIOCallback() {}
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Simple wrapper around a file that allows asynchronous operations.
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NET_EXPORT_PRIVATE File : public base::RefCounted<File> {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class base::RefCounted<File>;
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  File();
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // mixed_mode set to true enables regular synchronous operations for the file.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit File(bool mixed_mode);
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes the object to use the passed in file instead of opening it with
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the Init() call. No asynchronous operations can be performed with this
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // object.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit File(base::PlatformFile file);
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes the object to point to a given file. The file must aready exist
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // on disk, and allow shared read and write.
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool Init(const base::FilePath& name);
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the handle or file descriptor.
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::PlatformFile platform_file() const;
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the file was opened properly.
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsValid() const;
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Performs synchronous IO.
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Read(void* buffer, size_t buffer_len, size_t offset);
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Write(const void* buffer, size_t buffer_len, size_t offset);
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Performs asynchronous IO. callback will be called when the IO completes,
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // as an APC on the thread that queued the operation.
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Read(void* buffer, size_t buffer_len, size_t offset,
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            FileIOCallback* callback, bool* completed);
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Write(const void* buffer, size_t buffer_len, size_t offset,
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             FileIOCallback* callback, bool* completed);
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets the file's length. The file is truncated or extended with zeros to
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the new length.
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SetLength(size_t length);
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t GetLength();
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Blocks until |num_pending_io| IO operations complete.
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void WaitForPendingIO(int* num_pending_io);
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Drops current pending operations without waiting for them to complete.
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void DropPendingIO();
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~File();
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Performs the actual asynchronous write. If notify is set and there is no
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // callback, the call will be re-synchronized.
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool AsyncWrite(const void* buffer, size_t buffer_len, size_t offset,
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  FileIOCallback* callback, bool* completed);
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool init_;
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool mixed_;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::PlatformFile platform_file_;  // Regular, asynchronous IO handle.
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::PlatformFile sync_platform_file_;  // Synchronous IO handle.
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(File);
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace disk_cache
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // NET_DISK_CACHE_FILE_H_
96