File: campaigns_manager_session.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 (690 lines) | stat: -rw-r--r-- 21,858 bytes parent folder | download | duplicates (5)
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
// 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/ash/growth/campaigns_manager_session.h"

#include <optional>
#include <string>
#include <string_view>

#include "ash/constants/ash_features.h"
#include "ash/constants/ash_switches.h"
#include "ash/login/ui/lock_screen.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "base/check.h"
#include "base/check_is_test.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chrome/browser/apps/app_service/app_service_proxy_ash.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/ash/ownership/owner_settings_service_ash.h"
#include "chrome/browser/ash/ownership/owner_settings_service_ash_factory.h"
#include "chrome/browser/ash/system_web_apps/system_web_app_manager.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/web_applications/app_browser_controller.h"
#include "chromeos/ash/components/growth/action_performer.h"
#include "chromeos/ash/components/growth/campaigns_constants.h"
#include "chromeos/ash/components/growth/campaigns_logger.h"
#include "chromeos/ash/components/growth/campaigns_manager.h"
#include "chromeos/ash/components/growth/campaigns_model.h"
#include "chromeos/ash/components/growth/campaigns_utils.h"
#include "chromeos/ash/components/growth/growth_metrics.h"
#include "components/account_id/account_id.h"
#include "components/app_constants/constants.h"
#include "components/services/app_service/public/cpp/app_registry_cache_wrapper.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/session_manager/core/session_manager.h"
#include "components/session_manager/session_manager_types.h"
#include "content/public/browser/web_contents.h"

