1// Copyright 2013 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 CHROME_BROWSER_EXTENSIONS_API_SYSTEM_STORAGE_STORAGE_INFO_PROVIDER_H_
6#define CHROME_BROWSER_EXTENSIONS_API_SYSTEM_STORAGE_STORAGE_INFO_PROVIDER_H_
7
8#include <set>
9
10#include "base/lazy_instance.h"
11#include "base/memory/ref_counted.h"
12#include "base/observer_list_threadsafe.h"
13#include "base/timer/timer.h"
14#include "chrome/browser/extensions/api/system_info/system_info_provider.h"
15#include "chrome/browser/storage_monitor/removable_storage_observer.h"
16#include "chrome/browser/storage_monitor/storage_info.h"
17#include "chrome/common/extensions/api/system_storage.h"
18#include "content/public/browser/notification_observer.h"
19#include "content/public/browser/notification_registrar.h"
20
21namespace extensions {
22
23namespace systeminfo {
24
25// Build StorageUnitInfo struct from StorageInfo instance. The |unit|
26// parameter is the output value.
27void BuildStorageUnitInfo(const StorageInfo& info,
28                          api::system_storage::StorageUnitInfo* unit);
29
30}  // namespace systeminfo
31
32typedef std::vector<linked_ptr<
33    api::system_storage::StorageUnitInfo> >
34        StorageUnitInfoList;
35
36class StorageInfoProvider : public SystemInfoProvider {
37 public:
38  typedef base::Callback<void(const std::string&, double)>
39      GetStorageFreeSpaceCallback;
40
41  // Get the single shared instance of StorageInfoProvider.
42  static StorageInfoProvider* Get();
43
44  // SystemInfoProvider implementations
45  virtual void PrepareQueryOnUIThread() OVERRIDE;
46  virtual void InitializeProvider(const base::Closure& do_query_info_callback)
47      OVERRIDE;
48
49  virtual double GetStorageFreeSpaceFromTransientIdOnFileThread(
50      const std::string& transient_id);
51
52  const StorageUnitInfoList& storage_unit_info_list() const;
53
54  static void InitializeForTesting(scoped_refptr<StorageInfoProvider> provider);
55
56 protected:
57  StorageInfoProvider();
58
59  virtual ~StorageInfoProvider();
60
61  // Put all available storages' information into |info_|.
62  void GetAllStoragesIntoInfoList();
63
64  // The last information filled up by QueryInfo and is accessed on multiple
65  // threads, but the whole class is being guarded by SystemInfoProvider base
66  // class.
67  //
68  // |info_| is accessed on the UI thread while |is_waiting_for_completion_| is
69  // false and on the sequenced worker pool while |is_waiting_for_completion_|
70  // is true.
71  StorageUnitInfoList info_;
72
73 private:
74  // SystemInfoProvider implementations.
75  // Override to query the available capacity of all known storage devices on
76  // the blocking pool, including fixed and removable devices.
77  virtual bool QueryInfo() OVERRIDE;
78
79  static base::LazyInstance<scoped_refptr<StorageInfoProvider> > provider_;
80
81  DISALLOW_COPY_AND_ASSIGN(StorageInfoProvider);
82};
83
84}  // namespace extensions
85
86#endif  // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_STORAGE_STORAGE_INFO_PROVIDER_H_
87