extensions: EventRouter: pass the whole event to WillDispatchCallback
[chromium-blink-merge.git] / ui / base / text / bytes_formatting.cc
blob7f98c6b6e533271c197c12ea977a1df251f9e423
1 // Copyright (c) 2011 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 "ui/base/text/bytes_formatting.h"
7 #include "base/i18n/number_formatting.h"
8 #include "base/logging.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "ui/base/l10n/l10n_util.h"
12 #include "ui/strings/grit/ui_strings.h"
14 namespace ui {
16 namespace {
18 // Byte suffix string constants. These both must match the DataUnits enum.
19 const int kByteStrings[] = {
20 IDS_APP_BYTES,
21 IDS_APP_KIBIBYTES,
22 IDS_APP_MEBIBYTES,
23 IDS_APP_GIBIBYTES,
24 IDS_APP_TEBIBYTES,
25 IDS_APP_PEBIBYTES
28 const int kSpeedStrings[] = {
29 IDS_APP_BYTES_PER_SECOND,
30 IDS_APP_KIBIBYTES_PER_SECOND,
31 IDS_APP_MEBIBYTES_PER_SECOND,
32 IDS_APP_GIBIBYTES_PER_SECOND,
33 IDS_APP_TEBIBYTES_PER_SECOND,
34 IDS_APP_PEBIBYTES_PER_SECOND
37 base::string16 FormatBytesInternal(int64 bytes,
38 DataUnits units,
39 bool show_units,
40 const int* const suffix) {
41 DCHECK(units >= DATA_UNITS_BYTE && units <= DATA_UNITS_PEBIBYTE);
42 if (bytes < 0) {
43 NOTREACHED() << "Negative bytes value";
44 return base::string16();
47 // Put the quantity in the right units.
48 double unit_amount = static_cast<double>(bytes);
49 for (int i = 0; i < units; ++i)
50 unit_amount /= 1024.0;
52 int fractional_digits = 0;
53 if (bytes != 0 && units != DATA_UNITS_BYTE && unit_amount < 100)
54 fractional_digits = 1;
56 base::string16 result = base::FormatDouble(unit_amount, fractional_digits);
58 if (show_units)
59 result = l10n_util::GetStringFUTF16(suffix[units], result);
61 return result;
64 } // namespace
66 DataUnits GetByteDisplayUnits(int64 bytes) {
67 // The byte thresholds at which we display amounts. A byte count is displayed
68 // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1].
69 // This must match the DataUnits enum.
70 static const int64 kUnitThresholds[] = {
71 0, // DATA_UNITS_BYTE,
72 3 * (1LL << 10), // DATA_UNITS_KIBIBYTE,
73 2 * (1LL << 20), // DATA_UNITS_MEBIBYTE,
74 1LL << 30, // DATA_UNITS_GIBIBYTE,
75 1LL << 40, // DATA_UNITS_TEBIBYTE,
76 1LL << 50 // DATA_UNITS_PEBIBYTE,
79 if (bytes < 0) {
80 NOTREACHED() << "Negative bytes value";
81 return DATA_UNITS_BYTE;
84 int unit_index = arraysize(kUnitThresholds);
85 while (--unit_index > 0) {
86 if (bytes >= kUnitThresholds[unit_index])
87 break;
90 DCHECK(unit_index >= DATA_UNITS_BYTE && unit_index <= DATA_UNITS_PEBIBYTE);
91 return DataUnits(unit_index);
94 base::string16 FormatBytesWithUnits(int64 bytes,
95 DataUnits units,
96 bool show_units) {
97 return FormatBytesInternal(bytes, units, show_units, kByteStrings);
100 base::string16 FormatSpeedWithUnits(int64 bytes,
101 DataUnits units,
102 bool show_units) {
103 return FormatBytesInternal(bytes, units, show_units, kSpeedStrings);
106 base::string16 FormatBytes(int64 bytes) {
107 return FormatBytesWithUnits(bytes, GetByteDisplayUnits(bytes), true);
110 base::string16 FormatSpeed(int64 bytes) {
111 return FormatSpeedWithUnits(bytes, GetByteDisplayUnits(bytes), true);
114 } // namespace ui