namespace {

CampaignsManagerSession* g_instance = nullptr;

Profile* g_profile_for_testing = nullptr;

// The time to trigger delayed campaigns.
constexpr base::TimeDelta kTimeToTriggerDelayedCampaigns = base::Minutes(5);

Profile* GetProfile() {
  if (g_profile_for_testing) {
    return g_profile_for_testing;
  }

  return ProfileManager::GetActiveUserProfile();
}

bool IsEligible() {
  Profile* profile = GetProfile();

  if (!profile) {
    // Records metrics when profile is nullptr.
    // TODO: b/367998596 - Change this to CHECK(profile).
    // In the test ExtensionCrxInstallerTest.KioskOnlyTest, the call sequences
    // are this:
    // 1. CampaignsManagerSession::OnSessionStateChanged().
    // 2. The IsEligible() returns true, the code continues.
    // 3. Add a callback when the device owner is set: OnOwnershipDetermined().
    // 4. In OnOwnershipDetermined(), load the campaigns.
    // 5. When the campaigns are loaded, call MaybeTriggerRuntimeCampaigns().
    // 6. Which calls IsEligible() again, and hits the CHECK(profile).
    // The profile becames nullptr during steps 2-6.
    growth::RecordCampaignsManagerError(
        growth::CampaignsManagerError::kNullptrProfile);
    return false;
  }

  // TODO(b/320789239): Enable for unicorn users.
  if (profile->GetProfilePolicyConnector()->IsManaged()) {
    // Only enabled for consumer session for now.
    // Demo Mode session is handled separately at `DemoSession`.
    return false;
  }

  // TODO: b/341328441 - Enable Growth Framework on guest mode.
  if (profile->IsGuestSession()) {
    return false;
  }

  return true;
}

bool IsWebBrowserAppId(std::string_view app_id) {
  return app_id == app_constants::kChromeAppId;
}

bool IsAppVisible(const apps::InstanceUpdate& update) {
  return update.State() & apps::InstanceState::kVisible;
}

bool IsAppActiveAndVisible(const apps::InstanceUpdate& update) {
  return IsAppVisible(update) &&
         (update.State() & apps::InstanceState::kActive);
}

std::optional<growth::ActionType> GetActionTypeBySlot(growth::Slot slot) {
  if (slot == growth::Slot::kNotification) {
    return growth::ActionType::kShowNotification;
  }

  if (slot == growth::Slot::kNudge) {
    return growth::ActionType::kShowNudge;
  }

  if (slot == growth::Slot::kDryRun) {
    return growth::ActionType::kDryRun;
  }

  return std::nullopt;
}

std::string_view GetAppGroupId() {
  auto* campaigns_manager = growth::CampaignsManager::Get();
  CHECK(campaigns_manager);

  const auto app_id = campaigns_manager->GetOpenedAppId();
  return IsWebBrowserAppId(app_id)
             ? growth::GetAppGroupId(campaigns_manager->GetActiveUrl())
             : growth::GetAppGroupId(app_id);
}

base::TimeDelta GetTimeToTriggerDelayedCampaigns() {
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  if (command_line->HasSwitch(
          ash::switches::kGrowthCampaignsDelayedTriggerTimeInSecs)) {
    const auto& value = command_line->GetSwitchValueASCII(
        ash::switches::kGrowthCampaignsDelayedTriggerTimeInSecs);

    double seconds;
    CHECK(base::StringToDouble(value, &seconds));
    return base::Seconds(seconds);
  }

  return kTimeToTriggerDelayedCampaigns;
}

void MaybeTriggerSlot(growth::Slot slot) {
  const auto action_type = GetActionTypeBySlot(slot);
  if (!action_type) {
    CAMPAIGNS_LOG(ERROR) << "Invalid: no supported action type for slot "
                         << static_cast<int>(action_type.value());
    return;
  }

  auto* campaigns_manager = growth::CampaignsManager::Get();
  CHECK(campaigns_manager);

  auto* campaign = campaigns_manager->GetCampaignBySlot(slot);
  if (!campaign) {
    // No campaign matched.
    return;
  }

  auto campaign_id = growth::GetCampaignId(campaign);
  if (!campaign_id) {
    CAMPAIGNS_LOG(ERROR) << "Invalid: Missing campaign id.";
    return;
  }

  if (action_type == growth::ActionType::kDryRun) {
    // Skip rendering for dry run.
    return;
  }

  const auto* payload = growth::GetPayloadBySlot(campaign, slot);
  if (!payload) {
    // No payload for the targeted slot. It is valid for counterfactual control.
    return;
  }

  campaigns_manager->PerformAction(campaign_id.value(),
                                   growth::GetCampaignGroupId(campaign),
                                   action_type.value(), payload);
}

void MaybeTriggerRuntimeCampaigns(growth::TriggerType type,
                                  std::string_view event = std::string_view()) {
  // We need this for trigger points that are not managed by
  // `CampaignsManagerSession`, e.g: `MaybeTriggerCampaignsOnEvent()`.
  if (!IsEligible()) {
    return;
  }

  auto* campaigns_manager = growth::CampaignsManager::Get();
  CHECK(campaigns_manager);

  growth::Trigger trigger(type);
  trigger.events = {std::string(event)};
  campaigns_manager->SetTrigger(std::move(trigger));

  MaybeTriggerSlot(growth::Slot::kNudge);
  MaybeTriggerSlot(growth::Slot::kNotification);
  MaybeTriggerSlot(growth::Slot::kDryRun);
}

void MaybeTriggerCampaignsWhenCampaignsLoaded() {
  if (!ash::features::IsGrowthCampaignsTriggerAtLoadComplete()) {
    return;
  }

  MaybeTriggerRuntimeCampaigns(growth::TriggerType::kCampaignsLoaded);
}

void MaybeTriggerDelayedCampaigns() {
  MaybeTriggerRuntimeCampaigns(growth::TriggerType::kDelayedOneShotTimer);
}

// The app_id is optional and only required if the browser type is app.
content::WebContents* FindActiveWebContent(
    const Profile* profile,
    Browser::Type browser_type,
    const webapps::AppId& app_id = std::string()) {
  for (Browser* browser : BrowserList::GetInstance()->OrderedByActivation()) {
    if (browser->IsAttemptingToCloseBrowser() || browser->IsBrowserClosing()) {
      continue;
    }
    if (browser->type() != browser_type) {
      continue;
    }
    if (browser->profile() != profile) {
      continue;
    }
    // For web app type, it must match the app_id.
    if (browser_type == Browser::TYPE_APP &&
        !web_app::AppBrowserController::IsForWebApp(browser, app_id)) {
      continue;
    }

    const auto* tab_strip_model = browser->tab_strip_model();
    if (!tab_strip_model) {
      CAMPAIGNS_LOG(ERROR) << "No tab_strip_model.";
      continue;
    }

    auto* active_web_contents = tab_strip_model->GetActiveWebContents();
    if (!active_web_contents) {
      CAMPAIGNS_LOG(ERROR) << "No active web contents.";
      continue;
    }

    return active_web_contents;
  }
  return nullptr;
}

const GURL FindActiveWebAppUrl(Profile* profile, const webapps::AppId& app_id) {
  auto* active_web_contents =
      FindActiveWebContent(profile, Browser::TYPE_APP, app_id);
  if (!active_web_contents) {
    return GURL::EmptyGURL();
  }
  return active_web_contents->GetURL();
}

content::WebContents* FindActiveTabWebContent(Profile* profile) {
  return FindActiveWebContent(profile, Browser::TYPE_NORMAL);
}

std::optional<apps::AppType> GetAppType(const std::string& app_id) {
  auto account_id =
      ash::Shell::Get()->session_controller()->GetActiveAccountId();
  apps::AppRegistryCache* cache =
      apps::AppRegistryCacheWrapper::Get().GetAppRegistryCache(account_id);
  if (!cache) {
    return std::nullopt;
  }
  return cache->GetAppType(app_id);
}

bool IsSystemWebApp(Profile* profile, const webapps::AppId& app_id) {
  ash::SystemWebAppManager* swa_manager =
      ash::SystemWebAppManager::Get(profile);
  if (!swa_manager) {
    CHECK_IS_TEST();
    return false;
  }
  return swa_manager->IsSystemWebApp(app_id);
}

bool HasValidPwaBrowserForAppId(const std::string& app_id) {
  for (Browser* browser : BrowserList::GetInstance()->OrderedByActivation()) {
    if (browser->profile()->IsOffTheRecord() || !browser->IsActive()) {
      continue;
    }

    if (browser->type() != Browser::TYPE_APP) {
      CAMPAIGNS_LOG(ERROR) << "Not pwa browser type";
      return false;
    }

    if (!web_app::AppBrowserController::IsForWebApp(browser, app_id)) {
      CAMPAIGNS_LOG(ERROR) << "Browser belongs to a different app";
      return false;
    }

    return true;
  }

  CAMPAIGNS_LOG(ERROR) << "No browser window";
  return false;
}

void SetCampaignManagerPrefService(Profile* profile) {
  auto* campaigns_manager = growth::CampaignsManager::Get();
  CHECK(campaigns_manager);

  if (!profile) {
    campaigns_manager->SetPrefs(nullptr);
    return;
  }
  campaigns_manager->SetPrefs(profile->GetPrefs());
}

}  // namespace

