1 // Copyright 2014 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 "third_party/blink/renderer/core/frame/device_single_window_event_controller.h"
6 
7 #include "third_party/blink/renderer/core/dom/events/event.h"
8 #include "third_party/blink/renderer/core/page/page.h"
9 #include "third_party/blink/renderer/platform/weborigin/security_origin.h"
10 
11 namespace blink {
12 
DeviceSingleWindowEventController(LocalDOMWindow & window)13 DeviceSingleWindowEventController::DeviceSingleWindowEventController(
14     LocalDOMWindow& window)
15     : PlatformEventController(window), needs_checking_null_events_(true) {
16   window.RegisterEventListenerObserver(this);
17 }
18 
19 DeviceSingleWindowEventController::~DeviceSingleWindowEventController() =
20     default;
21 
DidUpdateData()22 void DeviceSingleWindowEventController::DidUpdateData() {
23   DispatchDeviceEvent(LastEvent());
24 }
25 
DispatchDeviceEvent(Event * event)26 void DeviceSingleWindowEventController::DispatchDeviceEvent(Event* event) {
27   if (GetWindow().IsContextPaused() || GetWindow().IsContextDestroyed())
28     return;
29 
30   GetWindow().DispatchEvent(*event);
31 
32   if (needs_checking_null_events_) {
33     if (IsNullEvent(event))
34       StopUpdating();
35     else
36       needs_checking_null_events_ = false;
37   }
38 }
39 
DidAddEventListener(LocalDOMWindow * window,const AtomicString & event_type)40 void DeviceSingleWindowEventController::DidAddEventListener(
41     LocalDOMWindow* window,
42     const AtomicString& event_type) {
43   if (event_type != EventTypeName())
44     return;
45 
46   if (GetPage() && GetPage()->IsPageVisible())
47     StartUpdating();
48 
49   has_event_listener_ = true;
50 }
51 
DidRemoveEventListener(LocalDOMWindow * window,const AtomicString & event_type)52 void DeviceSingleWindowEventController::DidRemoveEventListener(
53     LocalDOMWindow* window,
54     const AtomicString& event_type) {
55   if (event_type != EventTypeName() ||
56       window->HasEventListeners(EventTypeName()))
57     return;
58 
59   StopUpdating();
60   has_event_listener_ = false;
61 }
62 
DidRemoveAllEventListeners(LocalDOMWindow *)63 void DeviceSingleWindowEventController::DidRemoveAllEventListeners(
64     LocalDOMWindow*) {
65   StopUpdating();
66   has_event_listener_ = false;
67 }
68 
IsSameSecurityOriginAsMainFrame() const69 bool DeviceSingleWindowEventController::IsSameSecurityOriginAsMainFrame()
70     const {
71   LocalFrame* frame = GetWindow().GetFrame();
72   if (!frame)
73     return false;
74 
75   if (frame->IsMainFrame())
76     return true;
77 
78   const SecurityOrigin* main_security_origin =
79       frame->GetPage()->MainFrame()->GetSecurityContext()->GetSecurityOrigin();
80 
81   if (main_security_origin &&
82       GetWindow().GetSecurityOrigin()->CanAccess(main_security_origin))
83     return true;
84 
85   return false;
86 }
87 
CheckPolicyFeatures(const Vector<mojom::blink::FeaturePolicyFeature> & features) const88 bool DeviceSingleWindowEventController::CheckPolicyFeatures(
89     const Vector<mojom::blink::FeaturePolicyFeature>& features) const {
90   const LocalDOMWindow& window = GetWindow();
91   return std::all_of(features.begin(), features.end(),
92                      [&window](mojom::blink::FeaturePolicyFeature feature) {
93                        return window.IsFeatureEnabled(
94                            feature, ReportOptions::kReportOnFailure);
95                      });
96 }
97 
Trace(Visitor * visitor) const98 void DeviceSingleWindowEventController::Trace(Visitor* visitor) const {
99   PlatformEventController::Trace(visitor);
100 }
101 
102 }  // namespace blink
103