OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 COMPONENTS_FRAMELET_BROWSER_RESOURCE_USAGE_REPORTER_H_ |
| 6 #define COMPONENTS_FRAMELET_BROWSER_RESOURCE_USAGE_REPORTER_H_ |
| 7 |
| 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "components/framelet/browser/resource_usage_range.h" |
| 11 |
| 12 namespace framelet { |
| 13 |
| 14 template <class T> |
| 15 class ResourceUsageReporterClient; |
| 16 |
| 17 enum class ResourceUsageLevel : int { |
| 18 LOW = 0, |
| 19 MEDIUM, |
| 20 HIGH, |
| 21 CRITICAL, |
| 22 MAX_VALUE |
| 23 }; |
| 24 |
| 25 template <class T> |
| 26 class ResourceUsageReporter { |
| 27 public: |
| 28 explicit ResourceUsageReporter(ResourceUsageReporterClient<T>* client) |
| 29 : client_(client), last_reported_usage_level_(ResourceUsageLevel::LOW) { |
| 30 for (int usage_level = static_cast<int>(ResourceUsageLevel::LOW); |
| 31 usage_level < static_cast<int>(ResourceUsageLevel::MAX_VALUE); |
| 32 ++usage_level) { |
| 33 usage_levels_[usage_level] = std::move(client_->GetRangeForUsageLevel( |
| 34 static_cast<ResourceUsageLevel>(usage_level))); |
| 35 } |
| 36 } |
| 37 |
| 38 ~ResourceUsageReporter() {} |
| 39 |
| 40 void ReportUsage(T usage) { |
| 41 int usage_level = static_cast<int>(last_reported_usage_level_); |
| 42 // TODO(fsamuel): This could be an infinite loop if the ranges are not |
| 43 // monotonically increasing in high value and low value. |
| 44 while (!usage_levels_[usage_level]->Contains(usage)) { |
| 45 if (usage_levels_[usage_level]->IsLowerThan(usage)) { |
| 46 ++usage_level; |
| 47 } else { |
| 48 --usage_level; |
| 49 } |
| 50 } |
| 51 if (static_cast<int>(last_reported_usage_level_) != usage_level) { |
| 52 last_reported_usage_level_ = static_cast<ResourceUsageLevel>(usage_level); |
| 53 client_->OnResourceUsageLevelChanged(last_reported_usage_level_); |
| 54 } |
| 55 } |
| 56 |
| 57 private: |
| 58 ResourceUsageReporterClient<T>* const client_; |
| 59 scoped_ptr<ResourceUsageRange<T>> |
| 60 usage_levels_[static_cast<int>(ResourceUsageLevel::MAX_VALUE)]; |
| 61 ResourceUsageLevel last_reported_usage_level_; |
| 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(ResourceUsageReporter); |
| 64 }; |
| 65 |
| 66 } // namespace framelet |
| 67 |
| 68 #endif // COMPONENTS_FRAMELET_BROWSER_RESOURCE_USAGE_REPORTER_H_ |
OLD | NEW |