// static
CampaignsManagerSession* CampaignsManagerSession::Get() {
  return g_instance;
}

CampaignsManagerSession::CampaignsManagerSession() {
  CHECK_EQ(g_instance, nullptr);
  g_instance = this;

  // SessionManager may be unset in unit tests.
  auto* session_manager = session_manager::SessionManager::Get();
  if (session_manager) {
    session_manager_observation_.Observe(session_manager);
    OnSessionStateChanged();
  }

  // Shell is not available in unit tests.
  auto* power_manager_client = chromeos::PowerManagerClient::Get();
  if (power_manager_client && ash::Shell::HasInstance()) {
    shell_observer_.Observe(ash::Shell::Get());
    power_manager_client_observer_.Observe(power_manager_client);
  }
}

CampaignsManagerSession::~CampaignsManagerSession() {
  CHECK_EQ(g_instance, this);
  g_instance = nullptr;
  g_profile_for_testing = nullptr;
  SetCampaignManagerPrefService(nullptr);
}

void CampaignsManagerSession::OnSessionStateChanged() {
  // Stop the timer to avoid triggering campaigns if the session is not active.
  if (delayed_timer_.IsRunning()) {
    delayed_timer_.Stop();
  }

  if (session_manager::SessionManager::Get()->session_state() ==
      session_manager::SessionState::LOCKED) {
    if (scoped_observation_.IsObserving()) {
      scoped_observation_.Reset();
    }
    return;
  }

  if (session_manager::SessionManager::Get()->session_state() !=
      session_manager::SessionState::ACTIVE) {
    // Loads campaigns at session active only.
    return;
  }

  if (!IsEligible()) {
    return;
  }

  RecordSessionUnlockEvent();
  SetCampaignManagerPrefService(GetProfile());

  ash::OwnerSettingsServiceAsh* service =
      ash::OwnerSettingsServiceAshFactory::GetForBrowserContext(GetProfile());
  if (service) {
    service->IsOwnerAsync(
        base::BindOnce(&CampaignsManagerSession::OnOwnershipDetermined,
                       weak_ptr_factory_.GetWeakPtr()));
  } else {
    // TODO: b/338085893 - Add metric to track the case that settings service
    // is not available at this point.
    CAMPAIGNS_LOG(ERROR)
        << "Owner settings service unavailable for the profile.";
  }
}

