File: glic_keyed_service.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 (581 lines) | stat: -rw-r--r-- 21,112 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// Copyright 2024 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/glic/glic_keyed_service.h"

#include <memory>

#include "base/check.h"
#include "base/command_line.h"
#include "base/containers/flat_set.h"
#include "base/feature_list.h"
#include "base/location.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/rand_util.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "chrome/browser/contextual_cueing/contextual_cueing_service.h"
#include "chrome/browser/contextual_cueing/contextual_cueing_service_factory.h"
#include "chrome/browser/glic/fre/glic_fre_controller.h"
#include "chrome/browser/glic/glic_enabling.h"
#include "chrome/browser/glic/glic_enums.h"
#include "chrome/browser/glic/glic_keyed_service_factory.h"
#include "chrome/browser/glic/glic_metrics.h"
#include "chrome/browser/glic/glic_occlusion_notifier.h"
#include "chrome/browser/glic/glic_pref_names.h"
#include "chrome/browser/glic/glic_profile_manager.h"
#include "chrome/browser/glic/host/auth_controller.h"
#include "chrome/browser/glic/host/context/glic_screenshot_capturer.h"
#include "chrome/browser/glic/host/context/glic_sharing_manager_impl.h"
#include "chrome/browser/glic/host/context/glic_tab_data.h"
#include "chrome/browser/glic/host/glic_actor_controller.h"
#include "chrome/browser/glic/host/host.h"
#include "chrome/browser/glic/host/webui_contents_container.h"
#include "chrome/browser/glic/widget/glic_widget.h"
#include "chrome/browser/glic/widget/glic_window_controller_impl.h"
#include "chrome/browser/global_features.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_switches.h"
#include "components/guest_view/browser/guest_view_base.h"
#include "components/optimization_guide/proto/features/actions_data.pb.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/common/url_constants.h"
#include "extensions/browser/guest_view/web_view/web_view_guest.h"
#include "mojo/public/cpp/bindings/callback_helpers.h"
#include "net/log/net_log_with_source.h"
#include "third_party/blink/public/common/web_preferences/web_preferences.h"
#include "ui/base/page_transition_types.h"
#include "ui/views/widget/widget.h"
#include "url/gurl.h"

