Commit ea4d661f authored by Nate Chapin's avatar Nate Chapin Committed by Commit Bot

Merge NavigatorVibration into VibrationController

Change-Id: I5dbcb6c06c98862facd34376c8b43abe557f4b9d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2493375Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Commit-Queue: Nate Chapin <japhet@chromium.org>
Cr-Commit-Position: refs/heads/master@{#820250}
parent e9ad4df5
...@@ -316,8 +316,8 @@ String Notification::badge() const { ...@@ -316,8 +316,8 @@ String Notification::badge() const {
return data_->badge.GetString(); return data_->badge.GetString();
} }
NavigatorVibration::VibrationPattern Notification::vibrate() const { VibrationController::VibrationPattern Notification::vibrate() const {
NavigatorVibration::VibrationPattern pattern; VibrationController::VibrationPattern pattern;
if (data_->vibration_pattern.has_value()) { if (data_->vibration_pattern.has_value()) {
pattern.AppendRange(data_->vibration_pattern->begin(), pattern.AppendRange(data_->vibration_pattern->begin(),
data_->vibration_pattern->end()); data_->vibration_pattern->end());
......
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
#include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h" #include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
#include "third_party/blink/renderer/modules/event_target_modules.h" #include "third_party/blink/renderer/modules/event_target_modules.h"
#include "third_party/blink/renderer/modules/modules_export.h" #include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/modules/vibration/navigator_vibration.h" #include "third_party/blink/renderer/modules/vibration/vibration_controller.h"
#include "third_party/blink/renderer/platform/heap/handle.h" #include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_receiver.h" #include "third_party/blink/renderer/platform/mojo/heap_mojo_receiver.h"
#include "third_party/blink/renderer/platform/timer.h" #include "third_party/blink/renderer/platform/timer.h"
...@@ -110,7 +110,7 @@ class MODULES_EXPORT Notification final ...@@ -110,7 +110,7 @@ class MODULES_EXPORT Notification final
String image() const; String image() const;
String icon() const; String icon() const;
String badge() const; String badge() const;
NavigatorVibration::VibrationPattern vibrate() const; VibrationController::VibrationPattern vibrate() const;
DOMTimeStamp timestamp() const; DOMTimeStamp timestamp() const;
bool renotify() const; bool renotify() const;
bool silent() const; bool silent() const;
......
...@@ -6,8 +6,6 @@ import("//third_party/blink/renderer/modules/modules.gni") ...@@ -6,8 +6,6 @@ import("//third_party/blink/renderer/modules/modules.gni")
blink_modules_sources("vibration") { blink_modules_sources("vibration") {
sources = [ sources = [
"navigator_vibration.cc",
"navigator_vibration.h",
"vibration_controller.cc", "vibration_controller.cc",
"vibration_controller.h", "vibration_controller.h",
] ]
......
/*
* Copyright (C) 2012 Samsung Electronics
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "third_party/blink/renderer/modules/vibration/navigator_vibration.h"
#include "base/metrics/histogram_functions.h"
#include "third_party/blink/renderer/core/frame/deprecation.h"
#include "third_party/blink/renderer/core/frame/frame_console.h"
#include "third_party/blink/renderer/core/frame/intervention.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/navigator.h"
#include "third_party/blink/renderer/core/inspector/console_message.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/modules/vibration/vibration_controller.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
namespace blink {
NavigatorVibration::NavigatorVibration(Navigator& navigator)
: ExecutionContextLifecycleObserver(navigator.DomWindow()) {}
NavigatorVibration::~NavigatorVibration() = default;
// static
NavigatorVibration& NavigatorVibration::From(Navigator& navigator) {
NavigatorVibration* navigator_vibration =
Supplement<Navigator>::From<NavigatorVibration>(navigator);
if (!navigator_vibration) {
navigator_vibration = MakeGarbageCollected<NavigatorVibration>(navigator);
ProvideTo(navigator, navigator_vibration);
}
return *navigator_vibration;
}
// static
const char NavigatorVibration::kSupplementName[] = "NavigatorVibration";
// static
bool NavigatorVibration::vibrate(Navigator& navigator, unsigned time) {
VibrationPattern pattern;
pattern.push_back(time);
return NavigatorVibration::vibrate(navigator, pattern);
}
// static
bool NavigatorVibration::vibrate(Navigator& navigator,
const VibrationPattern& pattern) {
LocalFrame* frame = navigator.GetFrame();
// There will be no frame if the window has been closed, but a JavaScript
// reference to |window| or |navigator| was retained in another window.
if (!frame)
return false;
CollectHistogramMetrics(navigator);
DCHECK(frame->DomWindow());
DCHECK(frame->GetPage());
if (!frame->GetPage()->IsPageVisible())
return false;
if (!frame->HasStickyUserActivation()) {
String message;
if (frame->IsCrossOriginToMainFrame()) {
message =
"Blocked call to navigator.vibrate inside a cross-origin "
"iframe because the frame has never been activated by the user: "
"https://www.chromestatus.com/feature/5682658461876224.";
} else {
message =
"Blocked call to navigator.vibrate because user hasn't tapped "
"on the frame or any embedded frame yet: "
"https://www.chromestatus.com/feature/5644273861001216.";
}
Intervention::GenerateReport(frame, "NavigatorVibrate", message);
return false;
}
return NavigatorVibration::From(navigator).Controller(*frame)->Vibrate(
pattern);
}
// static
void NavigatorVibration::CollectHistogramMetrics(const Navigator& navigator) {
NavigatorVibrationType type;
LocalFrame* frame = navigator.GetFrame();
bool user_gesture = frame->HasStickyUserActivation();
UseCounter::Count(navigator.DomWindow(), WebFeature::kNavigatorVibrate);
if (!frame->IsMainFrame()) {
UseCounter::Count(navigator.DomWindow(),
WebFeature::kNavigatorVibrateSubFrame);
if (frame->IsCrossOriginToMainFrame()) {
if (user_gesture)
type = NavigatorVibrationType::kCrossOriginSubFrameWithUserGesture;
else
type = NavigatorVibrationType::kCrossOriginSubFrameNoUserGesture;
} else {
if (user_gesture)
type = NavigatorVibrationType::kSameOriginSubFrameWithUserGesture;
else
type = NavigatorVibrationType::kSameOriginSubFrameNoUserGesture;
}
} else {
if (user_gesture)
type = NavigatorVibrationType::kMainFrameWithUserGesture;
else
type = NavigatorVibrationType::kMainFrameNoUserGesture;
}
base::UmaHistogramEnumeration("Vibration.Context", type);
}
VibrationController* NavigatorVibration::Controller(LocalFrame& frame) {
if (!controller_)
controller_ = MakeGarbageCollected<VibrationController>(frame);
return controller_.Get();
}
void NavigatorVibration::ContextDestroyed() {
if (controller_) {
controller_->Cancel();
controller_ = nullptr;
}
}
void NavigatorVibration::Trace(Visitor* visitor) const {
visitor->Trace(controller_);
Supplement<Navigator>::Trace(visitor);
ExecutionContextLifecycleObserver::Trace(visitor);
}
} // namespace blink
/*
* Copyright (C) 2012 Samsung Electronics
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_VIBRATION_NAVIGATOR_VIBRATION_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_VIBRATION_NAVIGATOR_VIBRATION_H_
#include "base/macros.h"
#include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
#include "third_party/blink/renderer/core/frame/navigator.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/supplementable.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"
namespace blink {
class LocalFrame;
class Navigator;
class VibrationController;
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class NavigatorVibrationType {
kMainFrameNoUserGesture = 0,
kMainFrameWithUserGesture = 1,
kSameOriginSubFrameNoUserGesture = 2,
kSameOriginSubFrameWithUserGesture = 3,
kCrossOriginSubFrameNoUserGesture = 4,
kCrossOriginSubFrameWithUserGesture = 5,
kMaxValue = kCrossOriginSubFrameWithUserGesture,
};
class MODULES_EXPORT NavigatorVibration final
: public GarbageCollected<NavigatorVibration>,
public Supplement<Navigator>,
public ExecutionContextLifecycleObserver {
public:
static const char kSupplementName[];
using VibrationPattern = Vector<unsigned>;
explicit NavigatorVibration(Navigator&);
virtual ~NavigatorVibration();
static NavigatorVibration& From(Navigator&);
static bool vibrate(Navigator&, unsigned time);
static bool vibrate(Navigator&, const VibrationPattern&);
VibrationController* Controller(LocalFrame&);
void Trace(Visitor*) const override;
private:
// Inherited from ExecutionContextLifecycleObserver.
void ContextDestroyed() override;
static void CollectHistogramMetrics(const Navigator&);
Member<VibrationController> controller_;
DISALLOW_COPY_AND_ASSIGN(NavigatorVibration);
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_VIBRATION_NAVIGATOR_VIBRATION_H_
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
// https://w3c.github.io/vibration/#vibration-interface // https://w3c.github.io/vibration/#vibration-interface
[ [
ImplementedAs=NavigatorVibration ImplementedAs=VibrationController
] partial interface Navigator { ] partial interface Navigator {
// FIXME: should be union type https://crbug.com/240176 // FIXME: should be union type https://crbug.com/240176
// FIXME: The contents of the pattern argument should be clamped. // FIXME: The contents of the pattern argument should be clamped.
......
...@@ -32,25 +32,20 @@ ...@@ -32,25 +32,20 @@
#include "third_party/blink/renderer/core/frame/navigator.h" #include "third_party/blink/renderer/core/frame/navigator.h"
#include "third_party/blink/renderer/core/testing/internals.h" #include "third_party/blink/renderer/core/testing/internals.h"
#include "third_party/blink/renderer/modules/vibration/navigator_vibration.h"
#include "third_party/blink/renderer/modules/vibration/vibration_controller.h" #include "third_party/blink/renderer/modules/vibration/vibration_controller.h"
namespace blink { namespace blink {
bool InternalsVibration::isVibrating(Internals&, Navigator* navigator) { bool InternalsVibration::isVibrating(Internals&, Navigator* navigator) {
DCHECK(navigator && navigator->GetFrame()); DCHECK(navigator && navigator->DomWindow());
return NavigatorVibration::From(*navigator) return VibrationController::From(*navigator).IsRunning();
.Controller(*navigator->GetFrame())
->IsRunning();
} }
Vector<unsigned> InternalsVibration::pendingVibrationPattern( Vector<unsigned> InternalsVibration::pendingVibrationPattern(
Internals&, Internals&,
Navigator* navigator) { Navigator* navigator) {
DCHECK(navigator && navigator->GetFrame()); DCHECK(navigator && navigator->DomWindow());
return NavigatorVibration::From(*navigator) return VibrationController::From(*navigator).Pattern();
.Controller(*navigator->GetFrame())
->Pattern();
} }
} // namespace blink } // namespace blink
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "third_party/blink/public/platform/platform.h" #include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/task_type.h" #include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/renderer/bindings/modules/v8/unsigned_long_or_unsigned_long_sequence.h" #include "third_party/blink/renderer/bindings/modules/v8/unsigned_long_or_unsigned_long_sequence.h"
#include "third_party/blink/renderer/core/frame/intervention.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h" #include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h" #include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/navigator.h" #include "third_party/blink/renderer/core/frame/navigator.h"
...@@ -61,6 +62,44 @@ blink::VibrationController::VibrationPattern sanitizeVibrationPatternInternal( ...@@ -61,6 +62,44 @@ blink::VibrationController::VibrationPattern sanitizeVibrationPatternInternal(
namespace blink { namespace blink {
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class NavigatorVibrationType {
kMainFrameNoUserGesture = 0,
kMainFrameWithUserGesture = 1,
kSameOriginSubFrameNoUserGesture = 2,
kSameOriginSubFrameWithUserGesture = 3,
kCrossOriginSubFrameNoUserGesture = 4,
kCrossOriginSubFrameWithUserGesture = 5,
kMaxValue = kCrossOriginSubFrameWithUserGesture,
};
void CollectHistogramMetrics(LocalDOMWindow* window) {
NavigatorVibrationType type;
bool user_gesture = window->GetFrame()->HasStickyUserActivation();
UseCounter::Count(window, WebFeature::kNavigatorVibrate);
if (!window->GetFrame()->IsMainFrame()) {
UseCounter::Count(window, WebFeature::kNavigatorVibrateSubFrame);
if (window->GetFrame()->IsCrossOriginToMainFrame()) {
if (user_gesture)
type = NavigatorVibrationType::kCrossOriginSubFrameWithUserGesture;
else
type = NavigatorVibrationType::kCrossOriginSubFrameNoUserGesture;
} else {
if (user_gesture)
type = NavigatorVibrationType::kSameOriginSubFrameWithUserGesture;
else
type = NavigatorVibrationType::kSameOriginSubFrameNoUserGesture;
}
} else {
if (user_gesture)
type = NavigatorVibrationType::kMainFrameWithUserGesture;
else
type = NavigatorVibrationType::kMainFrameNoUserGesture;
}
base::UmaHistogramEnumeration("Vibration.Context", type);
}
// static // static
VibrationController::VibrationPattern VibrationController::VibrationPattern
VibrationController::SanitizeVibrationPattern( VibrationController::SanitizeVibrationPattern(
...@@ -75,24 +114,80 @@ VibrationController::SanitizeVibrationPattern( ...@@ -75,24 +114,80 @@ VibrationController::SanitizeVibrationPattern(
return sanitizeVibrationPatternInternal(pattern); return sanitizeVibrationPatternInternal(pattern);
} }
VibrationController::VibrationController(LocalFrame& frame) // static
: ExecutionContextLifecycleObserver(frame.DomWindow()), VibrationController& VibrationController::From(Navigator& navigator) {
PageVisibilityObserver(frame.GetPage()), VibrationController* vibration_controller =
vibration_manager_(frame.DomWindow()), Supplement<Navigator>::From<VibrationController>(navigator);
timer_do_vibrate_(frame.GetTaskRunner(TaskType::kMiscPlatformAPI), if (!vibration_controller) {
vibration_controller = MakeGarbageCollected<VibrationController>(navigator);
ProvideTo(navigator, vibration_controller);
}
return *vibration_controller;
}
// static
const char VibrationController::kSupplementName[] = "VibrationController";
// static
bool VibrationController::vibrate(Navigator& navigator, unsigned time) {
VibrationPattern pattern;
pattern.push_back(time);
return vibrate(navigator, pattern);
}
// static
bool VibrationController::vibrate(Navigator& navigator,
const VibrationPattern& pattern) {
// There will be no frame if the window has been closed, but a JavaScript
// reference to |window| or |navigator| was retained in another window.
if (!navigator.DomWindow())
return false;
return From(navigator).Vibrate(pattern);
}
VibrationController::VibrationController(Navigator& navigator)
: Supplement<Navigator>(navigator),
ExecutionContextLifecycleObserver(navigator.DomWindow()),
PageVisibilityObserver(DomWindow()->GetFrame()->GetPage()),
vibration_manager_(DomWindow()),
timer_do_vibrate_(DomWindow()->GetTaskRunner(TaskType::kMiscPlatformAPI),
this, this,
&VibrationController::DoVibrate), &VibrationController::DoVibrate),
is_running_(false), is_running_(false),
is_calling_cancel_(false), is_calling_cancel_(false),
is_calling_vibrate_(false) { is_calling_vibrate_(false) {
frame.GetBrowserInterfaceBroker().GetInterface( DomWindow()->GetBrowserInterfaceBroker().GetInterface(
vibration_manager_.BindNewPipeAndPassReceiver( vibration_manager_.BindNewPipeAndPassReceiver(
frame.GetTaskRunner(TaskType::kMiscPlatformAPI))); DomWindow()->GetTaskRunner(TaskType::kMiscPlatformAPI)));
} }
VibrationController::~VibrationController() = default; VibrationController::~VibrationController() = default;
bool VibrationController::Vibrate(const VibrationPattern& pattern) { bool VibrationController::Vibrate(const VibrationPattern& pattern) {
CollectHistogramMetrics(DomWindow());
LocalFrame* frame = DomWindow()->GetFrame();
if (!frame->GetPage()->IsPageVisible())
return false;
if (!frame->HasStickyUserActivation()) {
String message;
if (frame->IsCrossOriginToMainFrame()) {
message =
"Blocked call to navigator.vibrate inside a cross-origin "
"iframe because the frame has never been activated by the user: "
"https://www.chromestatus.com/feature/5682658461876224.";
} else {
message =
"Blocked call to navigator.vibrate because user hasn't tapped "
"on the frame or any embedded frame yet: "
"https://www.chromestatus.com/feature/5644273861001216.";
}
Intervention::GenerateReport(frame, "NavigatorVibrate", message);
return false;
}
// Cancel clears the stored pattern and cancels any ongoing vibration. // Cancel clears the stored pattern and cancels any ongoing vibration.
Cancel(); Cancel();
...@@ -194,6 +289,7 @@ void VibrationController::PageVisibilityChanged() { ...@@ -194,6 +289,7 @@ void VibrationController::PageVisibilityChanged() {
} }
void VibrationController::Trace(Visitor* visitor) const { void VibrationController::Trace(Visitor* visitor) const {
Supplement<Navigator>::Trace(visitor);
ExecutionContextLifecycleObserver::Trace(visitor); ExecutionContextLifecycleObserver::Trace(visitor);
PageVisibilityObserver::Trace(visitor); PageVisibilityObserver::Trace(visitor);
visitor->Trace(vibration_manager_); visitor->Trace(vibration_manager_);
......
...@@ -28,28 +28,35 @@ ...@@ -28,28 +28,35 @@
#include "third_party/blink/renderer/platform/heap/garbage_collected.h" #include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/handle.h" #include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h" #include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
#include "third_party/blink/renderer/platform/supplementable.h"
#include "third_party/blink/renderer/platform/timer.h" #include "third_party/blink/renderer/platform/timer.h"
#include "third_party/blink/renderer/platform/wtf/vector.h" #include "third_party/blink/renderer/platform/wtf/vector.h"
namespace blink { namespace blink {
class LocalFrame; class Navigator;
class UnsignedLongOrUnsignedLongSequence; class UnsignedLongOrUnsignedLongSequence;
class MODULES_EXPORT VibrationController final class MODULES_EXPORT VibrationController final
: public GarbageCollected<VibrationController>, : public GarbageCollected<VibrationController>,
public Supplement<Navigator>,
public ExecutionContextLifecycleObserver, public ExecutionContextLifecycleObserver,
public PageVisibilityObserver { public PageVisibilityObserver {
public: public:
using VibrationPattern = Vector<unsigned>; using VibrationPattern = Vector<unsigned>;
explicit VibrationController(LocalFrame&); static const char kSupplementName[];
static VibrationController& From(Navigator&);
static bool vibrate(Navigator&, unsigned time);
static bool vibrate(Navigator&, const VibrationPattern&);
explicit VibrationController(Navigator&);
virtual ~VibrationController(); virtual ~VibrationController();
static VibrationPattern SanitizeVibrationPattern( static VibrationPattern SanitizeVibrationPattern(
const UnsignedLongOrUnsignedLongSequence&); const UnsignedLongOrUnsignedLongSequence&);
bool Vibrate(const VibrationPattern&);
void DoVibrate(TimerBase*); void DoVibrate(TimerBase*);
void DidVibrate(); void DidVibrate();
...@@ -72,6 +79,8 @@ class MODULES_EXPORT VibrationController final ...@@ -72,6 +79,8 @@ class MODULES_EXPORT VibrationController final
// Inherited from PageVisibilityObserver. // Inherited from PageVisibilityObserver.
void PageVisibilityChanged() override; void PageVisibilityChanged() override;
bool Vibrate(const VibrationPattern&);
// Remote to VibrationManager mojo interface. This is reset in // Remote to VibrationManager mojo interface. This is reset in
// |contextDestroyed| and must not be called or recreated after it is reset. // |contextDestroyed| and must not be called or recreated after it is reset.
// //
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment