File: fake_content_analysis_delegate.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 (142 lines) | stat: -rw-r--r-- 6,018 bytes parent folder | download | duplicates (5)
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
// Copyright 2019 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_ENTERPRISE_CONNECTORS_TEST_FAKE_CONTENT_ANALYSIS_DELEGATE_H_
#define CHROME_BROWSER_ENTERPRISE_CONNECTORS_TEST_FAKE_CONTENT_ANALYSIS_DELEGATE_H_

#include <memory>

#include "base/files/file_path.h"
#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/enterprise/connectors/analysis/clipboard_request_handler.h"
#include "chrome/browser/enterprise/connectors/analysis/content_analysis_delegate.h"
#include "chrome/browser/enterprise/connectors/test/fake_files_request_handler.h"
#include "chrome/browser/safe_browsing/cloud_content_scanning/binary_upload_service.h"
#include "components/enterprise/common/proto/connectors.pb.h"

namespace content {
class WebContents;
}

namespace enterprise_connectors::test {

// A derivative of ContentAnalysisDelegate that overrides calls to the
// real binary upload service and re-implements them locally.
class FakeContentAnalysisDelegate : public ContentAnalysisDelegate {
 public:
  // Callback that determines the scan status of the file specified.  To
  // simulate a file that passes a scan return a successful response, such
  // as the value returned by SuccessfulResponse().  To simulate a file that
  // does not pass a scan return a failed response, such as the value returned
  // by MalwareResponse() or DlpResponse().
  //
  // For text requests, contents is not empty and path is empty.
  // For print requests, both contents and path are empty.
  using StatusCallback = base::RepeatingCallback<ContentAnalysisResponse(
      const std::string& contents,
      const base::FilePath&)>;

  FakeContentAnalysisDelegate(base::RepeatingClosure delete_closure,
                              StatusCallback status_callback,
                              std::string dm_token,
                              content::WebContents* web_contents,
                              Data data,
                              CompletionCallback callback);
  ~FakeContentAnalysisDelegate() override;

  // Use with ContentAnalysisDelegate::SetFactoryForTesting() to create
  // fake instances of this class.  Note that all but the last three arguments
  // will need to be bound at base::BindRepeating() time.
  static std::unique_ptr<ContentAnalysisDelegate> Create(
      base::RepeatingClosure delete_closure,
      StatusCallback status_callback,
      std::string dm_token,
      content::WebContents* web_contents,
      Data data,
      CompletionCallback callback);

  // Sets a delay to have before returning responses. This is used by tests that
  // need to simulate response taking some time.
  static void SetResponseDelay(base::TimeDelta delay);

  // Returns a content analysis response that represents a successful scan and
  // includes the given tags.
  static ContentAnalysisResponse SuccessfulResponse(
      const std::set<std::string>& tags);

  // Returns a content analysis response with a specific malware action.
  static ContentAnalysisResponse MalwareResponse(TriggeredRule::Action action);

  // Returns a content analysis response with a specific DLP action.
  static ContentAnalysisResponse DlpResponse(
      ContentAnalysisResponse::Result::Status status,
      const std::string& rule_name,
      TriggeredRule::Action action);

  // Returns a content analysis response with specific malware and DLP actions.
  static ContentAnalysisResponse MalwareAndDlpResponse(
      TriggeredRule::Action malware_action,
      ContentAnalysisResponse::Result::Status dlp_status,
      const std::string& dlp_rule_name,
      TriggeredRule::Action dlp_action);

  // Sets the BinaryUploadService::Result to use in the next response callback.
  static void SetResponseResult(
      safe_browsing::BinaryUploadService::Result result);

  static void ResetStaticDialogFlagsAndTotalRequestsCount();
  static bool WasDialogShown();
  static bool WasDialogCanceled();
  static int GetTotalAnalysisRequestsCount();

  ContentAnalysisResponse GetStatus(const std::string& contents,
                                    const base::FilePath& path);

  virtual void FakeUploadClipboardDataForDeepScanning(
      ClipboardRequestHandler::Type type,
      std::unique_ptr<safe_browsing::BinaryUploadService::Request> request);

 protected:
  // Simulates a response from the binary upload service.  the |path|
  // argument is used to call |status_callback_| to determine if the path
  // should succeed or fail.
  void Response(
      std::string contents,
      base::FilePath path,
      std::unique_ptr<safe_browsing::BinaryUploadService::Request> request,
      std::optional<FakeFilesRequestHandler::FakeFileRequestCallback>
          file_request_callback,
      bool is_image_request);

  // ContentAnalysisDelegate overrides.
  bool ShowFinalResultInDialog() override;
  bool CancelDialog() override;
  safe_browsing::BinaryUploadService* GetBinaryUploadService() override;

  // Fake upload callback for deep scanning. Virtual to be overridden by other
  // fakes.
  virtual void FakeUploadFileForDeepScanning(
      safe_browsing::BinaryUploadService::Result result,
      const base::FilePath& path,
      std::unique_ptr<safe_browsing::BinaryUploadService::Request> request,
      FakeFilesRequestHandler::FakeFileRequestCallback callback);
  void FakeUploadPageForDeepScanning(
      std::unique_ptr<safe_browsing::BinaryUploadService::Request> request);

  static safe_browsing::BinaryUploadService::Result result_;
  static bool dialog_shown_;
  static bool dialog_canceled_;
  static int64_t total_analysis_requests_count_;

  base::RepeatingClosure delete_closure_;
  StatusCallback status_callback_;
  std::string dm_token_;

  base::WeakPtrFactory<FakeContentAnalysisDelegate> weakptr_factory_{this};
};

}  // namespace enterprise_connectors::test

#endif  // CHROME_BROWSER_ENTERPRISE_CONNECTORS_TEST_FAKE_CONTENT_ANALYSIS_DELEGATE_H_