namespace glic {

namespace {

base::TimeDelta GetWarmingDelay() {
  base::TimeDelta delay_start =
      base::Milliseconds(features::kGlicWarmingDelayMs.Get());
  base::TimeDelta delay_limit =
      delay_start + base::Milliseconds(features::kGlicWarmingJitterMs.Get());
  if (delay_limit > delay_start) {
    return RandTimeDelta(delay_start, delay_limit);
  }
  return delay_start;
}

// TODO(b/421426722): Use net::DefineNetworkTrafficAnnotationTag() to define the
// annotation, and remove this file from tools/traffic_annotation/safe_list.txt.
const net::NetworkTrafficAnnotationTag kGlicWebUITrafficAnnotation =
    MISSING_TRAFFIC_ANNOTATION;

}  // namespace

GlicKeyedService::GlicKeyedService(
    Profile* profile,
    signin::IdentityManager* identity_manager,
    ProfileManager* profile_manager,
    GlicProfileManager* glic_profile_manager,
    contextual_cueing::ContextualCueingService* contextual_cueing_service)
    : profile_(profile),
      enabling_(std::make_unique<GlicEnabling>(
          profile,
          &profile_manager->GetProfileAttributesStorage())),
      metrics_(std::make_unique<GlicMetrics>(profile, enabling_.get())),
      host_(std::make_unique<Host>(profile)),
      window_controller_(
          std::make_unique<GlicWindowControllerImpl>(profile,
                                                     identity_manager,
                                                     this,
                                                     enabling_.get())),
      sharing_manager_(
          std::make_unique<GlicSharingManagerImpl>(profile,
                                                   window_controller_.get(),
                                                   host_.get(),
                                                   metrics_.get())),
      screenshot_capturer_(std::make_unique<GlicScreenshotCapturer>()),
      auth_controller_(std::make_unique<AuthController>(profile,
                                                        identity_manager,
                                                        /*use_for_fre=*/false)),
      occlusion_notifier_(
          std::make_unique<GlicOcclusionNotifier>(*window_controller_)),
      contextual_cueing_service_(contextual_cueing_service) {
  CHECK(GlicEnabling::IsProfileEligible(Profile::FromBrowserContext(profile)));
  host_->Initialize(window_controller_.get());
  metrics_->SetControllers(window_controller_.get(), sharing_manager_.get());

  memory_pressure_listener_ = std::make_unique<base::MemoryPressureListener>(
      FROM_HERE, base::BindRepeating(&GlicKeyedService::OnMemoryPressure,
                                     weak_ptr_factory_.GetWeakPtr()));

  // If `--glic-always-open-fre` is present, unset this pref to ensure the FRE
  // is shown for testing convenience.
  auto* command_line = base::CommandLine::ForCurrentProcess();
  if (command_line->HasSwitch(::switches::kGlicAlwaysOpenFre)) {
    profile_->GetPrefs()->SetInteger(
        prefs::kGlicCompletedFre,
        static_cast<int>(prefs::FreStatus::kNotStarted));
    // or if automation is enabled, skip FRE
  } else if (command_line->HasSwitch(::switches::kGlicAutomation)) {
    profile_->GetPrefs()->SetInteger(
        prefs::kGlicCompletedFre,
        static_cast<int>(prefs::FreStatus::kCompleted));
  }

  if (base::FeatureList::IsEnabled(features::kGlicActor)) {
    actor_controller_ = std::make_unique<GlicActorController>(profile_);
  }

  // This is only used by automation for tests.
  glic_profile_manager->MaybeAutoOpenGlicPanel();
}

GlicKeyedService::~GlicKeyedService() {
  host().Destroy();
  metrics_->SetControllers(nullptr, nullptr);
}

// static
GlicKeyedService* GlicKeyedService::Get(content::BrowserContext* context) {
  return GlicKeyedServiceFactory::GetGlicKeyedService(context);
}

void GlicKeyedService::Shutdown() {
  CloseUI();
  GlicProfileManager* glic_profile_manager = GlicProfileManager::GetInstance();
  if (glic_profile_manager) {
    glic_profile_manager->OnServiceShutdown(this);
  }
}

void GlicKeyedService::ToggleUI(BrowserWindowInterface* bwi,
                                bool prevent_close,
                                mojom::InvocationSource source) {
  // Glic may be disabled for certain user profiles (the user is browsing in
  // incognito or guest mode, policy, etc). In those cases, the entry points to
  // this method should already have been removed.
  CHECK(GlicEnabling::IsEnabledForProfile(profile_));

  GlicProfileManager* glic_profile_manager = GlicProfileManager::GetInstance();
  if (glic_profile_manager) {
    glic_profile_manager->SetActiveGlic(this);
  }
  window_controller_->Toggle(bwi, prevent_close, source);
}

void GlicKeyedService::OpenFreDialogInNewTab(BrowserWindowInterface* bwi,
                                             mojom::InvocationSource source) {
  // Glic may be disabled for certain user profiles (the user is browsing in
  // incognito or guest mode, policy, etc). In those cases, the entry points to
  // this method should already have been removed.
  CHECK(GlicEnabling::IsEnabledForProfile(profile_));

  GlicProfileManager* glic_profile_manager = GlicProfileManager::GetInstance();
  if (glic_profile_manager) {
    glic_profile_manager->SetActiveGlic(this);
  }
  window_controller_->fre_controller()->OpenFreDialogInNewTab(bwi, source);
}

void GlicKeyedService::CloseUI() {
  window_controller_->Shutdown();
  host().Shutdown();
  SetContextAccessIndicator(false);
}

void GlicKeyedService::PrepareForOpen() {
  window_controller_->fre_controller()->MaybePreconnect();

  auto* active_web_contents =
      sharing_manager_->GetFocusedTabData().focus()
          ? sharing_manager_->GetFocusedTabData().focus()->GetContents()
          : nullptr;
  if (contextual_cueing_service_ && active_web_contents) {
    contextual_cueing_service_
        ->PrepareToFetchContextualGlicZeroStateSuggestions(active_web_contents);
  }
}

void GlicKeyedService::OnZeroStateSuggestionsFetched(
    mojom::ZeroStateSuggestionsPtr suggestions,
    mojom::WebClientHandler::GetZeroStateSuggestionsForFocusedTabCallback
        callback,
    std::optional<std::vector<std::string>> returned_suggestions) {
  std::vector<mojom::SuggestionContentPtr> output_suggestions;
  if (returned_suggestions) {
    for (const std::string& suggestion_string : returned_suggestions.value()) {
      output_suggestions.push_back(
          mojom::SuggestionContent::New(suggestion_string));
    }
    suggestions->suggestions = std::move(output_suggestions);
  }

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

void GlicKeyedService::FetchZeroStateSuggestions(
    bool is_first_run,
    std::optional<std::vector<std::string>> supported_tools,
    mojom::WebClientHandler::GetZeroStateSuggestionsForFocusedTabCallback
        callback) {
  auto* active_web_contents =
      sharing_manager_->GetFocusedTabData().focus()
          ? sharing_manager_->GetFocusedTabData().focus()->GetContents()
          : nullptr;

  if (contextual_cueing_service_ && active_web_contents && IsWindowShowing()) {
    auto suggestions = mojom::ZeroStateSuggestions::New();
    suggestions->tab_id = GetTabId(active_web_contents);
    suggestions->tab_url = active_web_contents->GetLastCommittedURL();
    contextual_cueing_service_->GetContextualGlicZeroStateSuggestions(
        active_web_contents, is_first_run, supported_tools,
        mojo::WrapCallbackWithDefaultInvokeIfNotRun(
            base::BindOnce(&GlicKeyedService::OnZeroStateSuggestionsFetched,
                           GetWeakPtr(), std::move(suggestions),
                           std::move(callback)),
            std::nullopt));

  } else {
    std::move(callback).Run(nullptr);
  }
}

GlicWindowController& GlicKeyedService::window_controller() {
  CHECK(window_controller_);
  return *window_controller_.get();
}

GlicSharingManager& GlicKeyedService::sharing_manager() {
  return *sharing_manager_.get();
}

void GlicKeyedService::GuestAdded(content::WebContents* guest_contents) {
  host().GuestAdded(guest_contents);
}

bool GlicKeyedService::IsWindowShowing() const {
  return window_controller_->IsShowing();
}

bool GlicKeyedService::IsWindowDetached() const {
  return window_controller_->IsDetached();
}

bool GlicKeyedService::IsWindowOrFreShowing() const {
  return window_controller_->IsShowing() ||
         window_controller_->fre_controller()->IsShowingDialog();
}

base::CallbackListSubscription
GlicKeyedService::AddContextAccessIndicatorStatusChangedCallback(
    ContextAccessIndicatorChangedCallback callback) {
  return context_access_indicator_callback_list_.Add(std::move(callback));
}

void GlicKeyedService::CreateTab(
    const ::GURL& url,
    bool open_in_background,
    const std::optional<int32_t>& window_id,
    glic::mojom::WebClientHandler::CreateTabCallback callback) {
  // If we need to open other URL types, it should be done in a more specific
  // function.
  if (!url.SchemeIsHTTPOrHTTPS()) {
    std::move(callback).Run(nullptr);
    return;
  }
  NavigateParams params(profile_, url, ui::PAGE_TRANSITION_AUTO_TOPLEVEL);
  params.disposition = open_in_background
                           ? WindowOpenDisposition::NEW_BACKGROUND_TAB
                           : WindowOpenDisposition::NEW_FOREGROUND_TAB;
  base::WeakPtr<content::NavigationHandle> navigation_handle =
      Navigate(&params);
  if (!navigation_handle.get()) {
    std::move(callback).Run(nullptr);
    return;
  }
  // Right after requesting the navigation, the WebContents will have almost no
  // information to populate TabData, hence the overriding of the URL. Should we
  // ever want to send more data back to the web client, we should wait until
  // the navigation commits.
  mojom::TabDataPtr tab_data =
      CreateTabData(navigation_handle.get()->GetWebContents());
  if (tab_data) {
    tab_data->url = url;
  }
  std::move(callback).Run(std::move(tab_data));
}

void GlicKeyedService::ClosePanel() {
  window_controller_->Close();
  SetContextAccessIndicator(false);
  screenshot_capturer_->CloseScreenPicker();
}

void GlicKeyedService::AttachPanel() {
  window_controller_->Attach();
}

void GlicKeyedService::DetachPanel() {
  window_controller_->Detach();
}

void GlicKeyedService::ResizePanel(const gfx::Size& size,
                                   base::TimeDelta duration,
                                   base::OnceClosure callback) {
  window_controller_->Resize(size, duration, std::move(callback));
}

void GlicKeyedService::SetPanelDraggableAreas(
    const std::vector<gfx::Rect>& draggable_areas) {
  window_controller_->SetDraggableAreas(draggable_areas);
}

void GlicKeyedService::SetContextAccessIndicator(bool show) {
  if (is_context_access_indicator_enabled_ == show) {
    return;
  }
  is_context_access_indicator_enabled_ = show;
  context_access_indicator_callback_list_.Notify(show);
}

void GlicKeyedService::ActInFocusedTab(
    const std::vector<uint8_t>& action_proto,
    const mojom::GetTabContextOptions& options,
    mojom::WebClientHandler::ActInFocusedTabCallback callback) {
  CHECK(base::FeatureList::IsEnabled(features::kGlicActor));

  optimization_guide::proto::BrowserAction action;
  if (!action.ParseFromArray(action_proto.data(), action_proto.size())) {
    mojom::ActInFocusedTabResultPtr result =
        mojom::ActInFocusedTabResult::NewErrorReason(
            mojom::ActInFocusedTabErrorReason::kInvalidActionProto);

    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(std::move(callback), std::move(result)));
    return;
  }

  CHECK(actor_controller_);
  actor_controller_->Act(action, options, std::move(callback));
}

void GlicKeyedService::StopActorTask(actor::TaskId task_id) {
  CHECK(base::FeatureList::IsEnabled(features::kGlicActor));
  CHECK(actor_controller_);
  actor_controller_->StopTask(task_id);
}

void GlicKeyedService::PauseActorTask(actor::TaskId task_id) {
  CHECK(base::FeatureList::IsEnabled(features::kGlicActor));
  CHECK(actor_controller_);
  actor_controller_->PauseTask(task_id);
}

void GlicKeyedService::ResumeActorTask(
    actor::TaskId task_id,
    const mojom::GetTabContextOptions& context_options,
    glic::mojom::WebClientHandler::ResumeActorTaskCallback callback) {
  CHECK(base::FeatureList::IsEnabled(features::kGlicActor));
  CHECK(actor_controller_);
  actor_controller_->ResumeTask(task_id, context_options, std::move(callback));
}

void GlicKeyedService::OnUserInputSubmitted(glic::mojom::WebClientMode mode) {
  metrics_->OnUserInputSubmitted(mode);
  if (actor_controller_) {
    actor_controller_->OnUserInputSubmitted();
  }
}

void GlicKeyedService::OnRequestStarted() {
  if (actor_controller_) {
    actor_controller_->OnRequestStarted();
  }
}

void GlicKeyedService::OnResponseStarted() {
  metrics_->OnResponseStarted();
  if (actor_controller_) {
    actor_controller_->OnResponseStarted();
  }
}

void GlicKeyedService::OnResponseStopped() {
  metrics_->OnResponseStarted();
  if (actor_controller_) {
    actor_controller_->OnResponseStopped();
  }
}

bool GlicKeyedService::IsExecutionEngineActingOnTab(
    const content::WebContents* tab) const {
  return actor_controller_ &&
         actor_controller_->IsExecutionEngineActingOnTab(tab);
}

actor::ExecutionEngine& GlicKeyedService::GetExecutionEngineForTesting(
    tabs::TabInterface* tab) {
  CHECK(actor_controller_);
  return actor_controller_->GetExecutionEngineForTesting(tab);  // IN-TEST
}

void GlicKeyedService::CaptureScreenshot(
    mojom::WebClientHandler::CaptureScreenshotCallback callback) {
  screenshot_capturer_->CaptureScreenshot(
      window_controller_->GetGlicWidget()->GetNativeWindow(),
      std::move(callback));
}

bool GlicKeyedService::IsContextAccessIndicatorShown(
    const content::WebContents* contents) {
  return is_context_access_indicator_enabled_ &&
         sharing_manager_->GetFocusedTabData().focus() &&
         sharing_manager_->GetFocusedTabData().focus()->GetContents() ==
             contents;
}

void GlicKeyedService::AddPreloadCallback(base::OnceCallback<void()> callback) {
  preload_callback_ = std::move(callback);
}

void GlicKeyedService::TryPreload() {
  if (base::FeatureList::IsEnabled(features::kGlicDisableWarming) &&
      !base::FeatureList::IsEnabled(features::kGlicWarming)) {
    // This is to ensure the preload process completes and preload_callback_ is
    // called.
    FinishPreload(false);
    return;
  }
  GlicProfileManager* glic_profile_manager = GlicProfileManager::GetInstance();
  CHECK(glic_profile_manager);
  base::TimeDelta delay = GetWarmingDelay();

  // TODO(b/411100559): Ideally we'd use post delayed task in all cases,
  // but this requires a refactor of tests that are currently brittle. For now,
  // just synchronously call ShouldPreloadForProfile if there is no delay.
  if (delay.is_zero()) {
    glic_profile_manager->ShouldPreloadForProfile(
        profile_,
        base::BindOnce(&GlicKeyedService::FinishPreload, GetWeakPtr()));
  } else {
    base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
        FROM_HERE,
        base::BindOnce(&GlicKeyedService::TryPreloadAfterDelay, GetWeakPtr()),
        delay);
  }
}

