Revert of [Password Manager] Support <input> elements not in a <form> element (patchs...
[chromium-blink-merge.git] / cc / resources / shared_bitmap.cc
blob1ac03233dfb94e02750573723fa1aeb325db2f8d
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.
5 #include "cc/resources/shared_bitmap.h"
7 #include "base/logging.h"
8 #include "base/numerics/safe_math.h"
9 #include "base/rand_util.h"
11 namespace cc {
13 SharedBitmap::SharedBitmap(uint8* pixels, const SharedBitmapId& id)
14 : pixels_(pixels), id_(id) {
17 SharedBitmap::~SharedBitmap() {
20 // static
21 bool SharedBitmap::SizeInBytes(const gfx::Size& size, size_t* size_in_bytes) {
22 if (size.IsEmpty())
23 return false;
24 base::CheckedNumeric<size_t> s = 4;
25 s *= size.width();
26 s *= size.height();
27 if (!s.IsValid())
28 return false;
29 *size_in_bytes = s.ValueOrDie();
30 return true;
33 // static
34 size_t SharedBitmap::CheckedSizeInBytes(const gfx::Size& size) {
35 CHECK(!size.IsEmpty());
36 base::CheckedNumeric<size_t> s = 4;
37 s *= size.width();
38 s *= size.height();
39 return s.ValueOrDie();
42 // static
43 size_t SharedBitmap::UncheckedSizeInBytes(const gfx::Size& size) {
44 DCHECK(VerifySizeInBytes(size));
45 size_t s = 4;
46 s *= size.width();
47 s *= size.height();
48 return s;
51 // static
52 bool SharedBitmap::VerifySizeInBytes(const gfx::Size& size) {
53 if (size.IsEmpty())
54 return false;
55 base::CheckedNumeric<size_t> s = 4;
56 s *= size.width();
57 s *= size.height();
58 return s.IsValid();
61 // static
62 SharedBitmapId SharedBitmap::GenerateId() {
63 SharedBitmapId id;
64 // Needs cryptographically-secure random numbers.
65 base::RandBytes(id.name, sizeof(id.name));
66 return id;
69 } // namespace cc