- add sources.
[platform/framework/web/crosswalk.git] / src / webkit / browser / quota / quota_temporary_storage_evictor.h
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 WEBKIT_BROWSER_QUOTA_QUOTA_TEMPORARY_STORAGE_EVICTOR_H_
6 #define WEBKIT_BROWSER_QUOTA_QUOTA_TEMPORARY_STORAGE_EVICTOR_H_
7
8 #include <map>
9 #include <string>
10
11 #include "base/memory/weak_ptr.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "base/timer/timer.h"
14 #include "webkit/browser/webkit_storage_browser_export.h"
15 #include "webkit/common/quota/quota_types.h"
16
17 class GURL;
18
19 namespace quota {
20
21 class QuotaEvictionHandler;
22 struct UsageAndQuota;
23
24 class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE QuotaTemporaryStorageEvictor
25     : public base::NonThreadSafe {
26  public:
27   struct Statistics {
28     Statistics()
29         : num_errors_on_evicting_origin(0),
30           num_errors_on_getting_usage_and_quota(0),
31           num_evicted_origins(0),
32           num_eviction_rounds(0),
33           num_skipped_eviction_rounds(0) {}
34     int64 num_errors_on_evicting_origin;
35     int64 num_errors_on_getting_usage_and_quota;
36     int64 num_evicted_origins;
37     int64 num_eviction_rounds;
38     int64 num_skipped_eviction_rounds;
39
40     void subtract_assign(const Statistics& rhs) {
41       num_errors_on_evicting_origin -= rhs.num_errors_on_evicting_origin;
42       num_errors_on_getting_usage_and_quota -=
43           rhs.num_errors_on_getting_usage_and_quota;
44       num_evicted_origins -= rhs.num_evicted_origins;
45       num_eviction_rounds -= rhs.num_eviction_rounds;
46       num_skipped_eviction_rounds -= rhs.num_skipped_eviction_rounds;
47     }
48   };
49
50   struct EvictionRoundStatistics {
51     EvictionRoundStatistics();
52
53     bool in_round;
54     bool is_initialized;
55
56     base::Time start_time;
57     int64 usage_overage_at_round;
58     int64 diskspace_shortage_at_round;
59
60     int64 usage_on_beginning_of_round;
61     int64 usage_on_end_of_round;
62     int64 num_evicted_origins_in_round;
63   };
64
65   QuotaTemporaryStorageEvictor(
66       QuotaEvictionHandler* quota_eviction_handler,
67       int64 interval_ms);
68   virtual ~QuotaTemporaryStorageEvictor();
69
70   void GetStatistics(std::map<std::string, int64>* statistics);
71   void ReportPerRoundHistogram();
72   void ReportPerHourHistogram();
73   void Start();
74
75   int64 min_available_disk_space_to_start_eviction() {
76     return min_available_disk_space_to_start_eviction_;
77   }
78   void reset_min_available_disk_space_to_start_eviction() {
79     min_available_disk_space_to_start_eviction_ =
80         kMinAvailableDiskSpaceToStartEvictionNotSpecified;
81   }
82   void set_min_available_disk_space_to_start_eviction(int64 value) {
83     min_available_disk_space_to_start_eviction_ = value;
84   }
85
86  private:
87   friend class QuotaTemporaryStorageEvictorTest;
88
89   void StartEvictionTimerWithDelay(int delay_ms);
90   void ConsiderEviction();
91   void OnGotUsageAndQuotaForEviction(
92       QuotaStatusCode status,
93       const UsageAndQuota& quota_and_usage);
94   void OnGotLRUOrigin(const GURL& origin);
95   void OnEvictionComplete(QuotaStatusCode status);
96
97   void OnEvictionRoundStarted();
98   void OnEvictionRoundFinished();
99
100   // This is only used for tests.
101   void set_repeated_eviction(bool repeated_eviction) {
102     repeated_eviction_ = repeated_eviction;
103   }
104
105   static const int kMinAvailableDiskSpaceToStartEvictionNotSpecified;
106
107   int64 min_available_disk_space_to_start_eviction_;
108
109   // Not owned; quota_eviction_handler owns us.
110   QuotaEvictionHandler* quota_eviction_handler_;
111
112   Statistics statistics_;
113   Statistics previous_statistics_;
114   EvictionRoundStatistics round_statistics_;
115   base::Time time_of_end_of_last_nonskipped_round_;
116   base::Time time_of_end_of_last_round_;
117
118   int64 interval_ms_;
119   bool repeated_eviction_;
120
121   base::OneShotTimer<QuotaTemporaryStorageEvictor> eviction_timer_;
122   base::RepeatingTimer<QuotaTemporaryStorageEvictor> histogram_timer_;
123   base::WeakPtrFactory<QuotaTemporaryStorageEvictor> weak_factory_;
124
125   DISALLOW_COPY_AND_ASSIGN(QuotaTemporaryStorageEvictor);
126 };
127
128 }  // namespace quota
129
130 #endif  // WEBKIT_BROWSER_QUOTA_QUOTA_TEMPORARY_STORAGE_EVICTOR_H_