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
|
// 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 "components/javascript_dialogs/core/dialog_util.h"
#include "base/i18n/rtl.h"
#include "components/strings/grit/components_strings.h"
#include "components/url_formatter/elide_url.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"
#include "url/origin.h"
#include "url/scheme_host_port.h"
namespace javascript_dialogs::util {
// If an origin is opaque but has a precursor, then returns the precursor
// origin. If the origin is not opaque, returns it unchanged. Unwrapping origins
// allows the dialog code to provide the user with a clearer picture of which
// page is actually showing the dialog.
url::Origin UnwrapOriginIfOpaque(const url::Origin& origin) {
if (!origin.opaque()) {
return origin;
}
const url::SchemeHostPort& precursor =
origin.GetTupleOrPrecursorTupleIfOpaque();
if (!precursor.IsValid()) {
return origin;
}
return url::Origin::CreateFromNormalizedTuple(
precursor.scheme(), precursor.host(), precursor.port());
}
std::u16string DialogTitle(const url::Origin& main_frame_origin,
const url::Origin& alerting_frame_origin) {
// Note that `Origin::Create()` handles unwrapping of `blob:` and
// `filesystem:` schemed URLs, so no special handling is needed for that.
// However, origins can be opaque but have precursors that are origins that a
// user would be able to make sense of, so do unwrapping for that.
const url::Origin unwrapped_main_frame_origin =
UnwrapOriginIfOpaque(main_frame_origin);
const url::Origin unwrapped_alerting_frame_origin =
UnwrapOriginIfOpaque(alerting_frame_origin);
bool is_same_origin_as_main_frame =
unwrapped_alerting_frame_origin.IsSameOriginWith(
unwrapped_main_frame_origin);
if (unwrapped_alerting_frame_origin.GetURL().IsStandard() &&
!unwrapped_alerting_frame_origin.GetURL().SchemeIsFile()) {
std::u16string origin_string =
url_formatter::FormatOriginForSecurityDisplay(
unwrapped_alerting_frame_origin,
url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
return l10n_util::GetStringFUTF16(
is_same_origin_as_main_frame ? IDS_JAVASCRIPT_MESSAGEBOX_TITLE
: IDS_JAVASCRIPT_MESSAGEBOX_TITLE_IFRAME,
base::i18n::GetDisplayStringInLTRDirectionality(origin_string));
}
return l10n_util::GetStringUTF16(
is_same_origin_as_main_frame
? IDS_JAVASCRIPT_MESSAGEBOX_TITLE_NONSTANDARD_URL
: IDS_JAVASCRIPT_MESSAGEBOX_TITLE_NONSTANDARD_URL_IFRAME);
}
} // namespace javascript_dialogs::util
|