File: dlp_content_manager.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (480 lines) | stat: -rw-r--r-- 20,564 bytes parent folder | download | duplicates (3)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_CHROMEOS_POLICY_DLP_DLP_CONTENT_MANAGER_H_
#define CHROME_BROWSER_CHROMEOS_POLICY_DLP_DLP_CONTENT_MANAGER_H_

#include <array>
#include <memory>
#include <string>
#include <utility>

#include "base/containers/flat_map.h"
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/time/time.h"
#include "chrome/browser/chromeos/policy/dlp/dialogs/dlp_warn_dialog.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_confidential_contents.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_content_manager_observer.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_content_observer.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_content_restriction_set.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_content_tab_helper.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_rules_manager.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
#include "content/public/browser/desktop_media_id.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/media_stream_request.h"
#include "ui/views/widget/widget.h"
#include "url/gurl.h"

namespace content {
struct DesktopMediaID;
class WebContents;
}  // namespace content

namespace data_controls {
class DlpReportingManager;
}  // namespace data_controls

namespace policy {

class DlpWarnNotifier;

// System-wide class that tracks the set of currently known confidential
// WebContents and whether any of them are currently visible.
// If any confidential WebContents is visible, the corresponding restrictions
// will be enforced according to the current enterprise policy.
class DlpContentManager : public DlpContentObserver,
                          public BrowserListObserver,
                          public TabStripModelObserver {
 public:
  // Holds DLP restrictions information for `web_contents` object.
  struct WebContentsInfo {
    WebContentsInfo();
    WebContentsInfo(content::WebContents* web_contents,
                    DlpContentRestrictionSet restriction_set,
                    std::vector<DlpContentTabHelper::RfhInfo> rfh_info_vector);
    WebContentsInfo(const WebContentsInfo&);
    WebContentsInfo& operator=(const WebContentsInfo&);
    ~WebContentsInfo();

    raw_ptr<content::WebContents> web_contents = nullptr;
    // Restrictions set for `web_contents`.
    DlpContentRestrictionSet restriction_set;
    // DLP restrictions info for RenderFrameHosts in `web_contents`.
    std::vector<DlpContentTabHelper::RfhInfo> rfh_info_vector;
  };

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

  // Returns platform-specific implementation of the class. Never returns
  // nullptr.
  static DlpContentManager* Get();

  // Returns which restrictions are applied to the |web_contents| according to
  // the policy.
  DlpContentRestrictionSet GetConfidentialRestrictions(
      content::WebContents* web_contents) const;

  // Returns whether screenshare should be blocked for the specified
  // WebContents.
  bool IsScreenShareBlocked(content::WebContents* web_contents) const;

  // Checks whether printing of |web_contents| is restricted or not advised.
  // Depending on the result, calls |callback| and passes an indicator whether
  // to proceed or not.
  void CheckPrintingRestriction(content::WebContents* web_contents,
                                content::GlobalRenderFrameHostId rfh_id,
                                WarningCallback callback);

  // Returns whether screenshots should be restricted for extensions API.
  virtual bool IsScreenshotApiRestricted(content::WebContents* web_contents);

  // Checks whether screen sharing of content from the |media_id| source with
  // application |application_title| is restricted or not advised. Depending on
  // the result, calls |callback| and passes an indicator whether to proceed or
  // not.
  virtual void CheckScreenShareRestriction(
      const content::DesktopMediaID& media_id,
      const std::u16string& application_title,
      WarningCallback callback) = 0;

  // Called when screen share is started.
  // |state_change_callback| will be called when restricted content will appear
  // or disappear in the captured area to pause/resume the share.
  // |source_callback| will be called only to update the source for a tab share
  // before resuming the capture.
  // |stop_callback| will be called after a user dismisses a warning.
  virtual void OnScreenShareStarted(
      const std::string& label,
      std::vector<content::DesktopMediaID> screen_share_ids,
      const std::u16string& application_title,
      base::RepeatingClosure stop_callback,
      content::MediaStreamUI::StateChangeCallback state_change_callback,
      content::MediaStreamUI::SourceCallback source_callback) = 0;

  // Called when screen share is stopped.
  virtual void OnScreenShareStopped(
      const std::string& label,
      const content::DesktopMediaID& media_id) = 0;

  // Called when a screen share is being stopped before processing a source
  // change from |old_media_id| to |new_media_id|. The share might have been
  // paused by DLP due to restricted content, so should be resumed before the
  // change source request proceeds.
  virtual void OnScreenShareSourceChanging(
      const std::string& label,
      const content::DesktopMediaID& old_media_id,
      const content::DesktopMediaID& new_media_id,
      bool captured_surface_control_active);

  void AddObserver(DlpContentManagerObserver* observer,
                   DlpContentRestriction restriction);

  void RemoveObserver(const DlpContentManagerObserver* observer,
                      DlpContentRestriction restriction);

  // Returns an array of DLP restrictions info to all the tracked WebContents.
  std::vector<WebContentsInfo> GetWebContentsInfo() const;

 protected:
  friend class DlpContentManagerTestHelper;

  void SetReportingManagerForTesting(
      data_controls::DlpReportingManager* manager);

  void SetWarnNotifierForTesting(
      std::unique_ptr<DlpWarnNotifier> warn_notifier);
  void ResetWarnNotifierForTesting();

  // Sets the delay before resuming a screen share.
  static void SetScreenShareResumeDelayForTesting(base::TimeDelta delay);

  // Structure that relates a list of confidential contents to the
  // corresponding restriction level.
  struct ConfidentialContentsInfo {
    RestrictionLevelAndUrl restriction_info;
    DlpConfidentialContents confidential_contents;
  };

  // Used to keep track of running screen shares.
  class ScreenShareInfo {
   public:
    enum class State {
      kRunning,
      kPaused,
      kStopped,
      kRunningBeforeSourceChange,
      kPausedBeforeSourceChange
    };

    ScreenShareInfo(
        const std::string& label,
        const content::DesktopMediaID& media_id,
        const std::u16string& application_title,
        base::OnceClosure stop_callback,
        content::MediaStreamUI::StateChangeCallback state_change_callback,
        content::MediaStreamUI::SourceCallback source_callback);
    ~ScreenShareInfo();

    // Updates an existing ScreenShareInfo instance. Should only be called for
    // tab shares and after a source change has occurred. In addition to the
    // passed parameters, restores the state to a correct value and updates
    // notifications if necessary.
    void UpdateAfterSourceChange(
        const content::DesktopMediaID& media_id,
        const std::u16string& application_title,
        base::OnceClosure stop_callback,
        content::MediaStreamUI::StateChangeCallback state_change_callback,
        content::MediaStreamUI::SourceCallback source_callback);

    bool operator==(const ScreenShareInfo& other) const;

    const content::DesktopMediaID& media_id() const;
    const content::DesktopMediaID& new_media_id() const;
    void set_new_media_id(const content::DesktopMediaID& media_id);
    const std::string& label() const;
    const std::u16string& application_title() const;
    State state() const;
    base::WeakPtr<content::WebContents> web_contents() const;
    // Saves the |dialog_widget| as the current dialog handle.
    // Assumes that the previous widget is closed or not set. It's
    // responsibility of the called to ensure that the restriction level is
    // WARN.
    void set_dialog_widget(base::WeakPtr<views::Widget> dialog_widget);
    void set_latest_confidential_contents_info(
        ConfidentialContentsInfo confidential_contents_info);
    // Returns the restriction information that was the last enforced on this
    // screen share.
    const RestrictionLevelAndUrl& GetLatestRestriction() const;
    // Returns the confidential contents that caused the latest restriction.
    const DlpConfidentialContents& GetConfidentialContents() const;

    // Pauses a running screen share.
    // No-op if the screen share is already paused.
    void Pause();
    // Resumes a paused screen share.
    // For tab shares, resuming can be a consequence of navigating within a page
    // so calls the |source_callback_| to ensure that the |media_id| is updated.
    // No-op if the screen share is already running.
    void Resume();
    // Changes the state to indicate that the source is being changed.
    // Every source change stops the share and starts a new one, so this is
    // needed to store all the required information in the meantime and update
    // it if the source change is successful.
    void ChangeStateBeforeSourceChange();
    // Stops the screen share. Can only be called once.
    void Stop();
    // Start the screen share after source change if pending.
    void StartIfPending();

    // If necessary, hides or shows the paused/resumed notification for this
    // screen share. The notification should be updated after changing the state
    // from running to paused, or paused to running.
    void MaybeUpdateNotifications();

    // If shown, hides both the paused and resumed notification for this screen
    // share.
    void HideNotifications();

    // If currently opened, closes the associated DlpWarnDialog widget.
    void MaybeCloseDialogWidget();
    // Returns true if there is an associated DlpWarnDialog object, false
    // otherwise.
    bool HasOpenDialogWidget();

    // Records that Captured Surface Control was active at some point during
    // the capture.
    void SetCapturedSurfaceControlActive();

    base::WeakPtr<ScreenShareInfo> GetWeakPtr();

   private:
    enum class NotificationState {
      kNotShowingNotification,
      kShowingPausedNotification,
      kShowingResumedNotification
    };
    // Shows (if |show| is true) or hides (if |show| is false) paused
    // notification for this screen share. Does nothing if the notification is
    // already in the required state.
    void UpdatePausedNotification(bool show);
    // Shows (if |show| is true) or hides (if |show| is false) resumed
    // notification for this screen share. Does nothing if the notification is
    // already in the required state.
    void UpdateResumedNotification(bool show);

    std::string label_;
    content::DesktopMediaID media_id_;
    content::DesktopMediaID new_media_id_;
    std::u16string application_title_;
    base::OnceClosure stop_callback_;
    content::MediaStreamUI::StateChangeCallback state_change_callback_;
    content::MediaStreamUI::SourceCallback source_callback_;
    State state_ = State::kRunning;
    NotificationState notification_state_ =
        NotificationState::kNotShowingNotification;
    // Information on the latest restriction enforced.
    ConfidentialContentsInfo latest_confidential_contents_info_;
    // Pointer to the associated DlpWarnDialog widget.
    // Not null only while the dialog is opened.
    base::WeakPtr<views::Widget> dialog_widget_ = nullptr;
    // Remembers that it should be restarted after source update.
    bool pending_start_on_source_change_ = false;

    // Set only for tab shares.
    base::WeakPtr<content::WebContents> web_contents_;

    // Only meaningful for tab-shares.
    bool captured_surface_control_active_ = false;

    base::WeakPtrFactory<ScreenShareInfo> weak_factory_{this};
  };

  DlpContentManager();
  ~DlpContentManager() override;

  // Reports events of type DlpPolicyEvent::Mode::WARN_PROCEED to
  // `reporting_manager`.
  static void ReportWarningProceededEvent(
      const GURL& url,
      DlpRulesManager::Restriction restriction,
      data_controls::DlpReportingManager* reporting_manager);

  // Helper method to create a callback with ReportWarningProceededEvent
  // function.
  static bool MaybeReportWarningProceededEvent(
      GURL url,
      DlpRulesManager::Restriction restriction,
      data_controls::DlpReportingManager* reporting_manager,
      bool should_proceed);

  // Retrieves WebContents from |media_id| for tab shares. Otherwise returns
  // nullptr.
  static content::WebContents* GetWebContentsFromMediaId(
      const content::DesktopMediaID& media_id);

  // Initializing to be called separately to make testing possible.
  virtual void Init();

  // DlpContentObserver overrides:
  void OnConfidentialityChanged(
      content::WebContents* web_contents,
      const DlpContentRestrictionSet& restriction_set) override;
  void OnWebContentsDestroyed(content::WebContents* web_contents) override;

  // BrowserListObserver overrides:
  void OnBrowserAdded(Browser* browser) override;
  void OnBrowserRemoved(Browser* browser) override;

  // TabStripModelObserver overrides:
  void OnTabStripModelChanged(
      TabStripModel* tab_strip_model,
      const TabStripModelChange& change,
      const TabStripSelectionChange& selection) override;

  // Called when tab was probably moved, but without change of the visibility.
  virtual void TabLocationMaybeChanged(content::WebContents* web_contents) = 0;

  // Helper to remove |web_contents| from the confidential set.
  virtual void RemoveFromConfidential(content::WebContents* web_contents);

  // Returns which level and url of printing restriction is currently enforced
  // for |web_contents|.
  RestrictionLevelAndUrl GetPrintingRestrictionInfo(
      content::WebContents* web_contents,
      content::GlobalRenderFrameHostId rfh_id) const;

  // Returns confidential info for screen share of a single |web_contents|.
  ConfidentialContentsInfo GetScreenShareConfidentialContentsInfoForWebContents(
      content::WebContents* web_contents) const;

  // Applies retrieved restrictions in |info| to screens share attempt from
  // app |application_title| and calls the |callback| with a result.
  void ProcessScreenShareRestriction(const std::u16string& application_title,
                                     ConfidentialContentsInfo info,
                                     WarningCallback callback);

  // Returns which level, url, and information about visible confidential
  // contents of screen share restriction that is currently enforced for
  // |media_id|. |web_contents| is not null for tab shares.
  virtual ConfidentialContentsInfo GetScreenShareConfidentialContentsInfo(
      const content::DesktopMediaID& media_id,
      content::WebContents* web_contents) const = 0;

  // If a screen share with the same |label| already exists in
  // |running_screen_shares_|, updates the existing object. Otherwise adds a new
  // screen share to be tracked in |running_screen_shares_|. Callbacks are used
  // to control the screen share state in case it should be paused, resumed or
  // completely stopped by DLP.
  void AddOrUpdateScreenShare(
      const std::string& label,
      const content::DesktopMediaID& media_id,
      const std::u16string& application_title,
      base::RepeatingClosure stop_callback,
      content::MediaStreamUI::StateChangeCallback state_change_callback,
      content::MediaStreamUI::SourceCallback source_callback);

  // Removes screen share from |running_screen_shares_|.
  void RemoveScreenShare(const std::string& label,
                         const content::DesktopMediaID& media_id);

  // Checks and stops the running screen shares if restricted content appeared
  // in the corresponding areas.
  void CheckRunningScreenShares();

  // Resumes the |screen_share| after a delay if it's still necessary.
  void MaybeResumeScreenShare(base::WeakPtr<ScreenShareInfo> screen_share);

  // Called back from Screen Share warning dialogs that are shown during the
  // screen share. Passes along the user's response, reflected in the value of
  // |should_proceed| along to |callback| which handles continuing or cancelling
  // the action based on this response. In case that |should_proceed| is true,
  // also saves the |confidential_contents| that were allowed to be shared by
  // the user to avoid future warnings.
  void OnDlpScreenShareWarnDialogReply(
      const ConfidentialContentsInfo& info,
      base::WeakPtr<ScreenShareInfo> screen_share,
      bool should_proceed);

  // Called back from warning dialogs. Passes along the user's response,
  // reflected in the value of |should_proceed| along to |callback| which
  // handles continuing or cancelling the action based on this response. In case
  // that |should_proceed| is true, also saves the |confidential_contents| that
  // were allowed by the user for |restriction| to avoid future warnings.
  void OnDlpWarnDialogReply(
      const DlpConfidentialContents& confidential_contents,
      DlpRulesManager::Restriction restriction,
      WarningCallback callback,
      bool should_proceed);

  // Reports events if required by the |restriction_info| and
  // `reporting_manager` is configured.
  void MaybeReportEvent(const RestrictionLevelAndUrl& restriction_info,
                        DlpRulesManager::Restriction restriction);

  // Reports warning events if `reporting_manager` is configured.
  void ReportWarningEvent(const GURL& url,
                          DlpRulesManager::Restriction restriction);

  // Removes all elemxents of |contents| that the user has recently already
  // acknowledged the warning for.
  void RemoveAllowedContents(DlpConfidentialContents& contents,
                             DlpRulesManager::Restriction restriction);

  // Updates confidentiality for |web_contents| with the |restriction_set|.
  void UpdateConfidentiality(content::WebContents* web_contents,
                             const DlpContentRestrictionSet& restriction_set);

  // Notifies observers if the restrictions they are listening to changed.
  void NotifyOnConfidentialityChanged(
      const DlpContentRestrictionSet& old_restriction_set,
      const DlpContentRestrictionSet& new_restriction_set,
      content::WebContents* web_contents);

  // Map from currently known confidential WebContents to the restrictions.
  base::flat_map<content::WebContents*, DlpContentRestrictionSet>
      confidential_web_contents_;

  // Keeps track of the contents for which the user allowed the action after
  // being shown a warning for each type of restriction.
  // TODO(crbug.com/1264803): Change to DlpConfidentialContentsCache
  DlpConfidentialContentsCache user_allowed_contents_cache_;

  // List of the currently running screen shares.
  std::vector<std::unique_ptr<ScreenShareInfo>> running_screen_shares_;

  raw_ptr<data_controls::DlpReportingManager, DanglingUntriaged>
      reporting_manager_{nullptr};

  std::unique_ptr<DlpWarnNotifier> warn_notifier_;

  // One ObserverList per restriction.
  std::array<base::ObserverList<DlpContentManagerObserver>,
             static_cast<int>(DlpContentRestriction::kMaxValue) + 1>
      observer_lists_;

  // A helper structure that contains web contents which were reported during
  // the current screen share.
  // Navigating a tab or switching a tab with share-this-tab-instead does not
  // invalidate this contents.
  struct LastReportedScreenShare {
    // Checks if DLP should report for |label| and |confidential_contents|. If
    // yes, then updates internal structures. Does not emit any reporting event.
    bool ShouldReportAndUpdate(
        const std::string& label,
        const DlpConfidentialContents& confidential_contents);

   private:
    std::string label_;
    DlpConfidentialContents confidential_contents_;
  } last_reported_screen_share_;
};

}  // namespace policy

#endif  // CHROME_BROWSER_CHROMEOS_POLICY_DLP_DLP_CONTENT_MANAGER_H_