summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/picture_in_picture/html_video_element_picture_in_picture.cc
blob: a07678ca9c54b74cffcbfc934ead11d9d08c9dbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/picture_in_picture/html_video_element_picture_in_picture.h"

#include "third_party/blink/public/common/picture_in_picture/picture_in_picture_control_info.h"
#include "third_party/blink/public/platform/web_icon_sizes_parser.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/dom/events/event.h"
#include "third_party/blink/renderer/core/html/media/html_video_element.h"
#include "third_party/blink/renderer/modules/picture_in_picture/picture_in_picture_control.h"
#include "third_party/blink/renderer/modules/picture_in_picture/picture_in_picture_controller_impl.h"
#include "third_party/blink/renderer/modules/picture_in_picture/picture_in_picture_window.h"

namespace blink {

using Status = PictureInPictureControllerImpl::Status;

namespace {

const char kDetachedError[] =
    "The element is no longer associated with a document.";
const char kMetadataNotLoadedError[] =
    "Metadata for the video element are not loaded yet.";
const char kVideoTrackNotAvailableError[] =
    "The video element has no video track.";
const char kFeaturePolicyBlocked[] =
    "Access to the feature \"picture-in-picture\" is disallowed by feature "
    "policy.";
const char kNotAvailable[] = "Picture-in-Picture is not available.";
const char kUserGestureRequired[] =
    "Must be handling a user gesture to request picture in picture.";
const char kDisablePictureInPicturePresent[] =
    "\"disablePictureInPicture\" attribute is present.";
}  // namespace

// static
ScriptPromise HTMLVideoElementPictureInPicture::requestPictureInPicture(
    ScriptState* script_state,
    HTMLVideoElement& element) {
  Document& document = element.GetDocument();
  PictureInPictureControllerImpl& controller =
      PictureInPictureControllerImpl::From(document);

  switch (controller.IsElementAllowed(element)) {
    case Status::kFrameDetached:
      return ScriptPromise::RejectWithDOMException(
          script_state,
          DOMException::Create(DOMExceptionCode::kInvalidStateError,
                               kDetachedError));
    case Status::kMetadataNotLoaded:
      return ScriptPromise::RejectWithDOMException(
          script_state,
          DOMException::Create(DOMExceptionCode::kInvalidStateError,
                               kMetadataNotLoadedError));
    case Status::kVideoTrackNotAvailable:
      return ScriptPromise::RejectWithDOMException(
          script_state,
          DOMException::Create(DOMExceptionCode::kInvalidStateError,
                               kVideoTrackNotAvailableError));
    case Status::kDisabledByFeaturePolicy:
      return ScriptPromise::RejectWithDOMException(
          script_state, DOMException::Create(DOMExceptionCode::kSecurityError,
                                             kFeaturePolicyBlocked));
    case Status::kDisabledByAttribute:
      return ScriptPromise::RejectWithDOMException(
          script_state,
          DOMException::Create(DOMExceptionCode::kInvalidStateError,
                               kDisablePictureInPicturePresent));
    case Status::kDisabledBySystem:
      return ScriptPromise::RejectWithDOMException(
          script_state,
          DOMException::Create(DOMExceptionCode::kNotSupportedError,
                               kNotAvailable));
    case Status::kEnabled:
      break;
  }

  // Frame is not null, otherwise `IsElementAllowed()` would have return
  // `kFrameDetached`.
  LocalFrame* frame = element.GetFrame();
  DCHECK(frame);
  if (!LocalFrame::ConsumeTransientUserActivation(frame)) {
    return ScriptPromise::RejectWithDOMException(
        script_state, DOMException::Create(DOMExceptionCode::kNotAllowedError,
                                           kUserGestureRequired));
  }

  ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state);
  ScriptPromise promise = resolver->Promise();

  document.GetTaskRunner(TaskType::kMediaElementEvent)
      ->PostTask(
          FROM_HERE,
          WTF::Bind(&PictureInPictureControllerImpl::EnterPictureInPicture,
                    WrapPersistent(&controller), WrapPersistent(&element),
                    WrapPersistent(resolver)));

  return promise;
}

void HTMLVideoElementPictureInPicture::setPictureInPictureControls(
    HTMLVideoElement& element,
    const HeapVector<PictureInPictureControl>& controls) {
  Document& document = element.GetDocument();

  PictureInPictureControllerImpl& controller =
      PictureInPictureControllerImpl::From(document);

  controller.SetPictureInPictureCustomControls(
      &element, ToPictureInPictureControlInfoVector(controls));
}

// static
bool HTMLVideoElementPictureInPicture::FastHasAttribute(
    const QualifiedName& name,
    const HTMLVideoElement& element) {
  DCHECK(name == HTMLNames::disablepictureinpictureAttr);
  return element.FastHasAttribute(name);
}

// static
void HTMLVideoElementPictureInPicture::SetBooleanAttribute(
    const QualifiedName& name,
    HTMLVideoElement& element,
    bool value) {
  DCHECK(name == HTMLNames::disablepictureinpictureAttr);
  element.SetBooleanAttribute(name, value);

  if (!value)
    return;

  Document& document = element.GetDocument();
  TreeScope& scope = element.GetTreeScope();
  PictureInPictureControllerImpl& controller =
      PictureInPictureControllerImpl::From(document);
  if (controller.PictureInPictureElement(scope) == &element) {
    controller.ExitPictureInPicture(&element, nullptr);
  }
}

// static
std::vector<PictureInPictureControlInfo>
HTMLVideoElementPictureInPicture::ToPictureInPictureControlInfoVector(
    const HeapVector<PictureInPictureControl>& controls) {
  std::vector<PictureInPictureControlInfo> converted_controls;
  for (size_t i = 0; i < controls.size(); ++i) {
    PictureInPictureControlInfo current_converted_control;
    HeapVector<MediaImage> current_icons = controls[i].icons();

    // Only two icons are supported, so cap the loop at running that many times
    // to avoid potential problems.
    for (size_t j = 0; j < current_icons.size() && j < 2; ++j) {
      PictureInPictureControlInfo::Icon current_icon;
      current_icon.src = KURL(WebString(current_icons[j].src()));

      WebVector<WebSize> sizes = WebIconSizesParser::ParseIconSizes(
          WebString(current_icons[j].sizes()));
      std::vector<gfx::Size> converted_sizes;
      for (size_t i = 0; i < sizes.size(); ++i)
        converted_sizes.push_back(static_cast<gfx::Size>(sizes[i]));

      current_icon.sizes = converted_sizes;
      current_icon.type = WebString(current_icons[j].type()).Utf8();
      current_converted_control.icons.push_back(current_icon);
    }

    current_converted_control.id = WebString(controls[i].id()).Utf8();
    current_converted_control.label = WebString(controls[i].label()).Utf8();
    converted_controls.push_back(current_converted_control);
  }
  return converted_controls;
}

}  // namespace blink