File: hats_dialog.cc

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 (201 lines) | stat: -rw-r--r-- 6,951 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
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
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/hats/hats_dialog.h"

#include <string_view>

#include "ash/constants/ash_features.h"
#include "base/containers/flat_map.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/escape.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/ash/hats/hats_config.h"
#include "chrome/browser/ash/hats/hats_finch_helper.h"
#include "chrome/browser/profiles/profile_destroyer.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/browser_resources.h"
#include "chrome/grit/generated_resources.h"
#include "chromeos/version/version_loader.h"
#include "components/language/core/browser/pref_names.h"
#include "components/language/core/common/locale_util.h"
#include "components/prefs/pref_service.h"
#include "components/version_info/version_info.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/re2/src/re2/re2.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/mojom/ui_base_types.mojom-shared.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/geometry/size.h"

using content::WebContents;
using content::WebUIMessageHandler;

namespace ash {

namespace {

// Default width/height ratio of screen size.
const int kDefaultWidth = 384;
const int kDefaultHeight = 428;

// The state specific UMA enumerations
const int kSurveyDisplayedEnumeration = 2;
const int kSurveyCompleteEnumeration = 3;

// Possible requested actions from the HTML+JS client.
// Client is ready to close the page.
const char kClientActionLoad[] = "load";
// Client is ready to close the page.
const char kClientActionClose[] = "close";
// Client is ready to close the page after completing the survey.
const char kClientActionComplete[] = "complete";
// There was an unhandled error and we need to log and close the page.
const char kClientActionUnhandledError[] = "survey-loading-error";
// A smiley was selected, so we'd like to track that.
const char kClientQuestionAnswered[] = "answer-";
const char kClientQuestionAnsweredRegex[] = "answer-(\\d+)-((?:\\d+,?)+)";
const char kClientQuestionAnsweredScoreRegex[] = "(\\d+),?";

constexpr char kCrOSHaTSURL[] =
    "https://storage.googleapis.com/chromeos-hats-web-stable/index.html";

}  // namespace

// Only log a histogram value if there is a histogram name provided.
void LogHistogram(const std::string& histogram_name, int enumeration) {
  if (!histogram_name.empty()) {
    base::UmaHistogramSparse(histogram_name, enumeration);
  }
}

// static
bool HatsDialog::ParseAnswer(const std::string& input,
                             int* question,
                             std::vector<int>* scores) {
  std::string question_num_string;
  std::string_view all_scores_string;
  if (!RE2::FullMatch(input, kClientQuestionAnsweredRegex, &question_num_string,
                      &all_scores_string))
    return false;

  if (!base::StringToInt(question_num_string, question) || *question <= 0 ||
      *question > 10) {
    LOG(ERROR) << "Can't parse Survey score";
    return false;
  }

  std::string score_string;
  while (RE2::FindAndConsume(
      &all_scores_string, kClientQuestionAnsweredScoreRegex, &score_string)) {
    int score;
    if (!base::StringToInt(score_string, &score) || score <= 0 || score > 100) {
      LOG(ERROR) << "Can't parse Survey score";
      return false;
    }
    scores->push_back(score);
  }

  return true;
}

bool HatsDialog::HandleClientTriggeredAction(
    const std::string& action,
    const std::string& histogram_name) {
  DVLOG(1) << "HandleClientTriggeredAction: Received " << action;

  // Page asks to be closed.
  if (action == kClientActionClose) {
    return true;
  }

  // An unhandled error in our client, log and close.
  if (base::StartsWith(action, kClientActionUnhandledError)) {
    LOG(ERROR) << "Error while loading a HaTS Survey " << action;
    return true;
  }

  // Page successfully loaded the survey.
  if (action == kClientActionLoad) {
    LogHistogram(histogram_name, kSurveyDisplayedEnumeration);
    return false;
  }

  // Page asks to be closed after completing the survey.
  if (action == kClientActionComplete) {
    LogHistogram(histogram_name, kSurveyCompleteEnumeration);
    return true;
  }

  // A question was answered
  if (base::StartsWith(action, kClientQuestionAnswered)) {
    int question;
    std::vector<int> question_scores;
    if (!ParseAnswer(action, &question, &question_scores)) {
      return false;  // It's a client error, but don't close the page.
    }

    for (int score : question_scores) {
      // The enumeration is specified as `QQNN`, where `QQ` is the question
      // number and `NN` is the answer index. Therefore, we can calculate this
      // value via `QQ * 100 + NN`.
      // Note: The `ParseAnswer` function guarantees that the score will be
      // in the range [1, 100].
      int enumeration = (question * 100) + score;
      LogHistogram(histogram_name, enumeration);
    }

    return false;  // Don't close the page.
  }

  // Future proof - ignore unimplemented commands.
  return false;
}

HatsDialog::HatsDialog(const std::string& trigger_id,
                       const std::string& histogram_name,
                       const std::string& site_context)
    : trigger_id_(trigger_id), histogram_name_(histogram_name) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  set_allow_default_context_menu(false);
  set_can_close(true);
  set_can_resize(false);
  set_dialog_content_url(GURL(std::string(kCrOSHaTSURL) + "?emitAnswers=true&" +
                              site_context + "&trigger=" + trigger_id_));
  set_dialog_frame_kind(ui::WebDialogDelegate::FrameKind::kDialog);
  set_dialog_modal_type(ui::mojom::ModalType::kSystem);
  set_dialog_size(gfx::Size(kDefaultWidth, kDefaultHeight));
  set_show_close_button(true);
  set_show_dialog_title(false);
}

HatsDialog::~HatsDialog() = default;

void HatsDialog::Show(const std::string& trigger_id,
                      const std::string& histogram_name,
                      const std::string& site_context) {
  // HatsDialog is self-deleting via OnDialogClosed().
  chrome::ShowWebDialog(
      nullptr, ProfileManager::GetActiveUserProfile(),
      new HatsDialog(trigger_id, histogram_name, site_context));
}

void HatsDialog::OnLoadingStateChanged(WebContents* source) {
  // Only trigger actions when the URL changes
  if (action_ != source->GetURL().ref()) {
    action_ = source->GetURL().ref();
    if (HandleClientTriggeredAction(action_, histogram_name_)) {
      source->ClosePage();
    }
  }
}

}  // namespace ash