File: mahi_manager_impl.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (1075 lines) | stat: -rw-r--r-- 41,648 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
// Copyright 2024 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/mahi/mahi_manager_impl.h"

#include <stdint.h>

#include <algorithm>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>

#include "ash/constants/ash_pref_names.h"
#include "ash/shell.h"
#include "ash/system/mahi/mahi_nudge_controller.h"
#include "ash/system/mahi/mahi_ui_controller.h"
#include "ash/webui/settings/public/constants/routes.mojom.h"
#include "ash/webui/settings/public/constants/setting.mojom.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/scoped_observation.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/unguessable_token.h"
#include "base/values.h"
#include "chrome/browser/ash/magic_boost/magic_boost_controller_ash.h"
#include "chrome/browser/ash/mahi/mahi_availability.h"
#include "chrome/browser/ash/mahi/mahi_cache_manager.h"
#include "chrome/browser/feedback/show_feedback_page.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/manta/manta_service_factory.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/settings_window_manager_chromeos.h"
#include "chromeos/components/magic_boost/public/cpp/magic_boost_state.h"
#include "chromeos/components/mahi/public/cpp/mahi_manager.h"
#include "chromeos/components/mahi/public/cpp/mahi_media_app_content_manager.h"
#include "chromeos/components/mahi/public/cpp/mahi_web_contents_manager.h"
#include "chromeos/constants/chromeos_features.h"
#include "chromeos/crosapi/mojom/magic_boost.mojom.h"
#include "chromeos/crosapi/mojom/mahi.mojom.h"
#include "chromeos/strings/grit/chromeos_strings.h"
#include "components/feedback/feedback_constants.h"
#include "components/manta/features.h"
#include "components/manta/manta_service.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/views/widget/unique_widget_ptr.h"

namespace {

// Aliases ---------------------------------------------------------------------

using chromeos::MahiGetContentResponseStatus;
using chromeos::MahiResponseStatus;
using crosapi::mojom::MahiContextMenuActionType;

// Constants -------------------------------------------------------------------

const char kMahiCacheHit[] = "ChromeOS.Mahi.CacheStateOnAccess";
const char kMahiResponseStatus[] = "ChromeOS.Mahi.ResponseStatusOnRequest";
const char kMahiProviderCreationStatus[] =
    "ChromeOS.Mahi.ProviderCreationStatus";
const char kMediaAppPDFUrlPrefix[] = "file:///media-app/";

// The following enum classes are persisted to logs. Entries should not be
// renumbered and numeric values should never be reused.

// CacheHit --------------------------------------------------------------------
enum class CacheHit {
  kNoHit = 0,
  kSummary = 1,
  kContent = 2,
  kMaxValue = kContent,
};

// Provider creation -----------------------------------------------------------
enum class ProviderCreationStatus {
  kOk = 0,
  kMantaServiceDisabled = 1,
  kProfileUnavailable = 2,
  kMantaServiceIsNull = 3,
  kMantaServiceFailedToCreate = 4,
  kMaxValue = kMantaServiceFailedToCreate,
};

void LogProviderCreationStatus(ProviderCreationStatus status) {
  base::UmaHistogramEnumeration(kMahiProviderCreationStatus, status);
}

std::optional<std::string> MaybeGetUrl(
    const crosapi::mojom::MahiPageInfoPtr& mahi_page_info) {
  // Do not send the fake URL of media app PDF files.
  return chromeos::features::IsMahiSendingUrl() &&
                 !mahi_page_info->url.spec().starts_with(kMediaAppPDFUrlPrefix)
             ? std::make_optional(mahi_page_info->url.spec())
             : std::nullopt;
}

// OnConsentStateUpdateClosureRunner -------------------------------------------

// Runs the specified closures when the consent state becomes approved or
// declined. NOTE: This class should be used only when the magic boost feature
// is enabled.
class OnConsentStateUpdateClosureRunner
    : public chromeos::MagicBoostState::Observer {
 public:
  OnConsentStateUpdateClosureRunner(base::OnceClosure on_approved_closure,
                                    base::OnceClosure on_declined_closure)
      : on_approved_closure_(std::move(on_approved_closure)),
        on_declined_closure_(std::move(on_declined_closure)) {
    CHECK(chromeos::MagicBoostState::Get()->IsMagicBoostAvailable());
    magic_boost_state_observation_.Observe(chromeos::MagicBoostState::Get());
  }

  OnConsentStateUpdateClosureRunner(const OnConsentStateUpdateClosureRunner&) =
      delete;
  OnConsentStateUpdateClosureRunner& operator=(
      const OnConsentStateUpdateClosureRunner&) = delete;
  ~OnConsentStateUpdateClosureRunner() override = default;

 private:
  // chromeos::MagicBoostState::Observer:
  void OnHMRConsentStatusUpdated(chromeos::HMRConsentStatus status) override {
    switch (status) {
      case chromeos::HMRConsentStatus::kApproved:
        magic_boost_state_observation_.Reset();
        std::move(on_approved_closure_).Run();
        return;
      case chromeos::HMRConsentStatus::kDeclined:
        magic_boost_state_observation_.Reset();
        std::move(on_declined_closure_).Run();
        return;
      case chromeos::HMRConsentStatus::kPendingDisclaimer:
      case chromeos::HMRConsentStatus::kUnset:
        return;
    }
  }

  void OnIsDeleting() override { magic_boost_state_observation_.Reset(); }

  // The closure that runs when the consent status becomes approved.
  base::OnceClosure on_approved_closure_;

  // The closure that runs when the consent status becomes declined.
  // NOTE: `on_declined_closure_` could destroy this observer.
  base::OnceClosure on_declined_closure_;

  base::ScopedObservation<chromeos::MagicBoostState,
                          chromeos::MagicBoostState::Observer>
      magic_boost_state_observation_{this};
};

MahiResponseStatus GetMahiResponseStatusFromMantaStatus(
    manta::MantaStatusCode code) {
  switch (code) {
    case manta::MantaStatusCode::kOk:
      return MahiResponseStatus::kSuccess;
    case manta::MantaStatusCode::kGenericError:
    case manta::MantaStatusCode::kBackendFailure:
    case manta::MantaStatusCode::kNoInternetConnection:
    case manta::MantaStatusCode::kNoIdentityManager:
      return MahiResponseStatus::kUnknownError;
    case manta::MantaStatusCode::kRestrictedCountry:
      return MahiResponseStatus::kRestrictedCountry;
    case manta::MantaStatusCode::kUnsupportedLanguage:
      return MahiResponseStatus::kUnsupportedLanguage;
    case manta::MantaStatusCode::kBlockedOutputs:
      return MahiResponseStatus::kInappropriate;
    case manta::MantaStatusCode::kResourceExhausted:
      return MahiResponseStatus::kResourceExhausted;
    case manta::MantaStatusCode::kPerUserQuotaExceeded:
      return MahiResponseStatus::kQuotaLimitHit;
    default:
      return MahiResponseStatus::kUnknownError;
  }
}

std::unique_ptr<manta::MahiProvider> CreateProvider() {
  if (!manta::features::IsMantaServiceEnabled()) {
    LogProviderCreationStatus(ProviderCreationStatus::kMantaServiceDisabled);
    return nullptr;
  }

  Profile* profile = ProfileManager::GetActiveUserProfile();
  if (!profile) {
    LogProviderCreationStatus(ProviderCreationStatus::kProfileUnavailable);
    return nullptr;
  }

  if (manta::MantaService* service =
          manta::MantaServiceFactory::GetForProfile(profile)) {
    auto provider = service->CreateMahiProvider();
    if (!provider) {
      LogProviderCreationStatus(
          ProviderCreationStatus::kMantaServiceFailedToCreate);
      return nullptr;
    }

    LogProviderCreationStatus(ProviderCreationStatus::kOk);
    return provider;
  }

  LogProviderCreationStatus(ProviderCreationStatus::kMantaServiceIsNull);
  return nullptr;
}

// Returns true if:
// 1. The magic boost feature is disabled; OR
// 2. The Mahi feature has been approved before.
bool IsMahiApproved() {
  return !chromeos::MagicBoostState::Get()->IsMagicBoostAvailable() ||
         chromeos::MagicBoostState::Get()->hmr_consent_status() ==
             chromeos::HMRConsentStatus::kApproved;
}

}  // namespace