void GlicKeyedService::TryPreloadAfterDelay() {
  GlicProfileManager* glic_profile_manager = GlicProfileManager::GetInstance();
  if (glic_profile_manager) {
    glic_profile_manager->ShouldPreloadForProfile(
        profile_,
        base::BindOnce(&GlicKeyedService::FinishPreload, GetWeakPtr()));
  }
}

void GlicKeyedService::TryPreloadFre() {
  if (base::FeatureList::IsEnabled(features::kGlicDisableWarming) &&
      !base::FeatureList::IsEnabled(features::kGlicFreWarming)) {
    return;
  }
  GlicProfileManager* glic_profile_manager = GlicProfileManager::GetInstance();
  CHECK(glic_profile_manager);

  glic_profile_manager->ShouldPreloadFreForProfile(
      profile_,
      base::BindOnce(&GlicKeyedService::FinishPreloadFre, GetWeakPtr()));
}

void GlicKeyedService::Reload() {
  window_controller().Reload();
}

void GlicKeyedService::OnMemoryPressure(
    base::MemoryPressureListener::MemoryPressureLevel level) {
  if (level == base::MemoryPressureListener::MemoryPressureLevel::
                   MEMORY_PRESSURE_LEVEL_NONE ||
      (this == GlicProfileManager::GetInstance()->GetLastActiveGlic())) {
    return;
  }

  CloseUI();
}