void CampaignsManagerSession::OnShellDestroying() {
  // Observe shell destroying as indicator of power manager destroying event if
  // this happens before campaign manager session is destructed.
  power_manager_client_observer_.Reset();
  shell_observer_.Reset();
}

void CampaignsManagerSession::SuspendDone(base::TimeDelta sleep_duration) {
  // Do not record event when the session is not active, such as lock screen.
  if (session_manager::SessionManager::Get()->session_state() !=
      session_manager::SessionState::ACTIVE) {
    return;
  }

  RecordSessionUnlockEvent();
}

void CampaignsManagerSession::OnInstanceUpdate(
    const apps::InstanceUpdate& update) {
  // No state changes. Ignore the update.
  if (!update.StateChanged()) {
    return;
  }

  if (update.IsDestruction()) {
    HandleAppInstanceDestruction(update);
    return;
  }

  auto app_id = update.AppId();
  auto app_type = GetAppType(app_id);
  if (!app_type) {
    CAMPAIGNS_LOG(ERROR) << "Invalid app type for " << app_id;
    return;
  }

  switch (app_type.value()) {
    case apps::AppType::kUnknown:
      break;
    case apps::AppType::kChromeApp:
      HandleWebBrowserInstanceUpdate(update);
      break;
    case apps::AppType::kWeb:
      if (IsSystemWebApp(GetProfile(), app_id)) {
        // Active browser is not available for the SWA case, so we handle
        // it as a regular app open.
        HandleAppInstanceUpdate(update);
        break;
      }
      HandlePwaInstanceUpdate(update);
      break;
    case apps::AppType::kArc:
      HandleArcInstanceUpdate(update);
      break;
    default:
      HandleAppInstanceUpdate(update);
      break;
  }
}

void CampaignsManagerSession::OnInstanceRegistryWillBeDestroyed(
    apps::InstanceRegistry* cache) {
  if (scoped_observation_.GetSource() == cache) {
    scoped_observation_.Reset();
  }
}

void CampaignsManagerSession::MaybeTriggerCampaignsOnEvent(
    std::string_view event) {
  if (!ash::features::IsGrowthCampaignsTriggerByEventEnabled()) {
    return;
  }

  MaybeTriggerRuntimeCampaigns(growth::TriggerType::kEvent, event);
}

