File: chrome_session_state.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (140 lines) | stat: -rw-r--r-- 4,495 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
// Copyright 2020 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/flags/android/chrome_session_state.h"

#include "base/metrics/histogram_macros.h"
#include "base/notreached.h"
#include "chrome/browser/browser_process.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "services/metrics/public/cpp/ukm_source.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "chrome/browser/flags/jni_headers/ChromeSessionState_jni.h"

using chrome::android::ActivityType;
using chrome::android::DarkModeState;

namespace {
ActivityType activity_type = ActivityType::kPreFirstTab;
bool is_in_multi_window_mode = false;
DarkModeState dark_mode_state = DarkModeState::kUnknown;

// Name of local state pref to persist the last |chrome::android::ActivityType|.
const char kLastActivityTypePref[] =
    "user_experience_metrics.last_activity_type";

}  // namespace

namespace chrome {
namespace android {

CustomTabsVisibilityHistogram GetCustomTabsVisibleValue(
    ActivityType activity_type) {
  switch (activity_type) {
    case ActivityType::kTabbed:
    case ActivityType::kWebapp:
    case ActivityType::kWebApk:
      return VISIBLE_CHROME_TAB;
    case ActivityType::kCustomTab:
    case ActivityType::kTrustedWebActivity:
    case ActivityType::kAuthTab:
      return VISIBLE_CUSTOM_TAB;
    case ActivityType::kPreFirstTab:
      return NO_VISIBLE_TAB;
  }
  NOTREACHED();
}

ActivityType GetInitialActivityTypeForTesting() {
  return activity_type;
}

void SetInitialActivityTypeForTesting(ActivityType type) {
  activity_type = type;
}

void SetActivityType(PrefService* local_state, ActivityType type) {
  DCHECK(local_state);
  DCHECK_NE(type, ActivityType::kPreFirstTab);

  ActivityType prev_activity_type = activity_type;
  activity_type = type;

  // EmitActivityTypeHistograms on first SetActivityType call.
  if (prev_activity_type == ActivityType::kPreFirstTab) {
    EmitActivityTypeHistograms(activity_type);
    SaveActivityTypeToLocalState(local_state, activity_type);
  }

  ukm::UkmSource::SetAndroidActivityTypeState(static_cast<int>(activity_type));
}

ActivityType GetActivityType() {
  return activity_type;
}

DarkModeState GetDarkModeState() {
  return dark_mode_state;
}

bool GetIsInMultiWindowModeValue() {
  return is_in_multi_window_mode;
}

void EmitActivityTypeHistograms(ActivityType type) {
  UMA_STABILITY_HISTOGRAM_ENUMERATION("CustomTabs.Visible",
                                      GetCustomTabsVisibleValue(type));
  UMA_STABILITY_HISTOGRAM_ENUMERATION("Android.ChromeActivity.Type", type);
}

void RegisterActivityTypePrefs(PrefRegistrySimple* registry) {
  DCHECK(registry);
  // Register with a default value of -1 which is not a valid enum value.
  registry->RegisterIntegerPref(kLastActivityTypePref, -1);
}

std::optional<chrome::android::ActivityType> GetActivityTypeFromLocalState(
    PrefService* local_state) {
  auto value = local_state->GetInteger(kLastActivityTypePref);
  if (value >= static_cast<int>(ActivityType::kTabbed) &&
      value <= static_cast<int>(ActivityType::kMaxValue)) {
    return static_cast<ActivityType>(value);
  }
  return std::nullopt;
}

void SaveActivityTypeToLocalState(PrefService* local_state,
                                  chrome::android::ActivityType value) {
  local_state->SetInteger(kLastActivityTypePref, static_cast<int>(value));
}

MultipleUserProfilesState GetMultipleUserProfilesState() {
  static MultipleUserProfilesState multiple_user_profiles_state =
      static_cast<MultipleUserProfilesState>(
          Java_ChromeSessionState_getMultipleUserProfilesState(
              jni_zero::AttachCurrentThread()));
  return multiple_user_profiles_state;
}

}  // namespace android
}  // namespace chrome

static void JNI_ChromeSessionState_SetActivityType(JNIEnv* env, jint type) {
  DCHECK(g_browser_process);
  DCHECK(g_browser_process->local_state());
  chrome::android::SetActivityType(g_browser_process->local_state(),
                                   static_cast<ActivityType>(type));
}

static void JNI_ChromeSessionState_SetDarkModeState(JNIEnv* env, jint state) {
  dark_mode_state = static_cast<DarkModeState>(state);
}

static void JNI_ChromeSessionState_SetIsInMultiWindowMode(
    JNIEnv* env,
    jboolean j_is_in_multi_window_mode) {
  is_in_multi_window_mode = j_is_in_multi_window_mode;
}