Implement keycode text conversion functions for Ozone.
[chromium-blink-merge.git] / net / quic / quic_bandwidth.cc
blobb42a1f37e2e453c200bef87c30395b596c170c1d
1 // Copyright (c) 2012 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.
5 #include "net/quic/quic_bandwidth.h"
7 #include "base/logging.h"
8 #include "net/quic/quic_time.h"
9 #include "net/quic/quic_types.h"
11 namespace net {
13 // Highest number that QuicBandwidth can hold.
14 const int64 kQuicInfiniteBandwidth = GG_INT64_C(0x7fffffffffffffff);
16 // static
17 QuicBandwidth QuicBandwidth::Zero() {
18 return QuicBandwidth(0);
21 // static
22 QuicBandwidth QuicBandwidth::FromBitsPerSecond(int64 bits_per_second) {
23 return QuicBandwidth(bits_per_second);
26 // static
27 QuicBandwidth QuicBandwidth::FromKBitsPerSecond(int64 k_bits_per_second) {
28 DCHECK(k_bits_per_second < kQuicInfiniteBandwidth / 1000);
29 return QuicBandwidth(k_bits_per_second * 1000);
32 // static
33 QuicBandwidth QuicBandwidth::FromBytesPerSecond(int64 bytes_per_second) {
34 DCHECK(bytes_per_second < kQuicInfiniteBandwidth / 8);
35 return QuicBandwidth(bytes_per_second * 8);
38 // static
39 QuicBandwidth QuicBandwidth::FromKBytesPerSecond(int64 k_bytes_per_second) {
40 DCHECK(k_bytes_per_second < kQuicInfiniteBandwidth / 8000);
41 return QuicBandwidth(k_bytes_per_second * 8000);
44 // static
45 QuicBandwidth QuicBandwidth::FromBytesAndTimeDelta(QuicByteCount bytes,
46 QuicTime::Delta delta) {
47 DCHECK_LT(bytes, static_cast<uint64>(kQuicInfiniteBandwidth /
48 (8 * kNumMicrosPerSecond)));
49 int64 bytes_per_second =
50 (bytes * kNumMicrosPerSecond) / delta.ToMicroseconds();
51 return QuicBandwidth(bytes_per_second * 8);
54 QuicBandwidth::QuicBandwidth(int64 bits_per_second)
55 : bits_per_second_(bits_per_second) {
56 DCHECK_GE(bits_per_second, 0);
59 int64 QuicBandwidth::ToBitsPerSecond() const {
60 return bits_per_second_;
63 int64 QuicBandwidth::ToKBitsPerSecond() const {
64 return bits_per_second_ / 1000;
67 int64 QuicBandwidth::ToBytesPerSecond() const {
68 return bits_per_second_ / 8;
71 int64 QuicBandwidth::ToKBytesPerSecond() const {
72 return bits_per_second_ / 8000;
75 QuicByteCount QuicBandwidth::ToBytesPerPeriod(
76 QuicTime::Delta time_period) const {
77 return ToBytesPerSecond() * time_period.ToMicroseconds() /
78 kNumMicrosPerSecond;
81 int64 QuicBandwidth::ToKBytesPerPeriod(QuicTime::Delta time_period) const {
82 return ToKBytesPerSecond() * time_period.ToMicroseconds() /
83 kNumMicrosPerSecond;
86 bool QuicBandwidth::IsZero() const {
87 return (bits_per_second_ == 0);
90 QuicBandwidth QuicBandwidth::Add(const QuicBandwidth& delta) const {
91 return QuicBandwidth(bits_per_second_ + delta.bits_per_second_);
94 QuicBandwidth QuicBandwidth::Subtract(const QuicBandwidth& delta) const {
95 return QuicBandwidth(bits_per_second_ - delta.bits_per_second_);
98 QuicBandwidth QuicBandwidth::Scale(float scale_factor) const {
99 return QuicBandwidth(static_cast<int64>(bits_per_second_ * scale_factor));
102 QuicTime::Delta QuicBandwidth::TransferTime(QuicByteCount bytes) const {
103 if (bits_per_second_ == 0) {
104 return QuicTime::Delta::Zero();
106 return QuicTime::Delta::FromMicroseconds(bytes * 8 * kNumMicrosPerSecond /
107 bits_per_second_);
110 } // namespace net