App windows launched in windows ASH should not be messing with the AppUserModelId...
[chromium-blink-merge.git] / mojo / system / options_validation.h
blob3bb0d471ef5a2849fd53557bdc0bbd193ef4aa2b
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.
5 // Functions to help with verifying various |Mojo...Options| structs from the
6 // (public, C) API. These are "extensible" structs, which all have |struct_size|
7 // as their first member. All fields (other than |struct_size|) are optional,
8 // but any |flags| specified must be known to the system (otherwise, an error of
9 // |MOJO_RESULT_UNIMPLEMENTED| should be returned).
11 #ifndef MOJO_SYSTEM_OPTIONS_VALIDATION_H_
12 #define MOJO_SYSTEM_OPTIONS_VALIDATION_H_
14 #include <stddef.h>
15 #include <stdint.h>
17 #include <algorithm>
19 #include "base/logging.h"
20 #include "base/macros.h"
21 #include "mojo/public/c/system/types.h"
22 #include "mojo/system/constants.h"
23 #include "mojo/system/memory.h"
24 #include "mojo/system/system_impl_export.h"
26 namespace mojo {
27 namespace system {
29 template <class Options>
30 class UserOptionsReader {
31 public:
32 // Constructor from a |UserPointer<const Options>| (which it checks -- this
33 // constructor has side effects!).
34 // Note: We initialize |options_reader_| without checking, since we do a check
35 // in |GetSizeForReader()|.
36 explicit UserOptionsReader(UserPointer<const Options> options)
37 : options_reader_(UserPointer<const char>::Reader::NoCheck(),
38 options.template ReinterpretCast<const char>(),
39 GetSizeForReader(options)) {
40 COMPILE_ASSERT(offsetof(Options, struct_size) == 0,
41 Options_struct_size_not_first_member);
42 // TODO(vtl): With C++11, compile-assert that |sizeof(Options::struct_size)
43 // == sizeof(uint32_t)| somewhere.
46 bool is_valid() const { return !!options_reader_.GetPointer(); }
48 const Options& options() const {
49 DCHECK(is_valid());
50 return *reinterpret_cast<const Options*>(options_reader_.GetPointer());
53 // Checks that the given (variable-size) |options| passed to the constructor
54 // (plausibly) has a member at the given offset with the given size. You
55 // probably want to use |OPTIONS_STRUCT_HAS_MEMBER()| instead.
56 bool HasMember(size_t offset, size_t size) const {
57 DCHECK(is_valid());
58 // We assume that |offset| and |size| are reasonable, since they should come
59 // from |offsetof(Options, some_member)| and |sizeof(Options::some_member)|,
60 // respectively.
61 return options().struct_size >= offset + size;
64 private:
65 static inline size_t GetSizeForReader(UserPointer<const Options> options) {
66 uint32_t struct_size =
67 options.template ReinterpretCast<const uint32_t>().Get();
68 if (struct_size < sizeof(uint32_t))
69 return 0;
71 // Check the full requested size.
72 // Note: Use |MOJO_ALIGNOF()| here to match the exact macro used in the
73 // declaration of Options structs.
74 internal::CheckUserPointerWithSize<MOJO_ALIGNOF(Options)>(options.pointer_,
75 struct_size);
76 options.template ReinterpretCast<const char>().CheckArray(struct_size);
77 // But we'll never look at more than |sizeof(Options)| bytes.
78 return std::min(static_cast<size_t>(struct_size), sizeof(Options));
81 UserPointer<const char>::Reader options_reader_;
83 DISALLOW_COPY_AND_ASSIGN(UserOptionsReader);
86 // Macro to invoke |UserOptionsReader<Options>::HasMember()| parametrized by
87 // member name instead of offset and size.
89 // (We can't just give |HasMember()| a member pointer template argument instead,
90 // since there's no good/strictly-correct way to get an offset from that.)
92 // TODO(vtl): With C++11, use |sizeof(Options::member)| instead of (the
93 // contortion below). We might also be able to pull out the type |Options| from
94 // |reader| (using |decltype|) instead of requiring a parameter.
95 #define OPTIONS_STRUCT_HAS_MEMBER(Options, member, reader) \
96 reader.HasMember(offsetof(Options, member), sizeof(reader.options().member))
98 } // namespace system
99 } // namespace mojo
101 #endif // MOJO_SYSTEM_OPTIONS_VALIDATION_H_