void CampaignsManagerSession::PrimaryPageChanged(
    const content::WebContents* web_contents) {
  auto* campaigns_manager = growth::CampaignsManager::Get();
  CHECK(campaigns_manager);

  auto app_id = campaigns_manager->GetOpenedAppId();
  if (!IsWebBrowserAppId(app_id)) {
    return;
  }

  // Skip triggering campaign if this Primary Page Changed event happens on an
  // inactive tab (i.e. `web_contents` is not the active tab `web_contents`).
  // For example:
  // 1. Load `url1` in "tab 1".
  // 2. While `url1` is loading, open a "tab 2" and load the same URL `url1`
  // 3. The nudge triggered twice - one by the inactive "tab 1" and one by the
  // active "tab 2".
  auto* active_tab_web_contents = FindActiveTabWebContent(GetProfile());
  if (active_tab_web_contents != web_contents) {
    return;
  }

  auto url = active_tab_web_contents->GetURL();
  campaigns_manager->SetActiveUrl(url);
  MaybeTriggerCampaignsWhenAppOpened();
}

void CampaignsManagerSession::SetProfileForTesting(Profile* profile) {
  g_profile_for_testing = profile;
}

void CampaignsManagerSession::SetupWindowObserver() {
  // Tests might not go through LOCKED state and `scoped_observation_` might
  // be observing.
  if (scoped_observation_.IsObserving()) {
    return;
  }

  auto* profile = GetProfile();
  // Some test profiles will not have AppServiceProxy.
  if (!apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(profile)) {
    return;
  }
  auto* proxy = apps::AppServiceProxyFactory::GetForProfile(profile);
  CHECK(proxy);
  scoped_observation_.Observe(&proxy->InstanceRegistry());
}

void CampaignsManagerSession::OnOwnershipDetermined(bool is_user_owner) {
  auto* campaigns_manager = growth::CampaignsManager::Get();
  CHECK(campaigns_manager);

  campaigns_manager->SetIsUserOwner(is_user_owner);

  campaigns_manager->LoadCampaigns(
      base::BindOnce(&CampaignsManagerSession::OnLoadCampaignsCompleted,
                     weak_ptr_factory_.GetWeakPtr()));
}

void CampaignsManagerSession::OnLoadCampaignsCompleted() {
  if (ash::features::IsGrowthCampaignsTriggerByAppOpenEnabled()) {
    SetupWindowObserver();
  }

  MaybeTriggerCampaignsWhenCampaignsLoaded();
  StartDelayedTimer();
}

void CampaignsManagerSession::StartDelayedTimer() {
  delayed_timer_.Start(FROM_HERE, GetTimeToTriggerDelayedCampaigns(),
                       base::BindOnce(&MaybeTriggerDelayedCampaigns));
}

void CampaignsManagerSession::CacheAppOpenContext(
    const apps::InstanceUpdate& update,
    const GURL& url) {
  auto* campaigns_manager = growth::CampaignsManager::Get();
  CHECK(campaigns_manager);

  auto app_id = update.AppId();
  campaigns_manager->SetOpenedApp(app_id);
  campaigns_manager->SetActiveUrl(url);
  opened_window_ = update.Window();
}

void CampaignsManagerSession::ClearAppOpenContext() {
  auto* campaigns_manager = growth::CampaignsManager::Get();
  CHECK(campaigns_manager);

  campaigns_manager->SetOpenedApp(std::string());
  campaigns_manager->SetActiveUrl(GURL::EmptyGURL());
  opened_window_ = nullptr;
}

void CampaignsManagerSession::HandleAppInstanceUpdate(
    const apps::InstanceUpdate& update) {
  if (!update.IsCreation()) {
    return;
  }
  CacheAppOpenContext(update, GURL::EmptyGURL());
  MaybeTriggerCampaignsWhenAppOpened();
}

