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)#ifndef CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_FILE_SYSTEM_HELPER_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_FILE_SYSTEM_HELPER_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <list>
9eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include <map>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/callback.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/synchronization/lock.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/url_constants.h"
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/common/fileapi/file_system_types.h"
19eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/gurl.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)namespace storage {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FileSystemContext;
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Profile;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Defines an interface for classes that deal with aggregating and deleting
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// browsing data stored in an origin's file systems.
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// BrowsingDataFileSystemHelper instances for a specific profile should be
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// created via the static Create method. Each instance will lazily fetch file
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// system data when a client calls StartFetching from the UI thread, and will
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// notify the client via a supplied callback when the data is available.
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Only one StartFetching task can run at a time: executing StartFetching while
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// another StartFetching task is running will DCHECK.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The client's callback is passed a list of FileSystemInfo objects containing
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// usage information for each origin's temporary and persistent file systems.
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Clients may remove an origin's file systems at any time (even before fetching
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// data) by calling DeleteFileSystemOrigin() on the UI thread. Calling
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DeleteFileSystemOrigin() for an origin that doesn't have any is safe; it's
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// just an expensive NOOP.
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BrowsingDataFileSystemHelper
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : public base::RefCountedThreadSafe<BrowsingDataFileSystemHelper> {
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Detailed information about a file system, including it's origin GURL,
47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // the amount of data (in bytes) for each sandboxed filesystem type.
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct FileSystemInfo {
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    explicit FileSystemInfo(const GURL& origin);
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ~FileSystemInfo();
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The origin for which the information is relevant.
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GURL origin;
54eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // FileSystemType to usage (in bytes) map.
5503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    std::map<storage::FileSystemType, int64> usage_map;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates a BrowsingDataFileSystemHelper instance for the file systems
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // stored in |profile|'s user data directory. The BrowsingDataFileSystemHelper
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // object will hold a reference to the Profile that's passed in, but is not
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // responsible for destroying it.
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The BrowsingDataFileSystemHelper will not change the profile itself, but
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // can modify data it contains (by removing file systems).
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static BrowsingDataFileSystemHelper* Create(
6603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      storage::FileSystemContext* file_system_context);
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Starts the process of fetching file system data, which will call |callback|
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // upon completion, passing it a constant list of FileSystemInfo objects.
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // StartFetching must be called only in the UI thread; the provided Callback1
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // will likewise be executed asynchronously on the UI thread.
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BrowsingDataFileSystemHelper takes ownership of the Callback1, and is
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // responsible for deleting it once it's no longer needed.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void StartFetching(const base::Callback<
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      void(const std::list<FileSystemInfo>&)>& callback) = 0;
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Deletes any temporary or persistent file systems associated with |origin|
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // from the disk. Deletion will occur asynchronously on the FILE thread, but
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this function must be called only on the UI thread.
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void DeleteFileSystemOrigin(const GURL& origin) = 0;
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class base::RefCountedThreadSafe<BrowsingDataFileSystemHelper>;
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  BrowsingDataFileSystemHelper() {}
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~BrowsingDataFileSystemHelper() {}
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// An implementation of the BrowsingDataFileSystemHelper interface that can
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// be manually populated with data, rather than fetching data from the file
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// systems created in a particular Profile.
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CannedBrowsingDataFileSystemHelper
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : public BrowsingDataFileSystemHelper {
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |profile| is unused in this canned implementation, but it's the interface
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // we're writing to, so we'll accept it, but not store it.
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit CannedBrowsingDataFileSystemHelper(Profile* profile);
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Manually adds a filesystem to the set of canned file systems that this
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // helper returns via StartFetching. If an origin contains both a temporary
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and a persistent filesystem, AddFileSystem must be called twice (once for
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // each file system type).
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AddFileSystem(const GURL& origin,
10503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                     storage::FileSystemType type,
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     int64 size);
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Clear this helper's list of canned filesystems.
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Reset();
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if no filesystems are currently stored.
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool empty() const;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the number of currently stored filesystems.
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t GetFileSystemCount() const;
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the current list of filesystems.
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::list<FileSystemInfo>& GetFileSystemInfo() {
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return file_system_info_;
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BrowsingDataFileSystemHelper implementation.
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void StartFetching(const base::Callback<
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      void(const std::list<FileSystemInfo>&)>& callback) OVERRIDE;
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note that this doesn't actually have an implementation for this canned
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // class. It hasn't been necessary for anything that uses the canned
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // implementation, as the canned class is only used in tests, or in read-only
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // contexts (like the non-modal cookie dialog).
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void DeleteFileSystemOrigin(const GURL& origin) OVERRIDE {}
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~CannedBrowsingDataFileSystemHelper();
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Holds the current list of filesystems returned to the client.
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::list<FileSystemInfo> file_system_info_;
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The callback passed in at the beginning of the StartFetching workflow so
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that it can be triggered via NotifyOnUIThread.
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::Callback<void(const std::list<FileSystemInfo>&)> completion_callback_;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataFileSystemHelper);
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_FILE_SYSTEM_HELPER_H_
146