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
|
// 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.
#include "chrome/browser/ui/webui/ash/mako/mako_bubble_coordinator.h"
#include <algorithm>
#include <optional>
#include "ash/constants/ash_features.h"
#include "base/check.h"
#include "base/strings/string_util.h"
#include "base/strings/to_string.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/ash/mako/mako_consent_view.h"
#include "chrome/browser/ui/webui/ash/mako/mako_rewrite_view.h"
#include "chrome/browser/ui/webui/ash/mako/mako_ui.h"
#include "chrome/browser/ui/webui/ash/mako/url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/grit/orca_resources.h"
#include "chrome/grit/orca_resources_map.h"
#include "chromeos/components/magic_boost/public/cpp/magic_boost_state.h"
#include "chromeos/constants/chromeos_features.h"
#include "content/public/common/url_constants.h"
#include "net/base/url_util.h"
#include "ui/base/ime/ash/ime_bridge.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/views/view_utils.h"
namespace ash {
namespace {
std::string_view ToOrcaModeParamValue(MakoEditorMode mode) {
return mode == MakoEditorMode::kWrite ? kOrcaWriteMode : kOrcaRewriteMode;
}
std::string GetSystemLocale() {
return g_browser_process != nullptr
? g_browser_process->GetApplicationLocale()
: "";
}
} // namespace
MakoBubbleCoordinator::MakoBubbleCoordinator() = default;
MakoBubbleCoordinator::~MakoBubbleCoordinator() {
CloseUI();
}
void MakoBubbleCoordinator::LoadConsentUI(Profile* profile) {
GURL url = net::AppendOrReplaceQueryParameter(GURL(kChromeUIMakoPrivacyURL),
kOrcaHostLanguageParamKey,
GetSystemLocale());
contents_wrapper_ = std::make_unique<WebUIContentsWrapperT<MakoUntrustedUI>>(
GURL(kChromeUIMakoPrivacyURL), profile, IDS_ACCNAME_ORCA);
views::BubbleDialogDelegateView::CreateBubble(
std::make_unique<MakoConsentView>(contents_wrapper_.get(),
context_caret_bounds_));
}
void MakoBubbleCoordinator::LoadEditorUI(
Profile* profile,
MakoEditorMode mode,
bool can_fallback_to_center_position,
bool feedback_enabled,
std::optional<std::string_view> preset_query_id,
std::optional<std::string_view> freeform_text) {
if (IsShowingUI()) {
contents_wrapper_->CloseUI();
}
GURL url = net::AppendOrReplaceQueryParameter(GURL(kChromeUIMakoOrcaURL),
kOrcaModeParamKey,
ToOrcaModeParamValue(mode));
url = net::AppendOrReplaceQueryParameter(url, kOrcaPresetParamKey,
preset_query_id);
url = net::AppendOrReplaceQueryParameter(url, kOrcaFreeformParamKey,
freeform_text);
url = net::AppendOrReplaceQueryParameter(url, kOrcaHostLanguageParamKey,
GetSystemLocale());
url = net::AppendOrReplaceQueryParameter(url, kOrcaFeedbackEnabledParamKey,
base::ToString(feedback_enabled));
auto* magic_boost_state = chromeos::MagicBoostState::Get();
url = net::AppendOrReplaceQueryParameter(
url, kOrcaMagicBoostParamKey,
magic_boost_state && magic_boost_state->IsMagicBoostAvailable()
? "true"
: "false");
if (base::FeatureList::IsEnabled(ash::features::kOrcaResizingSupport)) {
url = net::AppendOrReplaceQueryParameter(url, kOrcaResizingEnabledParamKey,
"true");
}
contents_wrapper_ = std::make_unique<WebUIContentsWrapperT<MakoUntrustedUI>>(
url, profile, IDS_ACCNAME_ORCA,
/*esc_closes_ui=*/false);
views::BubbleDialogDelegateView::CreateBubble(
std::make_unique<MakoRewriteView>(contents_wrapper_.get(),
context_caret_bounds_,
can_fallback_to_center_position));
}
void MakoBubbleCoordinator::ShowUI() {
if (contents_wrapper_) {
contents_wrapper_->ShowUI();
}
}
void MakoBubbleCoordinator::CloseUI() {
if (contents_wrapper_) {
contents_wrapper_->CloseUI();
contents_wrapper_ = nullptr;
}
}
bool MakoBubbleCoordinator::IsShowingUI() const {
// TODO(b/301518440): To accurately check if the bubble is open, detect when
// the JS has finished loading instead of checking this pointer.
return contents_wrapper_ != nullptr &&
contents_wrapper_->GetHost() != nullptr;
}
void MakoBubbleCoordinator::CacheContextCaretBounds() {
const ui::InputMethod* input_method =
IMEBridge::Get()->GetInputContextHandler()->GetInputMethod();
if (input_method && input_method->GetTextInputClient()) {
context_caret_bounds_ =
input_method->GetTextInputClient()->GetCaretBounds();
}
}
} // namespace ash
|