1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CHROMEOS_COMPONENTS_QUICK_ANSWERS_QUICK_ANSWERS_MODEL_H_
6 #define CHROMEOS_COMPONENTS_QUICK_ANSWERS_QUICK_ANSWERS_MODEL_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/strings/string16.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "ui/gfx/color_palette.h"
14 #include "ui/gfx/image/image.h"
15 
16 namespace chromeos {
17 namespace quick_answers {
18 
19 // Interaction with the consent-view (used for logging).
20 enum class NoticeInteractionType {
21   // When user clicks on the "grant-consent" button.
22   kAccept = 0,
23   // When user clicks on the "manage-settings" button.
24   kManageSettings = 1,
25   // When user otherwise dismisses or ignores the consent-view.
26   kDismiss = 2
27 };
28 
29 // The status of loading quick answers.
30 // These values are persisted to logs. Entries should not be renumbered and
31 // numeric values should never be reused.
32 // Note: Enums labels are at |QuickAnswersLoadStatus|.
33 enum class LoadStatus {
34   kSuccess = 0,
35   kNetworkError = 1,
36   kNoResult = 2,
37   kMaxValue = kNoResult,
38 };
39 
40 // The type of the result. Valid values are map to the search result types.
41 // Please see go/1ns-doc for more detail.
42 // These values are persisted to logs. Entries should not be renumbered and
43 // numeric values should never be reused.
44 // Note: Enums labels are at |QuickAnswersResultType|.
45 enum class ResultType {
46   kNoResult = 0,
47   kKnowledgePanelEntityResult = 3982,
48   kDefinitionResult = 5493,
49   kTranslationResult = 6613,
50   kUnitConversionResult = 13668,
51 };
52 
53 // The predicted intent of the request.
54 // These values are persisted to logs. Entries should not be renumbered and
55 // numeric values should never be reused.
56 // Note: Enums labels are at |QuickAnswersIntentType|.
57 enum class IntentType {
58   kUnknown = 0,
59   kUnit = 1,
60   kDictionary = 2,
61   kTranslation = 3,
62   kMaxValue = kTranslation
63 };
64 
65 enum class QuickAnswerUiElementType {
66   kUnknown = 0,
67   kText = 1,
68   kImage = 2,
69 };
70 
71 struct QuickAnswerUiElement {
QuickAnswerUiElementQuickAnswerUiElement72   explicit QuickAnswerUiElement(QuickAnswerUiElementType type) : type(type) {}
73   QuickAnswerUiElement(const QuickAnswerUiElement&) = default;
74   QuickAnswerUiElement& operator=(const QuickAnswerUiElement&) = default;
75   QuickAnswerUiElement(QuickAnswerUiElement&&) = default;
76 
77   QuickAnswerUiElementType type = QuickAnswerUiElementType::kUnknown;
78 };
79 
80 // class to describe an answer text.
81 struct QuickAnswerText : public QuickAnswerUiElement {
82   QuickAnswerText(const std::string& text, SkColor color = gfx::kGoogleGrey900)
QuickAnswerUiElementQuickAnswerText83       : QuickAnswerUiElement(QuickAnswerUiElementType::kText),
84         text(base::UTF8ToUTF16(text)),
85         color(color) {}
86 
87   base::string16 text;
88 
89   // Attributes for text style.
90   SkColor color = SK_ColorBLACK;
91 };
92 
93 struct QuickAnswerResultText : public QuickAnswerText {
94  public:
95   QuickAnswerResultText(const std::string& text,
96                         SkColor color = gfx::kGoogleGrey700)
QuickAnswerTextQuickAnswerResultText97       : QuickAnswerText(text, color) {}
98 };
99 
100 struct QuickAnswerImage : public QuickAnswerUiElement {
QuickAnswerImageQuickAnswerImage101   explicit QuickAnswerImage(const gfx::Image& image)
102       : QuickAnswerUiElement(QuickAnswerUiElementType::kImage), image(image) {}
103 
104   gfx::Image image;
105 };
106 
107 // Structure to describe a quick answer.
108 struct QuickAnswer {
109   QuickAnswer();
110   ~QuickAnswer();
111 
112   //  TODO: Remove these after we deprecate simple UI version.
113   ResultType result_type;
114   std::string primary_answer;
115   std::string secondary_answer;
116 
117   std::vector<std::unique_ptr<QuickAnswerUiElement>> title;
118   std::vector<std::unique_ptr<QuickAnswerUiElement>> first_answer_row;
119   std::vector<std::unique_ptr<QuickAnswerUiElement>> second_answer_row;
120   std::unique_ptr<QuickAnswerImage> image;
121 };
122 
123 // Information of the device that used by the user to send the request.
124 struct DeviceProperties {
125   // Device language code.
126   std::string language;
127 
128   // List (separated by comma) of user preferred languages.
129   std::string preferred_languages;
130 };
131 
132 struct IntentInfo {
133   IntentInfo();
134   IntentInfo(const IntentInfo& other);
135   IntentInfo(const std::string& intent_text,
136              IntentType intent_type,
137              const std::string& source_language = std::string(),
138              const std::string& target_language = std::string());
139   ~IntentInfo();
140 
141   // The text extracted from the selected_text associated with the intent.
142   std::string intent_text;
143 
144   // Predicted intent.
145   IntentType intent_type = IntentType::kUnknown;
146 
147   // Source and target language for translation query.
148   // These fields should only be used for translation intents.
149   std::string source_language;
150   std::string target_language;
151 };
152 
153 // Extract information generated from |QuickAnswersRequest|.
154 struct PreprocessedOutput {
155   PreprocessedOutput();
156   PreprocessedOutput(const PreprocessedOutput& other);
157   ~PreprocessedOutput();
158 
159   IntentInfo intent_info;
160 
161   // Rewritten query based on |intent_type| and |intent_text|.
162   std::string query;
163 };
164 
165 // Structure of quick answers request context, including device properties and
166 // surrounding text.
167 struct Context {
168   // Device specific properties.
169   DeviceProperties device_properties;
170 
171   std::string surrounding_text;
172 };
173 
174 // Structure to describe an quick answer request including selected content and
175 // context.
176 struct QuickAnswersRequest {
177   QuickAnswersRequest();
178   QuickAnswersRequest(const QuickAnswersRequest& other);
179   ~QuickAnswersRequest();
180 
181   // The selected Text.
182   std::string selected_text;
183 
184   // Output of processed result.
185   PreprocessedOutput preprocessed_output;
186 
187   // Context information.
188   Context context;
189 
190   // TODO(b/169346016): Add context and other targeted objects (e.g: images,
191   // links, etc).
192 };
193 
194 }  // namespace quick_answers
195 }  // namespace chromeos
196 
197 #endif  // CHROMEOS_COMPONENTS_QUICK_ANSWERS_QUICK_ANSWERS_MODEL_H_
198