Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / mojo / public / cpp / bindings / lib / bounds_checker.h
1 // Copyright 2014 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 MOJO_PUBLIC_CPP_BINDINGS_LIB_BOUNDS_CHECKER_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BOUNDS_CHECKER_H_
7
8 #include <stdint.h>
9
10 #include "mojo/public/cpp/system/macros.h"
11
12 namespace mojo {
13
14 class Handle;
15
16 namespace internal {
17
18 // BoundsChecker is used to validate object sizes, pointers and handle indices
19 // for payload of incoming messages.
20 class BoundsChecker {
21  public:
22   // [data, data + data_num_bytes) specifies the initial valid memory range.
23   // [0, num_handles) specifies the initial valid range of handle indices.
24   BoundsChecker(const void* data, uint32_t data_num_bytes, size_t num_handles);
25
26   ~BoundsChecker();
27
28   // Claims the specified memory range.
29   // The method succeeds if the range is valid to claim. (Please see
30   // the comments for IsValidRange().)
31   // On success, the valid memory range is shrinked to begin right after the end
32   // of the claimed range.
33   bool ClaimMemory(const void* position, uint32_t num_bytes);
34
35   // Claims the specified encoded handle (which is basically a handle index).
36   // The method succeeds if:
37   // - |encoded_handle|'s value is |kEncodedInvalidHandleValue|.
38   // - the handle is contained inside the valid range of handle indices. In this
39   // case, the valid range is shinked to begin right after the claimed handle.
40   bool ClaimHandle(const Handle& encoded_handle);
41
42   // Returns true if the specified range is not empty, and the range is
43   // contained inside the valid memory range.
44   bool IsValidRange(const void* position, uint32_t num_bytes) const;
45
46  private:
47   bool InternalIsValidRange(uintptr_t begin, uintptr_t end) const;
48
49   // [data_begin_, data_end_) is the valid memory range.
50   uintptr_t data_begin_;
51   uintptr_t data_end_;
52
53   // [handle_begin_, handle_end_) is the valid handle index range.
54   uint32_t handle_begin_;
55   uint32_t handle_end_;
56
57   MOJO_DISALLOW_COPY_AND_ASSIGN(BoundsChecker);
58 };
59
60 }  // namespace internal
61 }  // namespace mojo
62
63 #endif  // MOJO_PUBLIC_CPP_BINDINGS_LIB_BOUNDS_CHECKER_H_