- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / vibration / vibration_message_filter.cc
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.
4
5 #include "content/browser/vibration/vibration_message_filter.h"
6
7 #include <algorithm>
8
9 #include "base/safe_numerics.h"
10 #include "content/common/view_messages.h"
11 #include "content/port/browser/vibration_provider.h"
12 #include "content/public/browser/content_browser_client.h"
13 #include "content/public/common/content_client.h"
14 #include "third_party/WebKit/public/platform/WebVibration.h"
15
16 namespace content {
17
18 // Minimum duration of a vibration is 1 millisecond.
19 const int64 kMinimumVibrationDurationMs = 1;
20
21 VibrationMessageFilter::VibrationMessageFilter() {
22   provider_.reset(GetContentClient()->browser()->OverrideVibrationProvider());
23   if (!provider_.get())
24     provider_.reset(CreateProvider());
25 }
26
27 VibrationMessageFilter::~VibrationMessageFilter() {
28 }
29
30 bool VibrationMessageFilter::OnMessageReceived(
31     const IPC::Message& message,
32     bool* message_was_ok) {
33   bool handled = true;
34   IPC_BEGIN_MESSAGE_MAP_EX(VibrationMessageFilter,
35                            message,
36                            *message_was_ok)
37     IPC_MESSAGE_HANDLER(ViewHostMsg_Vibrate, OnVibrate)
38     IPC_MESSAGE_HANDLER(ViewHostMsg_CancelVibration, OnCancelVibration)
39     IPC_MESSAGE_UNHANDLED(handled = false)
40   IPC_END_MESSAGE_MAP_EX()
41   return handled;
42 }
43
44 void VibrationMessageFilter::OnVibrate(int64 milliseconds) {
45   if (!provider_.get())
46     return;
47
48   // Though the Blink implementation already sanitizes vibration times, don't
49   // trust any values passed from the renderer.
50   milliseconds = std::max(kMinimumVibrationDurationMs, std::min(milliseconds,
51           base::checked_numeric_cast<int64>(WebKit::kVibrationDurationMax)));
52
53   provider_->Vibrate(milliseconds);
54 }
55
56 void VibrationMessageFilter::OnCancelVibration() {
57   if (!provider_.get())
58     return;
59
60   provider_->CancelVibration();
61 }
62
63 #if !defined(OS_ANDROID) && !defined(OS_TIZEN_MOBILE)
64 // static
65 VibrationProvider* VibrationMessageFilter::CreateProvider() {
66   return NULL;
67 }
68 #endif
69 }  // namespace content