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