Index: components/framelet/browser/resource_usage_reporter.h |
diff --git a/components/framelet/browser/resource_usage_reporter.h b/components/framelet/browser/resource_usage_reporter.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..94d4e8857a27734b4b5a830814033fb7ce9e5ead |
--- /dev/null |
+++ b/components/framelet/browser/resource_usage_reporter.h |
@@ -0,0 +1,68 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef COMPONENTS_FRAMELET_BROWSER_RESOURCE_USAGE_REPORTER_H_ |
+#define COMPONENTS_FRAMELET_BROWSER_RESOURCE_USAGE_REPORTER_H_ |
+ |
+#include "base/logging.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "components/framelet/browser/resource_usage_range.h" |
+ |
+namespace framelet { |
+ |
+template <class T> |
+class ResourceUsageReporterClient; |
+ |
+enum class ResourceUsageLevel : int { |
+ LOW = 0, |
+ MEDIUM, |
+ HIGH, |
+ CRITICAL, |
+ MAX_VALUE |
+}; |
+ |
+template <class T> |
+class ResourceUsageReporter { |
+ public: |
+ explicit ResourceUsageReporter(ResourceUsageReporterClient<T>* client) |
+ : client_(client), last_reported_usage_level_(ResourceUsageLevel::LOW) { |
+ for (int usage_level = static_cast<int>(ResourceUsageLevel::LOW); |
+ usage_level < static_cast<int>(ResourceUsageLevel::MAX_VALUE); |
+ ++usage_level) { |
+ usage_levels_[usage_level] = std::move(client_->GetRangeForUsageLevel( |
+ static_cast<ResourceUsageLevel>(usage_level))); |
+ } |
+ } |
+ |
+ ~ResourceUsageReporter() {} |
+ |
+ void ReportUsage(T usage) { |
+ int usage_level = static_cast<int>(last_reported_usage_level_); |
+ // TODO(fsamuel): This could be an infinite loop if the ranges are not |
+ // monotonically increasing in high value and low value. |
+ while (!usage_levels_[usage_level]->Contains(usage)) { |
+ if (usage_levels_[usage_level]->IsLowerThan(usage)) { |
+ ++usage_level; |
+ } else { |
+ --usage_level; |
+ } |
+ } |
+ if (static_cast<int>(last_reported_usage_level_) != usage_level) { |
+ last_reported_usage_level_ = static_cast<ResourceUsageLevel>(usage_level); |
+ client_->OnResourceUsageLevelChanged(last_reported_usage_level_); |
+ } |
+ } |
+ |
+ private: |
+ ResourceUsageReporterClient<T>* const client_; |
+ scoped_ptr<ResourceUsageRange<T>> |
+ usage_levels_[static_cast<int>(ResourceUsageLevel::MAX_VALUE)]; |
+ ResourceUsageLevel last_reported_usage_level_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ResourceUsageReporter); |
+}; |
+ |
+} // namespace framelet |
+ |
+#endif // COMPONENTS_FRAMELET_BROWSER_RESOURCE_USAGE_REPORTER_H_ |