namespace ash {

MahiManagerImpl::MahiManagerImpl()
    : cache_manager_(std::make_unique<MahiCacheManager>()),
      mahi_nudge_controller_(std::make_unique<MahiNudgeController>()) {
  magic_boost_state_observation_.Observe(chromeos::MagicBoostState::Get());
}

MahiManagerImpl::~MahiManagerImpl() {
  mahi_provider_.reset();
  mahi_web_contents_manager_ = nullptr;
}

std::u16string MahiManagerImpl::GetContentTitle() {
  return current_page_info_->title;
}

gfx::ImageSkia MahiManagerImpl::GetContentIcon() {
  return current_page_info_->favicon_image;
}

GURL MahiManagerImpl::GetContentUrl() {
  return current_page_info_->url;
}

std::u16string MahiManagerImpl::GetSelectedText() {
  return current_selected_text_.value_or(std::u16string());
}

void MahiManagerImpl::GetContent(MahiContentCallback callback) {
  if (!MaybeInitializeAndDiscardPendingRequests()) {
    std::move(callback).Run(u"", MahiGetContentResponseStatus::kUnknownError);
    LOG(ERROR) << "Initialized unsuccessfully.";
    return;
  }

  // Uses page content if it is already in the cache.
  const auto cached_content =
      cache_manager_->GetPageContentForUrl(current_page_info_->url.spec());
  if (!cached_content.empty()) {
    OnGetPageContent(current_page_info_->Clone(), std::move(callback),
                     crosapi::mojom::MahiPageContent::New(
                         /*client_id=*/base::UnguessableToken(),
                         /*page_id=*/base::UnguessableToken(), cached_content));

    base::UmaHistogramEnumeration(kMahiCacheHit, CacheHit::kContent);
    return;
  }

  base::UmaHistogramEnumeration(kMahiCacheHit, CacheHit::kNoHit);
  auto get_content_done_callback =
      base::BindOnce(&MahiManagerImpl::OnGetPageContent,
                     weak_ptr_factory_for_requests_.GetWeakPtr(),
                     current_page_info_->Clone(), std::move(callback));

  if (media_app_pdf_focused_) {
    chromeos::MahiMediaAppContentManager::Get()->GetContent(
        media_app_client_id_, std::move(get_content_done_callback));
  } else {
    mahi_web_contents_manager_->RequestContent(
        current_page_info_->page_id, std::move(get_content_done_callback));
  }
}

void MahiManagerImpl::GetSummary(MahiSummaryCallback callback) {
  // Resets latest_elucidation_ to avoid messing up the feedback body.
  latest_elucidation_ = std::u16string();

  if (!MaybeInitializeAndDiscardPendingRequests()) {
    latest_response_status_ = MahiResponseStatus::kUnknownError;
    std::move(callback).Run(u"", latest_response_status_);
    LOG(ERROR) << "Initialized unsuccessfully.";
    return;
  }

  current_panel_info_ = current_page_info_->Clone();

  const auto cached_content =
      cache_manager_->GetPageContentForUrl(current_panel_info_->url.spec());

  // Uses the cached summary only if the request is for the whole page
  // (`current_selected_text_` is nullopt).
  const auto cached_summary =
      cache_manager_->GetSummaryForUrl(current_panel_info_->url.spec());
  if (current_selected_text_ == std::nullopt && cached_summary.has_value()) {
    current_panel_content_ = crosapi::mojom::MahiPageContent::New(
        /*client_id=*/base::UnguessableToken(),
        /*page_id=*/base::UnguessableToken(), cached_content);
    current_panel_qa_.clear();

    // TODO(b:338140794): consider loading the QA cache here as well.

    latest_summary_ = cached_summary.value();
    latest_response_status_ = MahiResponseStatus::kSuccess;
    std::move(callback).Run(cached_summary.value(),
                            MahiResponseStatus::kSuccess);

    base::UmaHistogramEnumeration(kMahiCacheHit, CacheHit::kSummary);
    return;
  }

  // Uses page content if it is already in the cache.
  if (!cached_content.empty()) {
    OnGetPageContentForSummary(
        current_panel_info_->Clone(), std::move(callback),
        crosapi::mojom::MahiPageContent::New(
            /*client_id=*/base::UnguessableToken(),
            /*page_id=*/base::UnguessableToken(), cached_content));

    base::UmaHistogramEnumeration(kMahiCacheHit, CacheHit::kContent);
    return;
  }

  base::UmaHistogramEnumeration(kMahiCacheHit, CacheHit::kNoHit);
  auto get_content_done_callback =
      base::BindOnce(&MahiManagerImpl::OnGetPageContentForSummary,
                     weak_ptr_factory_for_requests_.GetWeakPtr(),
                     current_panel_info_->Clone(), std::move(callback));

  if (media_app_pdf_focused_) {
    chromeos::MahiMediaAppContentManager::Get()->GetContent(
        media_app_client_id_, std::move(get_content_done_callback));
  } else {
    mahi_web_contents_manager_->RequestContent(
        current_page_info_->page_id, std::move(get_content_done_callback));
  }
}

void MahiManagerImpl::GetElucidation(MahiElucidationCallback callback) {
  // Resets latest_summary_ to avoid messing up feedback.
  latest_summary_ = std::u16string();

  if (!MaybeInitializeAndDiscardPendingRequests()) {
    latest_response_status_ = MahiResponseStatus::kUnknownError;
    std::move(callback).Run(u"", latest_response_status_);
    LOG(ERROR) << "Initialized unsuccessfully.";
    return;
  }

  current_panel_info_ = current_page_info_->Clone();

  // Do not CHECK and crash here. It's true that Elucidation button should only
  // show when the selected text passed the eligiblity check, but this may also
  // be called by clicking `retry` link when error happens, and because of
  // crbug.com/375292907, the current_selected_text may change and not eligible
  // anymore (e.g. becomes empty). In such cases we returns an error.
  if (current_selected_text_->empty()) {
    std::move(callback).Run(u"", MahiResponseStatus::kInappropriate);
    return;
  }

  const auto cached_content =
      cache_manager_->GetPageContentForUrl(current_panel_info_->url.spec());

  // Uses page content if it is already in the cache.
  if (!cached_content.empty()) {
    OnGetPageContentForElucidation(
        current_selected_text_.value(), current_panel_info_->Clone(),
        std::move(callback),
        crosapi::mojom::MahiPageContent::New(
            /*client_id=*/base::UnguessableToken(),
            /*page_id=*/base::UnguessableToken(), cached_content));

    base::UmaHistogramEnumeration(kMahiCacheHit, CacheHit::kContent);
    return;
  }

  base::UmaHistogramEnumeration(kMahiCacheHit, CacheHit::kNoHit);
  auto get_content_done_callback =
      base::BindOnce(&MahiManagerImpl::OnGetPageContentForElucidation,
                     weak_ptr_factory_for_requests_.GetWeakPtr(),
                     current_selected_text_.value(),
                     current_panel_info_->Clone(), std::move(callback));

  if (media_app_pdf_focused_) {
    chromeos::MahiMediaAppContentManager::Get()->GetContent(
        media_app_client_id_, std::move(get_content_done_callback));
  } else {
    mahi_web_contents_manager_->RequestContent(
        current_page_info_->page_id, std::move(get_content_done_callback));
  }
}

void MahiManagerImpl::GetOutlines(MahiOutlinesCallback callback) {
  std::vector<chromeos::MahiOutline> outlines;
  for (int i = 0; i < 5; i++) {
    outlines.emplace_back(
        chromeos::MahiOutline(i, u"Outline " + base::NumberToString16(i)));
  }
  std::move(callback).Run(outlines, MahiResponseStatus::kSuccess);
}

void MahiManagerImpl::GoToOutlineContent(int outline_id) {}

void MahiManagerImpl::AnswerQuestion(const std::u16string& question,
                                     bool current_panel_content,
                                     MahiAnswerQuestionCallback callback) {
  if (!MaybeInitializeAndDiscardPendingRequests()) {
    latest_response_status_ = MahiResponseStatus::kUnknownError;
    std::move(callback).Run(u"", latest_response_status_);
    LOG(ERROR) << "Initialized unsuccessfully.";
    return;
  }

  if (current_panel_content) {
    mahi_provider_->QuestionAndAnswer(
        base::UTF16ToUTF8(current_panel_content_->page_content),
        base::UTF16ToUTF8(current_panel_info_->title),
        MaybeGetUrl(current_page_info_), current_panel_qa_,
        base::UTF16ToUTF8(question),
        base::BindOnce(&MahiManagerImpl::OnMahiProviderQAResponse,
                       weak_ptr_factory_for_requests_.GetWeakPtr(),
                       current_panel_info_->Clone(), question,
                       std::move(callback)));
    return;
  }

  current_panel_info_ = current_page_info_->Clone();

  // Uses page content if it is already in the cache.
  const auto cached_content =
      cache_manager_->GetPageContentForUrl(current_panel_info_->url.spec());
  if (!cached_content.empty()) {
    OnGetPageContentForQA(
        current_panel_info_->Clone(), question, std::move(callback),
        crosapi::mojom::MahiPageContent::New(
            /*client_id=*/base::UnguessableToken(),
            /*page_id=*/base::UnguessableToken(), cached_content));

    base::UmaHistogramEnumeration(kMahiCacheHit, CacheHit::kContent);
    return;
  }

  base::UmaHistogramEnumeration(kMahiCacheHit, CacheHit::kNoHit);
  auto get_content_done_callback = base::BindOnce(
      &MahiManagerImpl::OnGetPageContentForQA,
      weak_ptr_factory_for_requests_.GetWeakPtr(), current_panel_info_->Clone(),
      question, std::move(callback));
  if (media_app_pdf_focused_) {
    chromeos::MahiMediaAppContentManager::Get()->GetContent(
        media_app_client_id_, std::move(get_content_done_callback));
  } else {
    mahi_web_contents_manager_->RequestContent(
        current_panel_info_->page_id, std::move(get_content_done_callback));
  }
}

void MahiManagerImpl::GetSuggestedQuestion(
    MahiGetSuggestedQuestionCallback callback) {
  std::move(callback).Run(u"test suggested question",
                          MahiResponseStatus::kSuccess);
}

void MahiManagerImpl::SetCurrentFocusedPageInfo(
    crosapi::mojom::MahiPageInfoPtr info) {
  media_app_pdf_focused_ = false;
  // TODO(crbug.com/318565610): consider adding default icon when there is no
  // icon available.
  current_page_info_ = std::move(info);

  const bool availability =
      current_page_info_->IsDistillable.value_or(false) &&
      !current_panel_info_->url.EqualsIgnoringRef(current_page_info_->url);
  NotifyRefreshAvailability(/*available=*/availability);
}

void MahiManagerImpl::OnContextMenuClicked(
    crosapi::mojom::MahiContextMenuRequestPtr context_menu_request) {
  const MahiContextMenuActionType action_type =
      context_menu_request->action_type;

  // Show a disclaimer view before fulfilling `context_menu_request` if:
  // 1. Mahi is not approved by user; AND
  // 2. `context_menu_request` is NOT related to the Mahi settings. User
  //    is allowed to access the Mahi settings before approval.
  if (!IsMahiApproved() &&
      action_type != MahiContextMenuActionType::kSettings) {
    InterrputRequestHandlingWithDisclaimerView(std::move(context_menu_request));
    return;
  }

  switch (action_type) {
    case MahiContextMenuActionType::kElucidation:
      // Retrieves selected text from corresponding content manager, so that the
      // result panel ui can get this info from Mahi Manager directly instead of
      // contacting content managers.
      UpdateCurrentSelectedText();

      OpenMahiPanelForElucidation(
          context_menu_request->display_id,
          context_menu_request->mahi_menu_bounds.has_value()
              ? context_menu_request->mahi_menu_bounds.value()
              : gfx::Rect());
      return;
    case MahiContextMenuActionType::kSummary:
      current_selected_text_ = std::nullopt;
      OpenMahiPanel(context_menu_request->display_id,
                    context_menu_request->mahi_menu_bounds.has_value()
                        ? context_menu_request->mahi_menu_bounds.value()
                        : gfx::Rect());
      return;
    case MahiContextMenuActionType::kSummaryOfSelection:
      UpdateCurrentSelectedText();
      OpenMahiPanel(context_menu_request->display_id,
                    context_menu_request->mahi_menu_bounds.has_value()
                        ? context_menu_request->mahi_menu_bounds.value()
                        : gfx::Rect());
      return;
    case MahiContextMenuActionType::kQA:
      current_selected_text_ = std::nullopt;
      OpenMahiPanel(context_menu_request->display_id,
                    context_menu_request->mahi_menu_bounds.has_value()
                        ? context_menu_request->mahi_menu_bounds.value()
                        : gfx::Rect());

      // Ask question.
      // TODO(b/331837721): `MahiManagerImpl` should own an instance of
      // `MahiUiController` and use it to answer question here. This
      // functionality shouldn't need to be routed through the widget. We also
      // need to add unit test logic for this after the refactor.
      if (!context_menu_request->question) {
        return;
      }

      if (!ui_controller_.IsMahiPanelOpen()) {
        return;
      }

      // Because we call `MahiUiController::SendQuestion` right after
      // opening the panel here, `SendQuestion` will cancel the call to get
      // summary due to `MahiUiController::InvalidatePendingRequests()`. Thus,
      // we need to update the summary after answering the question to make sure
      // that user gets summary when navigating back to the summary UI
      // (b/345621992).
      // When the user sends a question from the context menu, we treat it as
      // the start of a new journey, so we set `current_panel_content` false.
      ui_controller_.SendQuestion(
          context_menu_request->question.value(),
          /*current_panel_content=*/false,
          MahiUiController::QuestionSource::kMenuView,
          /*update_summary_after_answer_question=*/true);

      return;
    case MahiContextMenuActionType::kSettings:
      chrome::SettingsWindowManager::GetInstance()->ShowOSSettings(
          ProfileManager::GetActiveUserProfile(),
          chromeos::settings::mojom::kSystemPreferencesSectionPath,
          chromeos::settings::mojom::Setting::kMahiOnOff);
      return;
    case MahiContextMenuActionType::kNone:
    case MahiContextMenuActionType::kOutline:
      // TODO(b/318565610): Update the behaviour of kOutline.
      return;
  }
}

void MahiManagerImpl::OpenFeedbackDialog() {
  std::string description_template = base::StringPrintf(
      "#Mahi user feedback:\n\n-----------\nlatest status code: %d",
      static_cast<int>(latest_response_status_));

  if (!latest_summary_.empty()) {
    base::StringAppendF(&description_template, "\nlatest summary: %s",
                        base::UTF16ToUTF8(latest_summary_).c_str());

    if (current_selected_text_ != std::nullopt) {
      base::StringAppendF(
          &description_template, "\n\nfor the selected text: %s",
          base::UTF16ToUTF8(current_selected_text_.value()).c_str());
    }

    if (!current_panel_qa_.empty()) {
      base::StringAppendF(&description_template, "\n\nQA history:");
      for (const auto& [question, answer] : current_panel_qa_) {
        base::StringAppendF(&description_template, "\nQ:%s\nA:%s\n",
                            question.c_str(), answer.c_str());
      }
    }
  } else if (!latest_elucidation_.empty()) {
    base::StringAppendF(
        &description_template,
        "\nlatest simplified text: %s\n\nfor the selected text: %s\n",
        base::UTF16ToUTF8(latest_elucidation_).c_str(),
        base::UTF16ToUTF8(current_selected_text_.value()).c_str());
  }

  base::Value::Dict ai_metadata;
  ai_metadata.Set(feedback::kMahiMetadataKey, "true");

  chrome::ShowFeedbackPage(
      /*browser=*/chrome::FindBrowserWithProfile(
          ProfileManager::GetActiveUserProfile()),
      /*source=*/feedback::kFeedbackSourceAI, description_template,
      /*description_placeholder_text=*/
      base::UTF16ToUTF8(
          l10n_util::GetStringUTF16(IDS_MAHI_FEEDBACK_PLACEHOLDER)),
      /*category_tag=*/"mahi",
      /*extra_diagnostics=*/std::string(),
      /*autofill_metadata=*/base::Value::Dict(), std::move(ai_metadata));
}

void MahiManagerImpl::OpenMahiPanel(int64_t display_id,
                                    const gfx::Rect& mahi_menu_bounds) {
  ui_controller_.OpenMahiPanel(display_id, mahi_menu_bounds,
                               /*elucidation_in_use=*/false);
}

void MahiManagerImpl::OpenMahiPanelForElucidation(
    int64_t display_id,
    const gfx::Rect& mahi_menu_bounds) {
  ui_controller_.OpenMahiPanel(display_id, mahi_menu_bounds,
                               /*elucidation_in_use=*/true);
}

bool MahiManagerImpl::IsEnabled() {
  return mahi_availability::IsMahiAvailable() &&
         chromeos::MagicBoostState::Get()->hmr_enabled().value_or(false);
}

void MahiManagerImpl::SetMediaAppPDFFocused() {
  chromeos::MahiMediaAppContentManager* media_app_content_manager =
      chromeos::MahiMediaAppContentManager::Get();
  CHECK(media_app_content_manager);

  bool old_media_app_pdf_focused = media_app_pdf_focused_;
  base::UnguessableToken old_media_app_client_id = media_app_client_id_;
  const std::u16string old_title = current_page_info_->title;

  media_app_client_id_ = media_app_content_manager->active_client_id();
  media_app_pdf_focused_ = true;
  std::optional<std::string> file_name =
      media_app_content_manager->GetFileName(media_app_client_id_);
  CHECK(file_name.has_value());

  // Fits the media app page info into a MahiPageInfoPtr.
  // Particularly, makes up a GURL with the file name.
  // TODO(b:338140794): Two file with the same name can hit the same cache.
  // Need to find a way to fix this.
  current_page_info_ = crosapi::mojom::MahiPageInfo::New(
      media_app_client_id_,
      /*page_id=*/media_app_client_id_,
      GURL{base::StrCat({kMediaAppPDFUrlPrefix, file_name.value()})},
      /*title=*/base::UTF8ToUTF16(file_name.value()), gfx::ImageSkia(),
      /*distillable=*/true, /*is_incognito=*/false);

  // To avoid refresh banner flicker. This could happen when a new PDF file is
  // opened from file picker dialog in media app.
  if (old_media_app_pdf_focused &&
      old_media_app_client_id == media_app_client_id_ &&
      current_page_info_->title == old_title) {
    return;
  }

  const bool availability =
      !current_panel_info_->url.EqualsIgnoringRef(current_page_info_->url);
  NotifyRefreshAvailability(/*available=*/availability);
}

void MahiManagerImpl::MediaAppPDFClosed(
    const base::UnguessableToken media_app_client_id) {
  if (media_app_pdf_focused_ && media_app_client_id_ == media_app_client_id &&
      current_page_info_->client_id == media_app_client_id) {
    // In this case if there's a refresh banner, it must be targeted to
    // the destroying media app PDF. Hides it by a false notification.
    NotifyRefreshAvailability(/*available=*/false);
    current_page_info_ = crosapi::mojom::MahiPageInfo::New();
  }

  media_app_pdf_focused_ = false;
  media_app_client_id_ = base::UnguessableToken::Null();
}

std::optional<base::UnguessableToken> MahiManagerImpl::GetMediaAppPDFClientId()
    const {
  if (media_app_pdf_focused_) {
    return media_app_client_id_;
  }
  return std::nullopt;
}

void MahiManagerImpl::ClearCache() {
  cache_manager_->ClearCache();
}

void MahiManagerImpl::NotifyRefreshAvailability(bool available) {
  // Do not notify if the result on the panel is based on the user selected
  // text, because clicking the refresh banner will update the panel with
  // summary of the new whole document, which is not consistent with the current
  // purpose of the panel.
  if (ui_controller_.IsMahiPanelOpen() &&
      current_selected_text_ == std::nullopt) {
    ui_controller_.NotifyRefreshAvailabilityChanged(available);
  }

  // Attempt showing an educational nudge when users visit eligible content.
  if (available) {
    mahi_nudge_controller_->MaybeShowNudge();
  }
}

void MahiManagerImpl::OnHistoryDeletions(
    history::HistoryService* history_service,
    const history::DeletionInfo& deletion_info) {
  // If IsAllHistory() returns true, all URLs are deleted and `deleted_rows()`
  //  and `favicon_urls()` are undefined.
  if (deletion_info.IsAllHistory()) {
    cache_manager_->ClearCache();
  } else {
    for (const auto& row : deletion_info.deleted_rows()) {
      cache_manager_->DeleteCacheForUrl(row.url().spec());
    }
  }
}

void MahiManagerImpl::OnHMREnabledUpdated(bool enabled) {
  if (enabled) {
    return;
  }
  ui_controller_.CloseMahiPanel();
  cache_manager_->ClearCache();
}

void MahiManagerImpl::OnIsDeleting() {
  magic_boost_state_observation_.Reset();
}

bool MahiManagerImpl::MaybeInitializeAndDiscardPendingRequests() {
  if (!mahi_provider_) {
    mahi_provider_ = CreateProvider();
  }

  if (!mahi_web_contents_manager_) {
    mahi_web_contents_manager_ = chromeos::MahiWebContentsManager::Get();
  }

  if (weak_ptr_factory_for_requests_.HasWeakPtrs()) {
    weak_ptr_factory_for_requests_.InvalidateWeakPtrs();
  }

  MaybeObserveHistoryService();

  return mahi_provider_ != nullptr && mahi_web_contents_manager_ != nullptr;
}

void MahiManagerImpl::MaybeObserveHistoryService() {
  Profile* profile = ProfileManager::GetActiveUserProfile();
  if (!profile) {
    return;
  }

  history::HistoryService* service =
      HistoryServiceFactory::GetForProfileWithoutCreating(profile);
  if (service && !scoped_history_service_observer_.IsObserving()) {
    scoped_history_service_observer_.Observe(service);
  }
}

void MahiManagerImpl::InterrputRequestHandlingWithDisclaimerView(
    crosapi::mojom::MahiContextMenuRequestPtr context_menu_request) {
  CHECK(chromeos::MagicBoostState::Get()->IsMagicBoostAvailable());

  // Cache the display id before moving `context_menu_request`.
  const int64_t display_id = context_menu_request->display_id;

  // Invalidate the closures of the existing closure runner, if any.
  weak_ptr_factory_for_closure_runner_.InvalidateWeakPtrs();

  // The closure that resets `on_consent_state_update_closure_runner_`.
  base::RepeatingClosure reset_observer_closure = base::BindRepeating(
      [](const base::WeakPtr<MahiManagerImpl>& weak_ptr) {
        if (weak_ptr) {
          weak_ptr->on_consent_state_update_closure_runner_.reset();
        }
      },
      weak_ptr_factory_for_closure_runner_.GetWeakPtr());

  on_consent_state_update_closure_runner_ =
      std::make_unique<OnConsentStateUpdateClosureRunner>(
          /*on_approved_closure=*/
          base::BindOnce(&MahiManagerImpl::OnContextMenuClicked,
                         weak_ptr_factory_for_closure_runner_.GetWeakPtr(),
                         std::move(context_menu_request))
              .Then(reset_observer_closure),
          /*on_declined_closure=*/reset_observer_closure);

  ash::MagicBoostControllerAsh::Get()->ShowDisclaimerUi(
      display_id,
      crosapi::mojom::MagicBoostController::TransitionAction::kDoNothing,
      chromeos::MagicBoostState::Get()->ShouldIncludeOrcaInOptInSync()
          ? OptInFeatures::kOrcaAndHmr
          : OptInFeatures::kHmrOnly);
}

void MahiManagerImpl::OnGetPageContent(
    crosapi::mojom::MahiPageInfoPtr request_page_info,
    MahiContentCallback callback,
    crosapi::mojom::MahiPageContentPtr mahi_content_ptr) {
  if (!mahi_content_ptr || mahi_content_ptr->page_content.empty()) {
    std::move(callback).Run(
        u"", MahiGetContentResponseStatus::kContentExtractionError);
    // TODO(b:371080356) add histogram metrics.
    return;
  }

  // Cache current panel content.
  CacheCurrentPanelContent(*request_page_info, *mahi_content_ptr);

  std::move(callback).Run(mahi_content_ptr->page_content,
                          MahiGetContentResponseStatus::kSuccess);
}

void MahiManagerImpl::OnGetPageContentForSummary(
    crosapi::mojom::MahiPageInfoPtr request_page_info,
    MahiSummaryCallback callback,
    crosapi::mojom::MahiPageContentPtr mahi_content_ptr) {
  if (!mahi_content_ptr || mahi_content_ptr->page_content.empty()) {
    latest_response_status_ = MahiResponseStatus::kContentExtractionError;
    std::move(callback).Run(u"", latest_response_status_);
    base::UmaHistogramEnumeration(kMahiResponseStatus, latest_response_status_);
    return;
  }

  // Assign current panel content and clear the current panel QA
  current_panel_content_ = std::move(mahi_content_ptr);
  current_panel_qa_.clear();

  CacheCurrentPanelContent(*request_page_info, *current_panel_content_);

  // Summarizes the `current_selected_text_` if it's not null, otherwise
  // summarizes the whole page content.
  // Note: we get the page content even if the `current_selected_text_` is not
  // null. The page content is not used for the summary request in such cases,
  // but it's necessary to be present because questions sent from the result
  // panel relies on it.
  const std::u16string text_to_summary =
      current_selected_text_.value_or(current_panel_content_->page_content);
  if (text_to_summary.empty()) {
    latest_response_status_ = MahiResponseStatus::kInappropriate;
    std::move(callback).Run(u"", latest_response_status_);
    return;
  }

  std::optional<std::string> context = std::nullopt;
  if (current_selected_text_ != std::nullopt) {
    context = base::UTF16ToUTF8(current_panel_content_->page_content);
  }

  CHECK(mahi_provider_);
  mahi_provider_->Summarize(
      base::UTF16ToUTF8(text_to_summary),
      base::UTF16ToUTF8(request_page_info->title), context,
      MaybeGetUrl(request_page_info),
      base::BindOnce(&MahiManagerImpl::OnMahiProviderSummaryResponse,
                     weak_ptr_factory_for_requests_.GetWeakPtr(),
                     std::move(request_page_info), std::move(callback)));
}

void MahiManagerImpl::OnGetPageContentForElucidation(
    const std::u16string& selected_text,
    crosapi::mojom::MahiPageInfoPtr request_page_info,
    MahiElucidationCallback callback,
    crosapi::mojom::MahiPageContentPtr mahi_content_ptr) {
  if (!mahi_content_ptr || mahi_content_ptr->page_content.empty()) {
    latest_response_status_ = MahiResponseStatus::kContentExtractionError;
    std::move(callback).Run(u"", latest_response_status_);
    base::UmaHistogramEnumeration(kMahiResponseStatus, latest_response_status_);
    return;
  }

  // Assign current panel content and clear the current panel QA
  current_panel_content_ = std::move(mahi_content_ptr);
  current_panel_qa_.clear();
  CacheCurrentPanelContent(*request_page_info, *current_panel_content_);

  CHECK(mahi_provider_);

  mahi_provider_->Elucidate(
      base::UTF16ToUTF8(selected_text),
      base::UTF16ToUTF8(current_panel_content_->page_content),
      base::UTF16ToUTF8(request_page_info->title),
      MaybeGetUrl(request_page_info),
      base::BindOnce(&MahiManagerImpl::OnMahiProviderElucidationResponse,
                     weak_ptr_factory_for_requests_.GetWeakPtr(),
                     std::move(request_page_info), selected_text,
                     std::move(callback)));
}

void MahiManagerImpl::OnGetPageContentForQA(
    crosapi::mojom::MahiPageInfoPtr request_page_info,
    const std::u16string& question,
    MahiAnswerQuestionCallback callback,
    crosapi::mojom::MahiPageContentPtr mahi_content_ptr) {
  if (!mahi_content_ptr || mahi_content_ptr->page_content.empty()) {
    latest_response_status_ = MahiResponseStatus::kContentExtractionError;
    std::move(callback).Run(std::nullopt, latest_response_status_);
    base::UmaHistogramEnumeration(kMahiResponseStatus, latest_response_status_);
    return;
  }

  // Assign current panel content and clear the current panel QA
  current_panel_content_ = std::move(mahi_content_ptr);
  current_panel_qa_.clear();

  CacheCurrentPanelContent(*request_page_info, *current_panel_content_);

  mahi_provider_->QuestionAndAnswer(
      base::UTF16ToUTF8(current_panel_content_->page_content),
      base::UTF16ToUTF8(request_page_info->title),
      MaybeGetUrl(request_page_info), current_panel_qa_,
      base::UTF16ToUTF8(question),
      base::BindOnce(&MahiManagerImpl::OnMahiProviderQAResponse,
                     weak_ptr_factory_for_requests_.GetWeakPtr(),
                     std::move(request_page_info), question,
                     std::move(callback)));
}

void MahiManagerImpl::OnMahiProviderSummaryResponse(
    crosapi::mojom::MahiPageInfoPtr request_page_info,
    MahiSummaryCallback summary_callback,
    base::Value::Dict dict,
    manta::MantaStatus status) {
  latest_summary_ = u"...";
  if (status.status_code != manta::MantaStatusCode::kOk) {
    latest_response_status_ =
        GetMahiResponseStatusFromMantaStatus(status.status_code);
    std::move(summary_callback)
        .Run(u"Couldn't get summary", latest_response_status_);
    base::UmaHistogramEnumeration(kMahiResponseStatus, latest_response_status_);
    return;
  }

  if (auto* text = dict.FindString("outputData")) {
    latest_response_status_ = MahiResponseStatus::kSuccess;
    latest_summary_ = base::UTF8ToUTF16(*text);

    // Caches the summary if it is not for the selected text.
    if (current_selected_text_ == std::nullopt) {
      cache_manager_->TryToUpdateSummaryForUrl(request_page_info->url.spec(),
                                               latest_summary_);
    }

    std::move(summary_callback).Run(latest_summary_, latest_response_status_);
  } else {
    latest_response_status_ = MahiResponseStatus::kCantFindOutputData;
    std::move(summary_callback)
        .Run(u"Cannot find output data", latest_response_status_);
  }

  base::UmaHistogramEnumeration(kMahiResponseStatus, latest_response_status_);
}

void MahiManagerImpl::OnMahiProviderElucidationResponse(
    crosapi::mojom::MahiPageInfoPtr request_page_info,
    const std::u16string& selected_text,
    MahiElucidationCallback elucidation_callback,
    base::Value::Dict dict,
    manta::MantaStatus status) {
  CHECK(current_selected_text_.value_or(u"") == selected_text);

  latest_elucidation_ = u"...";
  if (status.status_code != manta::MantaStatusCode::kOk) {
    latest_response_status_ =
        GetMahiResponseStatusFromMantaStatus(status.status_code);
    std::move(elucidation_callback)
        .Run(u"Couldn't get elucidation", latest_response_status_);
    base::UmaHistogramEnumeration(kMahiResponseStatus, latest_response_status_);
    return;
  }

  if (auto* text = dict.FindString("outputData")) {
    latest_response_status_ = MahiResponseStatus::kSuccess;
    latest_elucidation_ = base::UTF8ToUTF16(*text);
    std::move(elucidation_callback)
        .Run(latest_elucidation_, latest_response_status_);
  } else {
    latest_response_status_ = MahiResponseStatus::kCantFindOutputData;
    std::move(elucidation_callback)
        .Run(u"Cannot find output data", latest_response_status_);
  }

  base::UmaHistogramEnumeration(kMahiResponseStatus, latest_response_status_);
}

void MahiManagerImpl::OnMahiProviderQAResponse(
    crosapi::mojom::MahiPageInfoPtr request_page_info,
    const std::u16string& question,
    MahiAnswerQuestionCallback callback,
    base::Value::Dict dict,
    manta::MantaStatus status) {
  if (status.status_code != manta::MantaStatusCode::kOk) {
    latest_response_status_ =
        GetMahiResponseStatusFromMantaStatus(status.status_code);
    current_panel_qa_.emplace_back(base::UTF16ToUTF8(question), "");
    std::move(callback).Run(std::nullopt, latest_response_status_);
    base::UmaHistogramEnumeration(kMahiResponseStatus, latest_response_status_);
    return;
  }

  if (auto* text = dict.FindString("outputData")) {
    latest_response_status_ = MahiResponseStatus::kSuccess;
    current_panel_qa_.emplace_back(base::UTF16ToUTF8(question), *text);
    std::move(callback).Run(base::UTF8ToUTF16(*text), latest_response_status_);
  } else {
    latest_response_status_ = MahiResponseStatus::kCantFindOutputData;
    std::move(callback).Run(std::nullopt, latest_response_status_);
  }
  base::UmaHistogramEnumeration(kMahiResponseStatus, latest_response_status_);
}

void MahiManagerImpl::CacheCurrentPanelContent(
    crosapi::mojom::MahiPageInfo request_page_info,
    crosapi::mojom::MahiPageContent mahi_content) {
  // Add page content to the cache.
  // TODO(b:338140794): consider adding the QA to the cache.
  if (!request_page_info.is_incognito) {
    cache_manager_->AddCacheForUrl(
        request_page_info.url.spec(),
        MahiCacheManager::MahiData(
            request_page_info.url.spec(), request_page_info.title,
            mahi_content.page_content, request_page_info.favicon_image,
            /*summary=*/std::nullopt,
            /*previous_qa=*/{}));
  }
}

void MahiManagerImpl::UpdateCurrentSelectedText() {
  if (media_app_pdf_focused_) {
    current_selected_text_ = base::UTF8ToUTF16(
        chromeos::MahiMediaAppContentManager::Get()->GetSelectedText());
  } else {
    current_selected_text_ =
        chromeos::MahiWebContentsManager::Get()->GetSelectedText();
  }
}

// Repeating answers are not allowed for Mahi as all questions must only return
// one answer.
bool MahiManagerImpl::AllowRepeatingAnswers() {
  return false;
}

// This function will never be called as consecutive answers are not allowed for
// Mahi.
void MahiManagerImpl::AnswerQuestionRepeating(
    const std::u16string& question,
    bool current_panel_content,
    MahiAnswerQuestionCallbackRepeating callback) {}

}  // namespace ash