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/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" 15 16namespace content { 17 18// Minimum duration of a vibration is 1 millisecond. 19const int64 kMinimumVibrationDurationMs = 1; 20 21VibrationMessageFilter::VibrationMessageFilter() 22 : BrowserMessageFilter(ViewMsgStart) { 23 provider_.reset(GetContentClient()->browser()->OverrideVibrationProvider()); 24 if (!provider_.get()) 25 provider_.reset(CreateProvider()); 26} 27 28VibrationMessageFilter::~VibrationMessageFilter() { 29} 30 31bool VibrationMessageFilter::OnMessageReceived( 32 const IPC::Message& message) { 33 bool handled = true; 34 IPC_BEGIN_MESSAGE_MAP(VibrationMessageFilter, message) 35 IPC_MESSAGE_HANDLER(ViewHostMsg_Vibrate, OnVibrate) 36 IPC_MESSAGE_HANDLER(ViewHostMsg_CancelVibration, OnCancelVibration) 37 IPC_MESSAGE_UNHANDLED(handled = false) 38 IPC_END_MESSAGE_MAP() 39 return handled; 40} 41 42void VibrationMessageFilter::OnVibrate(int64 milliseconds) { 43 if (!provider_.get()) 44 return; 45 46 // Though the Blink implementation already sanitizes vibration times, don't 47 // trust any values passed from the renderer. 48 milliseconds = std::max(kMinimumVibrationDurationMs, std::min(milliseconds, 49 base::checked_cast<int64>(blink::kVibrationDurationMax))); 50 51 provider_->Vibrate(milliseconds); 52} 53 54void VibrationMessageFilter::OnCancelVibration() { 55 if (!provider_.get()) 56 return; 57 58 provider_->CancelVibration(); 59} 60 61#if !defined(OS_ANDROID) 62// static 63VibrationProvider* VibrationMessageFilter::CreateProvider() { 64 return NULL; 65} 66#endif 67} // namespace content 68