File: hats_service_desktop.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 (232 lines) | stat: -rw-r--r-- 8,852 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
// Copyright 2023 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_UI_HATS_HATS_SERVICE_DESKTOP_H_
#define CHROME_BROWSER_UI_HATS_HATS_SERVICE_DESKTOP_H_

#include <optional>
#include <set>
#include <string>
#include <string_view>

#include "base/functional/callback.h"
#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/ui/hats/hats_service.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "content/public/browser/web_contents_observer.h"

class Browser;
class PrefService;

// Key-value mapping type for survey's product specific bits data.
typedef std::map<std::string, bool> SurveyBitsData;

// Key-value mapping type for survey's product specific string data.
typedef std::map<std::string, std::string> SurveyStringData;

// The name of the histogram which records if a survey was shown, or if not, the
// reason why not.
extern const char kHatsShouldShowSurveyReasonHistogram[];

// This class provides the client side logic for determining if a
// survey should be shown for any trigger based on input from a finch
// configuration. It is created on a per profile basis.
class HatsServiceDesktop : public HatsService {
 public:
  class DelayedSurveyTask : public content::WebContentsObserver {
   public:
    DelayedSurveyTask(HatsServiceDesktop* hats_service,
                      std::string trigger,
                      content::WebContents* web_contents,
                      const SurveyBitsData& product_specific_bits_data,
                      const SurveyStringData& product_specific_string_data,
                      NavigationBehavior navigation_behavior,
                      base::OnceClosure success_callback,
                      base::OnceClosure failure_callback,
                      std::optional<std::string_view> supplied_trigger_id);

    // Not copyable or movable
    DelayedSurveyTask(const DelayedSurveyTask&) = delete;
    DelayedSurveyTask& operator=(const DelayedSurveyTask&) = delete;

    ~DelayedSurveyTask() override;

    // Asks |hats_service_| to launch the survey with id |trigger_| for tab
    // |web_contents_|.
    void Launch();

    // content::WebContentsObserver
    void DidFinishNavigation(
        content::NavigationHandle* navigation_handle) override;
    void WebContentsDestroyed() override;

    // Returns a weak pointer to this object.
    virtual base::WeakPtr<DelayedSurveyTask> GetWeakPtr();

    bool operator<(const HatsServiceDesktop::DelayedSurveyTask& other) const {
      return trigger_ < other.trigger_ ? true
                                       : web_contents() < other.web_contents();
    }

   private:
    raw_ptr<HatsServiceDesktop> hats_service_;

    std::string trigger_;
    SurveyBitsData product_specific_bits_data_;
    SurveyStringData product_specific_string_data_;
    NavigationBehavior navigation_behavior_;
    base::OnceClosure success_callback_;
    base::OnceClosure failure_callback_;
    std::optional<std::string> supplied_trigger_id_;
    base::WeakPtrFactory<DelayedSurveyTask> weak_ptr_factory_{this};
  };

  // These values are persisted to logs. Entries should not be renumbered and
  // numeric values should never be reused.
  enum class ShouldShowSurveyReasons {
    kYes = 0,
    kNoOffline = 1,
    kNoLastSessionCrashed = 2,
    kNoReceivedSurveyInCurrentMilestone = 3,
    kNoProfileTooNew = 4,
    kNoLastSurveyTooRecent = 5,
    kNoBelowProbabilityLimit = 6,
    kNoTriggerStringMismatch = 7,
    kNoWrongBrowserType = 8,
    kNoIncognitoDisabled = 9,
    kNoCookiesBlocked = 10,            // Unused.
    kNoThirdPartyCookiesBlocked = 11,  // Unused.
    kNoSurveyUnreachable = 12,
    kNoSurveyOverCapacity = 13,
    kNoSurveyAlreadyInProgress = 14,
    kNoAnyLastSurveyTooRecent = 15,
    kNoRejectedByHatsService = 16,
    kNoLastSurveyCheckTooRecent = 17,
    kMaxValue = kNoLastSurveyCheckTooRecent,
  };

  explicit HatsServiceDesktop(Profile* profile);

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

