File: optimization_guide_internals_ui.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (137 lines) | stat: -rw-r--r-- 6,151 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
// Copyright 2022 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/optimization_guide/optimization_guide_internals_ui.h"

#include <cstdint>
#include <memory>
#include <vector>

#include "base/hash/hash.h"
#include "base/i18n/time_formatting.h"
#include "base/time/time.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/grit/optimization_guide_internals_resources.h"
#include "components/grit/optimization_guide_internals_resources_map.h"
#include "components/optimization_guide/core/delivery/prediction_manager.h"
#include "components/optimization_guide/core/model_execution/feature_keys.h"
#include "components/optimization_guide/core/model_quality/model_quality_util.h"
#include "components/optimization_guide/core/optimization_guide_prefs.h"
#include "components/optimization_guide/optimization_guide_internals/webui/optimization_guide_internals.mojom.h"
#include "components/optimization_guide/optimization_guide_internals/webui/optimization_guide_internals_page_handler_impl.h"
#include "components/optimization_guide/proto/model_execution.pb.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui_data_source.h"
#include "third_party/icu/source/i18n/unicode/timezone.h"
#include "ui/webui/webui_util.h"

bool OptimizationGuideInternalsUIConfig::IsWebUIEnabled(
    content::BrowserContext* browser_context) {
  Profile* profile = Profile::FromBrowserContext(browser_context);
  auto* service = OptimizationGuideKeyedServiceFactory::GetForProfile(profile);
  return service != nullptr;
}

OptimizationGuideInternalsUI::OptimizationGuideInternalsUI(
    content::WebUI* web_ui)
    : MojoWebUIController(web_ui, /*enable_chrome_send=*/true) {
  content::WebUIDataSource* source = content::WebUIDataSource::CreateAndAdd(
      web_ui->GetWebContents()->GetBrowserContext(),
      optimization_guide_internals::kChromeUIOptimizationGuideInternalsHost);
  webui::SetupWebUIDataSource(
      source, kOptimizationGuideInternalsResources,
      IDR_OPTIMIZATION_GUIDE_INTERNALS_OPTIMIZATION_GUIDE_INTERNALS_HTML);
}

OptimizationGuideInternalsUI::~OptimizationGuideInternalsUI() = default;

void OptimizationGuideInternalsUI::BindInterface(
    mojo::PendingReceiver<
        optimization_guide_internals::mojom::PageHandlerFactory> receiver) {
  // TODO(crbug.com/40215132): Remove the reset which is needed now since
  // |this| is reused on internals page reloads.
  optimization_guide_internals_page_factory_receiver_.reset();
  optimization_guide_internals_page_factory_receiver_.Bind(std::move(receiver));
}

void OptimizationGuideInternalsUI::CreatePageHandler(
    mojo::PendingRemote<optimization_guide_internals::mojom::Page> page) {
  Profile* profile = Profile::FromWebUI(web_ui());
  auto* service = OptimizationGuideKeyedServiceFactory::GetForProfile(profile);
  DCHECK(service);
  OptimizationGuideLogger* optimization_guide_logger =
      service->GetOptimizationGuideLogger();
  optimization_guide_internals_page_handler_ =
      std::make_unique<OptimizationGuideInternalsPageHandlerImpl>(
          std::move(page), optimization_guide_logger);
}

void OptimizationGuideInternalsUI::RequestDownloadedModelsInfo(
    RequestDownloadedModelsInfoCallback callback) {
  Profile* profile = Profile::FromWebUI(web_ui());
  auto* service = OptimizationGuideKeyedServiceFactory::GetForProfile(profile);
  optimization_guide::PredictionManager* prediction_manager =
      service->GetPredictionManager();
  std::vector<optimization_guide_internals::mojom::DownloadedModelInfoPtr>
      downloaded_models_info =
          prediction_manager->GetDownloadedModelsInfoForWebUI();
  std::move(callback).Run(std::move(downloaded_models_info));
}

void OptimizationGuideInternalsUI::RequestLoggedModelQualityClientIds(
    RequestLoggedModelQualityClientIdsCallback callback) {
  PrefService* local_state = g_browser_process->local_state();

  int64_t client_id =
      local_state->GetInt64(optimization_guide::model_execution::prefs::
                                localstate::kModelQualityLoggingClientId);

  // If the client id is zero no client id is set, in that case do nothing.
  if (client_id == 0) {
    std::move(callback).Run({});
    return;
  }

  // Get the client ids for the compose and tab organization feature for the
  // past 28 days to show on chrome://optimization-guide-internals.
  // TODO(b/308642692): Add other features client id as requested.
  std::vector<optimization_guide_internals::mojom::LoggedClientIdsPtr>
      logged_client_ids;
  // Initialize time outside to have it change when generating the client ids
  // for different days.
  base::Time now = base::Time::Now();
  // Loop through past 28 days to generate the client ids for compose and
  // tab_organization features.
  for (int i = 0; i < 28; ++i) {
    base::Time day_i = now - base::Days(i);

    // Hash the client id with the date so that it changes everyday for every
    // feature.
    int64_t client_id_i_compose =
        optimization_guide::GetHashedModelQualityClientId(
            optimization_guide::proto::LogAiDataRequest::FeatureCase::kCompose,
            day_i, client_id);

    int64_t client_id_i_tab_organization =
        optimization_guide::GetHashedModelQualityClientId(
            optimization_guide::proto::LogAiDataRequest::FeatureCase::
                kTabOrganization,
            day_i, client_id);

    logged_client_ids.push_back(
        optimization_guide_internals::mojom::LoggedClientIds::New(
            client_id_i_compose));
    logged_client_ids.push_back(
        optimization_guide_internals::mojom::LoggedClientIds::New(
            client_id_i_tab_organization));
  }

  std::move(callback).Run(std::move(logged_client_ids));
}

WEB_UI_CONTROLLER_TYPE_IMPL(OptimizationGuideInternalsUI)