File: README.md

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 (76 lines) | stat: -rw-r--r-- 3,804 bytes parent folder | download | duplicates (6)
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
# Storing preferences

This guide is intended for developers of Chrome for Android who need to read
and/or write small amounts of data from Java to a persistent key-value store.

## SharedPreferencesManager

[`SharedPreferencesManager`][0] is a lightweight wrapper around Android
[SharedPreferences][1] to handle additional key management logic in Chrome. It
supports reading and writing simple key-value pairs to a file that is saved
across app sessions.

## PrefService

[`PrefService`][2] is a JNI bridge providing access to a native Chrome
[PrefService][3] instance associated. This interface can be used to read and
write prefs once they're registered through the `PrefRegistry` and exposed to
Java by way of a `java_cpp_strings` build target such as [this one][4].

## FAQ

**Should I use SharedPreferences or PrefService?**

Ask yourself the following questions about the preference to be stored:

*   Will the preference need to be accessed from native C++ code?
*   Should the preference be configured as syncable, so that its state can be
    managed by Chrome Sync at Backup and Restore?
*   Does the preference need a managed policy setting?

If the answer to one or more of the above questions is Yes, then the preference
should be stored in PrefService. If the answer to all of the above questions is
No, then SharedPreferences should be preferred.

**What if the PrefService type I need to access is not supported by PrefService
(i.e. double, Time, etc.)?**

If a base value type is supported by PrefService, then PrefService should be
extended to support it once it's needed.

**How do I access a PrefService pref associated with local state rather than
browser profile?**

Most Chrome for Android preferences should be associated with a specific
profile. If your preference should instead be associated with [local state][5]
(for example, if it is related to the First Run Experience), then you should not
use PrefService and should instead create your own feature-specific JNI bridge
to access the correct PrefService instance (see [`first_run_utils.cc`][6]).

**Can I move a pref from SharedPreferences to PrefService?**

In general, SharedPreferences are not exposed to C++. There is limited support
in [`shared_preferences_migrator_android.h`][7] for reading, writing, and
clearing values from SharedPreferences.

**Should I use the default SharedPreferences or create my own?**

You should prefer to use the default one through `SharedPreferencesManager`
unless you are going to store large amounts of data.

Rationale:

*   SharedPreferences are stored in binary xml files in the app's data directory
    and cached in memory the first time they are accessed.
*   The default one is prefetched on early start-up, so you can always rely on
    it being in memory (no need to read it on a background thread).


[0]: https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/SharedPreferencesManager.java
[1]: https://developer.android.com/reference/android/content/SharedPreferences
[2]: https://source.chromium.org/chromium/chromium/src/+/main:components/prefs/android/java/src/org/chromium/components/prefs/PrefService.java
[3]: https://chromium.googlesource.com/chromium/src/+/main/services/preferences/README.md
[4]: https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/preferences/BUILD.gn;drc=4ae1b7be67cd9b470ebcc90f2747a9f31f155b00;l=28
[5]: https://www.chromium.org/developers/design-documents/preferences#TOC-Introduction
[6]: https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/first_run/android/first_run_utils.cc
[7]: https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/android/preferences/shared_preferences_migrator_android.h