  ~HatsServiceDesktop() override;

  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);

  using HatsService::LaunchSurvey;
  void LaunchSurvey(const std::string& trigger,
                    base::OnceClosure success_callback,
                    base::OnceClosure failure_callback,
                    const SurveyBitsData& product_specific_bits_data,
                    const SurveyStringData& product_specific_string_data,
                    const std::optional<std::string>& supplied_trigger_id,
                    const SurveyOptions& survey_options) override;

  using HatsService::LaunchSurveyForWebContents;
  void LaunchSurveyForWebContents(
      const std::string& trigger,
      content::WebContents* web_contents,
      const SurveyBitsData& product_specific_bits_data,
      const SurveyStringData& product_specific_string_data,
      base::OnceClosure success_callback,
      base::OnceClosure failure_callback,
      const std::optional<std::string>& supplied_trigger_id,
      const SurveyOptions& survey_options) override;

  using HatsService::LaunchDelayedSurvey;
  bool LaunchDelayedSurvey(
      const std::string& trigger,
      int timeout_ms,
      const SurveyBitsData& product_specific_bits_data,
      const SurveyStringData& product_specific_string_data) override;

  using HatsService::LaunchDelayedSurveyForWebContents;
  bool LaunchDelayedSurveyForWebContents(
      const std::string& trigger,
      content::WebContents* web_contents,
      int timeout_ms,
      const SurveyBitsData& product_specific_bits_data,
      const SurveyStringData& product_specific_string_data,
      NavigationBehavior navigation_behavior,
      base::OnceClosure success_callback,
      base::OnceClosure failure_callback,
      const std::optional<std::string>& supplied_trigger_id,
      const SurveyOptions& survey_options) override;

  void SetSurveyMetadataForTesting(const HatsService::SurveyMetadata& metadata);
  void GetSurveyMetadataForTesting(HatsService::SurveyMetadata* metadata) const;

  bool HasPendingTasks();

  bool CanShowSurvey(const std::string& trigger) const override;

  bool CanShowAnySurvey(bool user_prompted) const override;

  void RecordSurveyAsShown(std::string trigger_id) override;

  // Indicates to the service that the HaTS Next dialog has been closed.
  virtual void HatsNextDialogClosed();

  // Returns whether a HaTS Next dialog currently exists, regardless of whether
  // it is being shown or not.
  bool hats_next_dialog_exists_for_testing() {
    return hats_next_dialog_exists_;
  }

 protected:
 private:
  FRIEND_TEST_ALL_PREFIXES(HatsServiceProbabilityOne, SingleHatsNextDialog);

  PrefService* GetPrefsForHatsMetadata() const;

  // Remove |task| from the set of |pending_tasks_|.
  void RemoveTask(const DelayedSurveyTask& task);

  // Returns true is the survey trigger specified should be shown.
  bool ShouldShowSurvey(const std::string& trigger) const;

  // Returns true is the requested browser type matches the actual browser type.
  bool IsRightBrowserType(
      Browser* browser,
      hats::SurveyConfig::RequestedBrowserType requested_browser_type) const;

  void LaunchSurveyForBrowser(
      Browser* browser,
      const std::string& trigger,
      base::OnceClosure success_callback,
      base::OnceClosure failure_callback,
      const SurveyBitsData& product_specific_bits_data,
      const SurveyStringData& product_specific_string_data,
      const std::optional<std::string_view>& supplied_trigger_id =
          std::nullopt);

  // Check whether the survey is reachable and under capacity and show it.
  // |success_callback| is called when the survey is shown to the user.
  // |failure_callback| is called if the survey does not launch for any reason.
  // The matches of field names with the `SurveyConfig` are CHECK
  // enforced.
  void CheckSurveyStatusAndMaybeShow(
      Browser* browser,
      const std::string& trigger,
      base::OnceClosure success_callback,
      base::OnceClosure failure_callback,
      const SurveyBitsData& product_specific_bits_data,
      const SurveyStringData& product_specific_string_data,
      const std::optional<std::string_view>& supplied_trigger_id);

  std::set<DelayedSurveyTask> pending_tasks_;

  // Whether a HaTS Next dialog currently exists (regardless of whether it
  // is being shown to the user).
  bool hats_next_dialog_exists_ = false;

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

#endif  // CHROME_BROWSER_UI_HATS_HATS_SERVICE_DESKTOP_H_