Commit f62f9ea0 authored by Julian Watson's avatar Julian Watson Committed by Commit Bot

crostini: extract out function hasSupportedMimeType

More checks will be introduced for extensions, and the existing
flow control was difficult to reason about as nested loops.

Bug: 1049453
Change-Id: I05ecf0b078183986d0efc6515cab374573cd066d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2054685
Commit-Queue: Joel Hockey <joelhockey@chromium.org>
Auto-Submit: Julian Watson <juwa@google.com>
Reviewed-by: default avatarJoel Hockey <joelhockey@chromium.org>
Cr-Commit-Position: refs/heads/master@{#741335}
parent aeb9c68b
......@@ -96,6 +96,45 @@ void OnTaskComplete(FileTaskFinishedCallback done,
failure_reason);
}
bool HasSupportedMimeType(
const std::set<std::string>& supported_mime_types,
const std::string& vm_name,
const std::string& container_name,
const crostini::CrostiniMimeTypesService& mime_types_service,
const extensions::EntryInfo& entry) {
if (supported_mime_types.find(entry.mime_type) !=
supported_mime_types.end()) {
return true;
}
// Allow files with type text/* to be opened with a text/plain application
// as per xdg spec.
// https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html.
// TODO(crbug.com/1032910): Add xdg mime support for aliases, subclasses.
if (base::StartsWith(entry.mime_type, "text/",
base::CompareCase::SENSITIVE) &&
supported_mime_types.find(kUnknownTextMimeType) !=
supported_mime_types.end()) {
return true;
}
// If we see either of these then we use the Linux container MIME type
// mappings as alternates for finding an appropriate app since these are
// the defaults when Chrome can't figure out the exact MIME type (but they
// can also be the actual MIME type, so we don't exclude them above).
if (entry.mime_type == kUnknownBinaryMimeType ||
entry.mime_type == kUnknownTextMimeType) {
const std::string& alternate_mime_type =
mime_types_service.GetMimeType(entry.path, vm_name, container_name);
if (supported_mime_types.find(alternate_mime_type) !=
supported_mime_types.end()) {
return true;
}
}
return false;
}
} // namespace
void FindCrostiniApps(Profile* profile,
......@@ -109,43 +148,19 @@ void FindCrostiniApps(Profile* profile,
for (const auto& pair : registry_service->GetRegisteredApps()) {
const std::string& app_id = pair.first;
const auto& registration = pair.second;
const std::set<std::string>& supported_mime_types =
registration.MimeTypes();
bool had_unsupported_mime_type = false;
for (const extensions::EntryInfo& entry : entries) {
if (supported_mime_types.find(entry.mime_type) !=
supported_mime_types.end()) {
continue;
}
// Allow files with type text/* to be opened with a text/plain application
// as per xdg spec.
// https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html.
// TODO(crbug.com/1032910): Add xdg mime support for aliases, subclasses.
if (base::StartsWith(entry.mime_type, "text/",
base::CompareCase::SENSITIVE) &&
supported_mime_types.find(kUnknownTextMimeType) !=
supported_mime_types.end()) {
continue;
}
// If we see either of these then we use the Linux container MIME type
// mappings as alternates for finding an appropriate app since these are
// the defaults when Chrome can't figure out the exact MIME type (but they
// can also be the actual MIME type, so we don't exclude them above).
if (entry.mime_type == kUnknownBinaryMimeType ||
entry.mime_type == kUnknownTextMimeType) {
std::string alternate_mime_type = mime_types_service->GetMimeType(
entry.path, registration.VmName(), registration.ContainerName());
if (supported_mime_types.find(alternate_mime_type) !=
supported_mime_types.end()) {
continue;
}
}
had_unsupported_mime_type = true;
break;
}
if (had_unsupported_mime_type)
if (std::any_of(cbegin(entries), cend(entries),
// Get fields once, as their getters are not cheap.
[supported_mime_types = registration.MimeTypes(),
vm_name = registration.VmName(),
container_name = registration.ContainerName(),
mime_types_service](const auto& entry) {
return !HasSupportedMimeType(supported_mime_types,
vm_name, container_name,
*mime_types_service, entry);
}))
continue;
app_ids->push_back(app_id);
app_names->push_back(registration.Name());
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment