File: screen_orientation_provider.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (102 lines) | stat: -rw-r--r-- 4,046 bytes parent folder | download | duplicates (6)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_SCREEN_ORIENTATION_SCREEN_ORIENTATION_PROVIDER_H_
#define CONTENT_BROWSER_SCREEN_ORIENTATION_SCREEN_ORIENTATION_PROVIDER_H_

#include <optional>

#include "base/functional/callback.h"
#include "content/common/content_export.h"
#include "content/public/browser/render_frame_host_receiver_set.h"
#include "content/public/browser/web_contents_observer.h"
#include "services/device/public/mojom/screen_orientation.mojom.h"
#include "services/device/public/mojom/screen_orientation_lock_types.mojom.h"
#include "ui/display/mojom/screen_orientation.mojom.h"

namespace content {

class ScreenOrientationDelegate;
class WebContents;

// Handles screen orientation lock/unlock. Platforms which wish to provide
// custom implementations can provide a factory for ScreenOrientationDelegate.
class CONTENT_EXPORT ScreenOrientationProvider
    : public device::mojom::ScreenOrientation,
      public WebContentsObserver {
 public:
  ScreenOrientationProvider(WebContents* web_contents);

  ScreenOrientationProvider(const ScreenOrientationProvider&) = delete;
  ScreenOrientationProvider& operator=(const ScreenOrientationProvider&) =
      delete;

  ~ScreenOrientationProvider() override;

  void BindScreenOrientation(
      RenderFrameHost* rfh,
      mojo::PendingAssociatedReceiver<device::mojom::ScreenOrientation>
          receiver);

  // Return if orientation lock is enabled.  This does not guarantee that any
  // particular call to `Lock` will succeed, but lack of support here does mean
  // that `Lock` will definitely won't work.
  bool IsOrientationLockSupported() const;

  // device::mojom::ScreenOrientation:
  void LockOrientation(device::mojom::ScreenOrientationLockType,
                       LockOrientationCallback callback) override;
  void UnlockOrientation() override;

  // Inform about a screen orientation update. It is called to let the provider
  // know if a lock has been resolved.
  void OnOrientationChange();

  // Provide a delegate which creates delegates for platform implementations.
  // The delegate is not owned by ScreenOrientationProvider.
  static void SetDelegate(ScreenOrientationDelegate* delegate);
  static ScreenOrientationDelegate* GetDelegateForTesting();
  static bool LockMatchesOrientation(
      device::mojom::ScreenOrientationLockType lock,
      display::mojom::ScreenOrientation orientation);

  // WebContentsObserver
  void DidToggleFullscreenModeForTab(bool entered_fullscreen,
                                     bool will_cause_resize) override;
  void PrimaryPageChanged(Page& page) override;

 private:
  // Calls on |on_result_callback_| with |result|, followed by resetting
  // |on_result_callback_| and |pending_lock_orientation_|.
  void NotifyLockResult(device::mojom::ScreenOrientationLockResult result);

  // Returns the lock type that should be associated with 'natural' lock.
  // Returns device::mojom::ScreenOrientationLockType if the natural lock type
  // can't be found.
  device::mojom::ScreenOrientationLockType GetNaturalLockType() const;

  // Whether the passed |lock| matches the current orientation. In other words,
  // whether the orientation will need to change to match the |lock|.
  bool LockMatchesCurrentOrientation(
      device::mojom::ScreenOrientationLockType lock);

  // Not owned, responsible for platform implementations.
  static ScreenOrientationDelegate* delegate_;

  // Whether the ScreenOrientationProvider currently has a lock applied.
  bool lock_applied_;

  // Lock that require orientation changes are not completed until
  // OnOrientationChange.
  std::optional<device::mojom::ScreenOrientationLockType>
      pending_lock_orientation_;

  LockOrientationCallback pending_callback_;

  RenderFrameHostReceiverSet<device::mojom::ScreenOrientation> receivers_;
};

}  // namespace content

#endif  // CONTENT_BROWSER_SCREEN_ORIENTATION_SCREEN_ORIENTATION_PROVIDER_H_