File: cookies_fetcher_util.cc

package info (click to toggle)
chromium 135.0.7049.95-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,959,392 kB
  • sloc: cpp: 34,198,526; ansic: 7,100,035; javascript: 3,985,800; python: 1,395,489; asm: 896,754; xml: 722,891; pascal: 180,504; sh: 94,909; perl: 88,388; objc: 79,739; sql: 53,020; cs: 41,358; fortran: 24,137; makefile: 22,501; php: 13,699; tcl: 10,142; yacc: 8,822; ruby: 7,350; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; awk: 197; sed: 36
file content (110 lines) | stat: -rw-r--r-- 4,959 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
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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/time/time.h"
#include "chrome/browser/android/cookies/cookies_fetcher_restore_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_constants.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/common/content_switches.h"
#include "net/cookies/cookie_partition_key.h"
#include "net/cookies/cookie_util.h"
#include "services/network/public/mojom/cookie_manager.mojom.h"

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

using base::android::JavaParamRef;
using base::android::ScopedJavaGlobalRef;
using base::android::ScopedJavaLocalRef;

namespace {

// Passes the fetched |cookies| to the application so that can be saved in a
// file.
void OnCookiesFetchFinished(
    const ScopedJavaGlobalRef<jobject>& j_cookie_fetcher,
    const net::CookieList& cookies) {
  JNIEnv* env = base::android::AttachCurrentThread();

  ScopedJavaLocalRef<jobjectArray> joa =
      Java_CookiesFetcher_createCookiesArray(env, cookies.size());

  int index = 0;
  for (const auto& cookie : cookies) {
    // TODO (crbug.com/326605834) Once ancestor chain bit changes are
    // implemented update this method utilize the ancestor bit.
    base::expected<net::CookiePartitionKey::SerializedCookiePartitionKey,
                   std::string>
        key_serialized_result =
            net::CookiePartitionKey::Serialize(cookie.PartitionKey());
    if (!key_serialized_result.has_value()) {
      continue;
    }
    ScopedJavaLocalRef<jobject> java_cookie = Java_CookiesFetcher_createCookie(
        env, cookie.Name(), cookie.Value(), cookie.Domain(), cookie.Path(),
        cookie.CreationDate().ToDeltaSinceWindowsEpoch().InMicroseconds(),
        cookie.ExpiryDate().ToDeltaSinceWindowsEpoch().InMicroseconds(),
        cookie.LastAccessDate().ToDeltaSinceWindowsEpoch().InMicroseconds(),
        cookie.LastUpdateDate().ToDeltaSinceWindowsEpoch().InMicroseconds(),
        cookie.SecureAttribute(), cookie.IsHttpOnly(),
        static_cast<int>(cookie.SameSite()), cookie.Priority(),
        key_serialized_result->TopLevelSite(),
        static_cast<int>(cookie.SourceScheme()), cookie.SourcePort(),
        static_cast<int>(cookie.SourceType()));
    env->SetObjectArrayElement(joa.obj(), index++, java_cookie.obj());
  }

  Java_CookiesFetcher_onCookieFetchFinished(env, j_cookie_fetcher, joa);
}

}  // namespace

std::string JNI_CookiesFetcher_GetCookieFileDirectory(JNIEnv* env,
                                                      Profile* profile) {
  return profile->GetPath().Append(chrome::kOTRTempStateDirname).value();
}

// Fetches cookies for the off-the-record session (i.e. incognito mode). It is a
// no-op for the standard session. Typically associated with the #onPause of
// Android's activity lifecycle.
void JNI_CookiesFetcher_PersistCookies(
    JNIEnv* env,
    Profile* profile,
    const JavaParamRef<jobject>& j_cookies_fetcher) {
  cookie_fetcher_restore_util::GetCookieServiceClient(profile)->GetAllCookies(
      base::BindOnce(&OnCookiesFetchFinished,
                     ScopedJavaGlobalRef<jobject>(j_cookies_fetcher)));
}

void JNI_CookiesFetcher_RestoreCookies(JNIEnv* env,
                                       Profile* profile,
                                       std::string& name,
                                       std::string& value,
                                       std::string& domain,
                                       std::string& path,
                                       jlong creation,
                                       jlong expiration,
                                       jlong last_access,
                                       jlong last_update,
                                       jboolean secure,
                                       jboolean httponly,
                                       jint same_site,
                                       jint priority,
                                       std::string& partition_key,
                                       jint source_scheme,
                                       jint source_port,
                                       jint source_type) {
  cookie_fetcher_restore_util::CookiesFetcherRestoreCookiesImpl(
      env, profile, name, value, domain, path, creation, expiration,
      last_access, last_update, secure, httponly, same_site, priority,
      partition_key, source_scheme, source_port, source_type);
}