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 "content/browser/vibration/vibration_message_filter.h"
9 #include "base/numerics/safe_conversions.h"
10 #include "content/common/view_messages.h"
11 #include "content/public/browser/content_browser_client.h"
12 #include "content/public/browser/vibration_provider.h"
13 #include "content/public/common/content_client.h"
14 #include "third_party/WebKit/public/platform/WebVibration.h"
18 // Minimum duration of a vibration is 1 millisecond.
19 const int64 kMinimumVibrationDurationMs = 1;
21 VibrationMessageFilter::VibrationMessageFilter()
22 : BrowserMessageFilter(ViewMsgStart) {
23 provider_.reset(GetContentClient()->browser()->OverrideVibrationProvider());
25 provider_.reset(CreateProvider());
28 VibrationMessageFilter::~VibrationMessageFilter() {
31 bool VibrationMessageFilter::OnMessageReceived(
32 const IPC::Message& message,
33 bool* message_was_ok) {
35 IPC_BEGIN_MESSAGE_MAP_EX(VibrationMessageFilter,
38 IPC_MESSAGE_HANDLER(ViewHostMsg_Vibrate, OnVibrate)
39 IPC_MESSAGE_HANDLER(ViewHostMsg_CancelVibration, OnCancelVibration)
40 IPC_MESSAGE_UNHANDLED(handled = false)
41 IPC_END_MESSAGE_MAP_EX()
45 void VibrationMessageFilter::OnVibrate(int64 milliseconds) {
49 // Though the Blink implementation already sanitizes vibration times, don't
50 // trust any values passed from the renderer.
51 milliseconds = std::max(kMinimumVibrationDurationMs, std::min(milliseconds,
52 base::checked_cast<int64>(blink::kVibrationDurationMax)));
54 provider_->Vibrate(milliseconds);
57 void VibrationMessageFilter::OnCancelVibration() {
61 provider_->CancelVibration();
64 #if !defined(OS_ANDROID)
66 VibrationProvider* VibrationMessageFilter::CreateProvider() {
70 } // namespace content