1 : // Copyright 2014 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/kasko/waitable_timer_impl.h"
16 :
17 : #include <windows.h>
18 :
19 : #include "base/time/time.h"
20 : #include "syzygy/common/com_utils.h"
21 :
22 : namespace kasko {
23 :
24 : // static
25 : scoped_ptr<WaitableTimer> WaitableTimerImpl::Create(
26 E : const base::TimeDelta& interval) {
27 E : scoped_ptr<WaitableTimer> result;
28 E : base::win::ScopedHandle handle(::CreateWaitableTimer(NULL, TRUE, NULL));
29 E : if (handle.IsValid())
30 E : result.reset(new WaitableTimerImpl(handle.Pass(), interval));
31 E : return result.Pass();
32 E : }
33 :
34 E : WaitableTimerImpl::~WaitableTimerImpl() {}
35 :
36 E : void WaitableTimerImpl::Start() {
37 E : if (!::SetWaitableTimer(handle_.Get(), &interval_, 0, NULL, NULL, FALSE))
38 i : LOG(ERROR) << "Unexpected failure to set a timer: " << ::common::LogWe();
39 E : }
40 :
41 E : HANDLE WaitableTimerImpl::GetHANDLE() {
42 E : return handle_.Get();
43 E : }
44 :
45 : WaitableTimerImpl::WaitableTimerImpl(base::win::ScopedHandle handle,
46 : const base::TimeDelta& interval)
47 E : : handle_(handle.Take()), interval_() {
48 : interval_.QuadPart = (-interval.InMicroseconds()) *
49 E : (base::Time::kNanosecondsPerMicrosecond / 100);
50 E : }
51 :
52 : } // namespace kasko
|