File: on_task_session_manager.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (195 lines) | stat: -rw-r--r-- 7,777 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_ASH_COMPONENTS_BOCA_ON_TASK_ON_TASK_SESSION_MANAGER_H_
#define CHROMEOS_ASH_COMPONENTS_BOCA_ON_TASK_ON_TASK_SESSION_MANAGER_H_

#include <memory>
#include <optional>
#include <string>

#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/thread_annotations.h"
#include "chromeos/ash/components/boca/boca_session_manager.h"
#include "chromeos/ash/components/boca/boca_window_observer.h"
#include "chromeos/ash/components/boca/on_task/activity/active_tab_tracker.h"
#include "chromeos/ash/components/boca/on_task/on_task_blocklist.h"
#include "chromeos/ash/components/boca/on_task/on_task_extensions_manager.h"
#include "chromeos/ash/components/boca/on_task/on_task_notifications_manager.h"
#include "chromeos/ash/components/boca/on_task/on_task_system_web_app_manager.h"
#include "chromeos/ash/components/boca/proto/bundle.pb.h"
#include "url/gurl.h"

namespace ash::boca {

class OnTaskSessionManagerTest;

// Session manager implementation that is primarily used for configuring and
// managing OnTask components and services throughout a Boca session.
class OnTaskSessionManager : public boca::BocaSessionManager::Observer,
                             public boca::BocaWindowObserver {
 public:
  explicit OnTaskSessionManager(
      std::unique_ptr<OnTaskSystemWebAppManager> system_web_app_manager,
      std::unique_ptr<OnTaskExtensionsManager> extensions_manager);
  OnTaskSessionManager(const OnTaskSessionManager&) = delete;
  OnTaskSessionManager& operator=(const OnTaskSessionManager&) = delete;
  ~OnTaskSessionManager() override;

  // BocaSessionManager::Observer:
  void OnSessionStarted(const std::string& session_id,
                        const ::boca::UserIdentity& producer) override;
  void OnSessionEnded(const std::string& session_id) override;
  void OnBundleUpdated(const ::boca::Bundle& bundle) override;
  void OnAppReloaded() override;

  ActiveTabTracker* active_tab_tracker() { return active_tab_tracker_.get(); }

  // BocaWindowObserver:
  void OnTabAdded(const SessionID active_tab_id,
                  const SessionID tab_id,
                  const GURL url) override;
  void OnTabRemoved(const SessionID tab_id) override;

  boca::OnTaskSystemWebAppManager* GetOnTaskSystemWebAppManager() {
    return system_web_app_manager_.get();
  }

  boca::OnTaskNotificationsManager* GetOnTaskNotificationsManager() {
    return notifications_manager_.get();
  }

  void SetActiveTabTrackerForTesting(
      std::unique_ptr<ActiveTabTracker> active_tab_tracker);

  void SetNotificationManagerForTesting(
      std::unique_ptr<ash::boca::OnTaskNotificationsManager>
          notification_manager);

 private:
  friend class OnTaskSessionManagerTest;

  // Helper class that is used to launch the Boca system web app as well as
  // manage all interactions with the Boca system web app while it is being
  // spawned.
  class SystemWebAppLaunchHelper {
   public:
    SystemWebAppLaunchHelper(
        OnTaskSystemWebAppManager* system_web_app_manager,
        const std::vector<boca::BocaWindowObserver*> observers);
    SystemWebAppLaunchHelper(const SystemWebAppLaunchHelper&) = delete;
    SystemWebAppLaunchHelper& operator=(const SystemWebAppLaunchHelper&) =
        delete;
    ~SystemWebAppLaunchHelper();

    void LaunchBocaSWA();
    void AddTab(
        GURL url,
        ::boca::LockedNavigationOptions::NavigationType restriction_level,
        base::OnceCallback<void(SessionID)> callback);
    void RemoveTab(const std::set<SessionID>& tab_ids_to_remove,
                   base::OnceClosure callback);
    void SetPinStateForActiveSWAWindow(bool pinned,
                                       base::RepeatingClosure callback);

    void SetObserversForTesting(
        std::vector<boca::BocaWindowObserver*> observers) {
      observers_ = std::move(observers);
    }

   private:
    // Callback triggered when the Boca SWA is launched. Normally at the onset
    // of a Boca session.
    void OnBocaSWALaunched(bool success);

    void SetPinStateForActiveSWAWindowInternal(bool pinned,
                                               base::RepeatingClosure callback);

    // Owned by the parent class `OnTaskSessionManager` that owns an instance of
    // the class `SystemWebAppLaunchHelper`, so there won't be UAF errors.
    raw_ptr<OnTaskSystemWebAppManager> system_web_app_manager_;
    std::vector<boca::BocaWindowObserver*> observers_;

    SEQUENCE_CHECKER(sequence_checker_);

    bool launch_in_progress_ GUARDED_BY_CONTEXT(sequence_checker_) = false;

    // The latest pin state of the bundle sent by provider.
    bool latest_pin_state_ GUARDED_BY_CONTEXT(sequence_checker_) = false;

    base::WeakPtrFactory<SystemWebAppLaunchHelper> weak_ptr_factory_{this};
  };

  // Internal helper used to lock or unlock the current app window. This
  // involves disabling relevant extensions and pinning the window if
  // `lock_window` is true, or re-enabling extensions and unpinning the window
  // otherwise.
  void LockOrUnlockWindow(bool lock_window);

  // Internal helper used to pause or unpause the boca app.
  void PauseOrUnpauseApp();

  // Show enter locked mode notification and lock the Boca SWA window.
  void EnterLockedMode();

  // Callback triggered when a tab from the bundle is added.
  void OnBundleTabAdded(
      GURL url,
      ::boca::LockedNavigationOptions::NavigationType restriction_level,
      SessionID tab_id);

  // Callback triggered when a tab from the bundle is removed.
  void OnBundleTabRemoved(GURL url);

  // Callback triggered when the Boca SWA window pin state is set.
  void OnSetPinStateOnBocaSWAWindow();

  // Set the `active_tab_url_` to be the url associated with `tab_id`.
  void TrackActiveTabURLFromTab(SessionID tab_id);

  std::unique_ptr<ActiveTabTracker> active_tab_tracker_;

  const std::unique_ptr<OnTaskSystemWebAppManager> system_web_app_manager_;

  SEQUENCE_CHECKER(sequence_checker_);

  std::optional<std::string> active_session_id_
      GUARDED_BY_CONTEXT(sequence_checker_) = std::nullopt;
  GURL active_tab_url_ GUARDED_BY_CONTEXT(sequence_checker_);
  bool should_lock_window_ GUARDED_BY_CONTEXT(sequence_checker_) = false;
  bool lock_in_progress_ GUARDED_BY_CONTEXT(sequence_checker_) = false;
  bool enter_pause_mode_ GUARDED_BY_CONTEXT(sequence_checker_) = false;

  // The set of urls sent by the provider.
  base::flat_set<GURL> provider_url_set_ GUARDED_BY_CONTEXT(sequence_checker_);

  // Maps the url that providers send to the tab ids spawned from the url. This
  // map allows to remove all the related tabs to the url.
  base::flat_map<GURL, std::set<SessionID>> provider_url_tab_ids_map_
      GUARDED_BY_CONTEXT(sequence_checker_);

  // Maps the url that providers send to the restriction levels it is currently
  // set to. This map allows for tracking restriction level updates.
  base::flat_map<GURL, ::boca::LockedNavigationOptions::NavigationType>
      provider_url_restriction_level_map_ GUARDED_BY_CONTEXT(sequence_checker_);

  const std::unique_ptr<OnTaskExtensionsManager> extensions_manager_;

  const std::unique_ptr<SystemWebAppLaunchHelper> system_web_app_launch_helper_;

  std::unique_ptr<OnTaskNotificationsManager> notifications_manager_;

  base::TimeDelta notification_countdown_duration_;

  base::WeakPtrFactory<OnTaskSessionManager> weak_ptr_factory_{this};
};

}  // namespace ash::boca

#endif  // CHROMEOS_ASH_COMPONENTS_BOCA_ON_TASK_ON_TASK_SESSION_MANAGER_H_