aw_component_metrics_provider_delegate.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "android_webview/browser/metrics/aw_component_metrics_provider_delegate.h"
  5. #include <algorithm>
  6. #include <string>
  7. #include <vector>
  8. #include "android_webview/browser/metrics/aw_metrics_service_client.h"
  9. #include "android_webview/common/components/aw_apps_package_names_allowlist_component_utils.h"
  10. #include "android_webview/common/metrics/app_package_name_logging_rule.h"
  11. #include "components/component_updater/android/components_info_holder.h"
  12. #include "components/component_updater/component_updater_service.h"
  13. #include "components/metrics/component_metrics_provider.h"
  14. using component_updater::ComponentInfo;
  15. namespace android_webview {
  16. AwComponentMetricsProviderDelegate::AwComponentMetricsProviderDelegate(
  17. AwMetricsServiceClient* client)
  18. : client_(client) {
  19. DCHECK(client);
  20. }
  21. // The returned ComponentInfo have component's id and version as this is the
  22. // only info WebView keeps about components.
  23. // TODO(https://crbug.com/1228535): record the component's Omaha fingerprint.
  24. std::vector<ComponentInfo> AwComponentMetricsProviderDelegate::GetComponents() {
  25. std::vector<ComponentInfo> components =
  26. component_updater::ComponentsInfoHolder::GetInstance()->GetComponents();
  27. // WebViewAppsPackageNamesAllowlist component has a special case because it
  28. // caches the result from previous loads of the component. We want to record
  29. // the info of the component that is actually used (cached) regardless of the
  30. // info the ComponentsInfoHolder has.
  31. components.erase(
  32. std::remove_if(components.begin(), components.end(),
  33. [](const ComponentInfo& c) {
  34. return c.id ==
  35. kWebViewAppsPackageNamesAllowlistComponentId;
  36. }),
  37. components.end());
  38. absl::optional<AppPackageNameLoggingRule> record =
  39. client_->GetCachedAppPackageNameLoggingRule();
  40. if (record.has_value()) {
  41. components.push_back(
  42. ComponentInfo(kWebViewAppsPackageNamesAllowlistComponentId, "",
  43. std::u16string(), record.value().GetVersion()));
  44. }
  45. return components;
  46. }
  47. } // namespace android_webview