void CampaignsManagerSession::HandleArcInstanceUpdate(
    const apps::InstanceUpdate& update) {
  // When an Arc app is opened, the instance state is `Started & Running &
  // Visible`. When an Arc app is closed, there are a sequence of
  // `Destroy`-`Started & Running`-`Destroy` state update as reported in
  // b/342489300. We skip the `Started & Running` state received after
  // closing the app.
  if (!update.IsCreation() || !IsAppVisible(update)) {
    return;
  }
  CacheAppOpenContext(update, GURL::EmptyGURL());
  MaybeTriggerCampaignsWhenAppOpened();
}

void CampaignsManagerSession::HandleWebBrowserInstanceUpdate(
    const apps::InstanceUpdate& update) {
  auto app_id = update.AppId();

  // Non web browser app such as text editor will be handled like default
  // app type.
  if (!IsWebBrowserAppId(app_id)) {
    CAMPAIGNS_LOG(ERROR) << "Not a web broswer: " << app_id;
    HandleAppInstanceUpdate(update);
    return;
  }

  if (!ash::features::IsGrowthCampaignsTriggerByBrowserEnabled()) {
    return;
  }

  // For browser app, the user can open a new tab or switch to an existing
  // tab before navigating to an url. So, it is not limited to a creation event
  // like other app types. In any case, the browser should be active and visible
  // when the user starts inserting the url.
  if (!IsAppActiveAndVisible(update)) {
    return;
  }

  // Caches the current app id and browser window and clears the url since it
  // isn't relevant to url navigation action. Caching url and triggering the
  // campaigns is deferred to PrimaryPageChanged when url navigation actually
  // happens.
  CacheAppOpenContext(update, GURL::EmptyGURL());
}

void CampaignsManagerSession::HandlePwaInstanceUpdate(
    const apps::InstanceUpdate& update) {
  auto* campaigns_manager = growth::CampaignsManager::Get();
  CHECK(campaigns_manager);

  if (!update.IsCreation()) {
    return;
  }

  // When a user navigates to the url from web browser, an instance update with
  // PWA id is also sent as reported in b/342489221. This check will skip
  // instance update with chrome browser window and PWA app id, which is a
  // mismatch.
  auto app_id = update.AppId();
  if (!HasValidPwaBrowserForAppId(app_id)) {
    CAMPAIGNS_LOG(ERROR) << "Invalid web app browser";
    return;
  }

  CacheAppOpenContext(update, FindActiveWebAppUrl(GetProfile(), app_id));
  MaybeTriggerCampaignsWhenAppOpened();
}

void CampaignsManagerSession::HandleAppInstanceDestruction(
    const apps::InstanceUpdate& update) {
  // TODO: b/330409492 - Maybe trigger a campaign when app is about to be
  // destroyed.
  auto* campaigns_manager = growth::CampaignsManager::Get();
  CHECK(campaigns_manager);

  if (update.AppId() != campaigns_manager->GetOpenedAppId()) {
    return;
  }
  ClearAppOpenContext();
}

void CampaignsManagerSession::MaybeTriggerCampaignsWhenAppOpened() {
  auto* campaigns_manager = growth::CampaignsManager::Get();
  CHECK(campaigns_manager);

  // If `app_group_id` is defined, record the `event` and trigger campaigns
  // based on the trigger `event`. An `app_group_id` is used to configurate how
  // often, i.e. the interval, to show the nudges.
  if (const std::string_view app_group_id = GetAppGroupId();
      !app_group_id.empty()) {
    campaigns_manager->RecordEvent(
        GetEventName(growth::CampaignEvent::kEvent, app_group_id));
    MaybeTriggerCampaignsOnEvent(app_group_id);
  }

  if (!ash::features::IsGrowthCampaignsTriggerByAppOpenEnabled()) {
    return;
  }

  MaybeTriggerRuntimeCampaigns(growth::TriggerType::kAppOpened);
}

void CampaignsManagerSession::RecordSessionUnlockEvent() {
  auto* campaigns_manager = growth::CampaignsManager::Get();
  CHECK(campaigns_manager);

  campaigns_manager->RecordEvent(growth::kGrowthCampaignsEventSessionUnlock);
}