base::WeakPtr<GlicKeyedService> GlicKeyedService::GetWeakPtr() {
  return weak_ptr_factory_.GetWeakPtr();
}

bool GlicKeyedService::IsActiveWebContents(content::WebContents* contents) {
  if (!contents) {
    return false;
  }
  return contents == host().webui_contents() ||
         contents == window_controller().GetFreWebContents();
}

void GlicKeyedService::FinishPreload(bool should_preload) {
  if (preload_callback_) {
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(std::move(preload_callback_)));
  }
  if (base::FeatureList::IsEnabled(features::kGlicWarming) && profile_ &&
      GlicEnabling::IsEnabledAndConsentForProfile(profile_)) {
    base::UmaHistogramBoolean("Glic.ShouldPreload", should_preload);
  }

  if (!should_preload) {
    return;
  }

  window_controller_->Preload();
}

void GlicKeyedService::FinishPreloadFre(bool should_preload) {
  if (!should_preload) {
    return;
  }

  window_controller_->PreloadFre();
}

bool GlicKeyedService::IsProcessHostForGlic(
    content::RenderProcessHost* process_host) {
  auto* fre_contents = window_controller_->GetFreWebContents();
  if (fre_contents) {
    if (fre_contents->GetPrimaryMainFrame()->GetProcess() == process_host) {
      return true;
    }
  }
  return host().IsGlicWebUiHost(process_host);
}

bool GlicKeyedService::IsGlicWebUi(content::WebContents* web_contents) {
  return host().IsGlicWebUi(web_contents);
}

void GlicKeyedService::LogDummyNetworkRequestForTrafficAnnotation(
    const GURL& url) {
  net::NetLogWithSource net_log =
      net::NetLogWithSource::Make(net::NetLogSourceType::URL_REQUEST);
  net_log.AddEvent(net::NetLogEventType::REQUEST_ALIVE, [&]() {
    base::Value::Dict dict;
    dict.Set("priority", "IDLE");
    dict.Set("url", url.spec());
    dict.Set("traffic_annotation",
             kGlicWebUITrafficAnnotation.unique_id_hash_code);
    dict.Set("dummy_request", true);
    return dict;
  });
}

}  // namespace glic