File: composebox_handler.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (136 lines) | stat: -rw-r--r-- 6,146 bytes parent folder | download | duplicates (4)
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
// Copyright 2025 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/ui/webui/new_tab_page/composebox/composebox_handler.h"

#include "base/time/time.h"
#include "chrome/browser/ui/webui/new_tab_page/composebox/composebox_fieldtrial.h"
#include "components/omnibox/composebox/composebox_image_helper.h"
#include "content/public/browser/page_navigator.h"

ComposeboxHandler::ComposeboxHandler(
    mojo::PendingReceiver<composebox::mojom::PageHandler> pending_handler,
    mojo::PendingRemote<composebox::mojom::Page> pending_page,
    std::unique_ptr<ComposeboxQueryController> query_controller,
    std::unique_ptr<ComposeboxMetricsRecorder> metrics_recorder,
    content::WebContents* web_contents)
    : query_controller_(std::move(query_controller)),
      metrics_recorder_(std::move(metrics_recorder)),
      web_contents_(web_contents),
      page_{std::move(pending_page)},
      handler_(this, std::move(pending_handler)) {
  query_controller_->AddObserver(this);
}

ComposeboxHandler::~ComposeboxHandler() {
  query_controller_->RemoveObserver(this);
}

void ComposeboxHandler::NotifySessionStarted() {
  query_controller_->NotifySessionStarted();
  metrics_recorder_->NotifySessionStateChanged(SessionState::kSessionStarted);
}

void ComposeboxHandler::NotifySessionAbandoned() {
  query_controller_->NotifySessionAbandoned();
  metrics_recorder_->NotifySessionStateChanged(SessionState::kSessionAbandoned);
}

void ComposeboxHandler::SubmitQuery(const std::string& query_text,
                                    uint8_t mouse_button,
                                    bool alt_key,
                                    bool ctrl_key,
                                    bool meta_key,
                                    bool shift_key) {
  // This is the time that the user clicked the submit button, and should not
  // go any lower in this method.
  base::Time query_start_time = base::Time::Now();
  metrics_recorder_->NotifySessionStateChanged(SessionState::kQuerySubmitted);
  const WindowOpenDisposition disposition = ui::DispositionFromClick(
      /*middle_button=*/mouse_button == 1, alt_key, ctrl_key, meta_key,
      shift_key);
  OpenUrl(query_controller_->CreateAimUrl(query_text, query_start_time), disposition);
  metrics_recorder_->NotifySessionStateChanged(
      SessionState::kNavigationOccurred);
  metrics_recorder_->RecordQueryMetrics(
      query_text.size(), query_controller_->num_files_in_request());
}

void ComposeboxHandler::OpenUrl(GURL url,
                                const WindowOpenDisposition disposition) {
  content::OpenURLParams params(url, content::Referrer(), disposition,
                                ui::PAGE_TRANSITION_LINK, false);
  web_contents_->OpenURL(params, base::DoNothing());
}

void ComposeboxHandler::AddFile(
    composebox::mojom::SelectedFileInfoPtr file_info_mojom,
    mojo_base::BigBuffer file_bytes,
    AddFileCallback callback) {
  scoped_refptr<base::RefCountedBytes> file_data =
      base::MakeRefCounted<base::RefCountedBytes>(file_bytes);

  auto file_info_metadata =
      std::make_unique<ComposeboxQueryController::FileInfo>();
  file_info_metadata->file_name = file_info_mojom->file_name;
  file_info_metadata->file_size_bytes = file_bytes.size();
  file_info_metadata->webui_selection_time = file_info_mojom->selection_time;
  file_info_metadata->file_token_ = base::UnguessableToken::Create();

  std::optional<composebox::ImageEncodingOptions> image_options = std::nullopt;

  if ((file_info_mojom->mime_type).find("pdf") != std::string::npos) {
    file_info_metadata->mime_type_ = lens::MimeType::kPdf;
  } else if ((file_info_mojom->mime_type).find("image") != std::string::npos) {
    file_info_metadata->mime_type_ = lens::MimeType::kImage;
    auto image_upload_config =
        ntp_composebox::FeatureConfig::Get().config.composebox().image_upload();
    image_options = composebox::ImageEncodingOptions{
        .enable_webp_encoding = image_upload_config.enable_webp_encoding(),
        .max_size = image_upload_config.downscale_max_image_size(),
        .max_height = image_upload_config.downscale_max_image_height(),
        .max_width = image_upload_config.downscale_max_image_width(),
        .compression_quality = image_upload_config.image_compression_quality()};
  } else {
    NOTREACHED();
  }

  std::move(callback).Run(file_info_metadata->file_token_);
  metrics_recorder_->RecordFileSizeMetric(file_info_metadata->mime_type_,
                                          file_bytes.size());
  query_controller_->StartFileUploadFlow(std::move(file_info_metadata),
                                         std::move(file_data),
                                         std::move(image_options));
}

void ComposeboxHandler::DeleteFile(const base::UnguessableToken& file_token) {
  ComposeboxQueryController::FileInfo* file_info =
      query_controller_->GetFileInfo(file_token);
  lens::MimeType file_type =
      file_info ? file_info->mime_type_ : lens::MimeType::kUnknown;
  FileUploadStatus file_status = file_info ? file_info->GetFileUploadStatus()
                                           : FileUploadStatus::kNotUploaded;

  // If an UnguessabledToken that wasn't in the cache was sent, delete fails.
  // Report a bad message.
  bool success = query_controller_->DeleteFile(file_token);
  metrics_recorder_->RecordFileDeletedMetrics(success, file_type, file_status);
  if (!success) {
    handler_.ReportBadMessage("An invalid file token was sent to DeleteFile");
  }
}

void ComposeboxHandler::ClearFiles() {
  query_controller_->ClearFiles();
}

void ComposeboxHandler::OnFileUploadStatusChanged(
    const base::UnguessableToken& file_token,
    lens::MimeType mime_type,
    composebox_query::mojom::FileUploadStatus file_upload_status,
    const std::optional<FileUploadErrorType>& error_type) {
  page_->OnFileUploadStatusChanged(file_token, file_upload_status, error_type);
  metrics_recorder_->OnFileUploadStatusChanged(mime_type, file_upload_status,
                                               error_type);
}