1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <vector>
6 
7 #include "base/android/jni_android.h"
8 #include "base/bind.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/profiles/android/jni_headers/ProfileManagerUtils_jni.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "components/prefs/pref_service.h"
14 #include "content/public/browser/browser_context.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/storage_partition.h"
17 #include "services/network/public/mojom/cookie_manager.mojom.h"
18 
19 using base::android::JavaParamRef;
20 using base::android::ScopedJavaLocalRef;
21 
22 namespace {
23 
FlushStoragePartition(content::StoragePartition * partition)24 void FlushStoragePartition(content::StoragePartition* partition) {
25   partition->Flush();
26 }
27 
CommitPendingWritesForProfile(Profile * profile)28 void CommitPendingWritesForProfile(Profile* profile) {
29   // These calls are asynchronous. They may not finish (and may not even
30   // start!) before the Android OS kills our process. But we can't wait for them
31   // to finish because blocking the UI thread is illegal.
32   profile->GetPrefs()->CommitPendingWrite();
33   content::BrowserContext::GetDefaultStoragePartition(profile)
34       ->GetCookieManagerForBrowserProcess()
35       ->FlushCookieStore(
36           network::mojom::CookieManager::FlushCookieStoreCallback());
37   content::BrowserContext::ForEachStoragePartition(
38       profile, base::BindRepeating(FlushStoragePartition));
39 }
40 
RemoveSessionCookiesForProfile(Profile * profile)41 void RemoveSessionCookiesForProfile(Profile* profile) {
42   auto filter = network::mojom::CookieDeletionFilter::New();
43   filter->session_control =
44       network::mojom::CookieDeletionSessionControl::SESSION_COOKIES;
45   content::BrowserContext::GetDefaultStoragePartition(profile)
46       ->GetCookieManagerForBrowserProcess()
47       ->DeleteCookies(std::move(filter),
48                       network::mojom::CookieManager::DeleteCookiesCallback());
49 }
50 
51 }  // namespace
52 
JNI_ProfileManagerUtils_FlushPersistentDataForAllProfiles(JNIEnv * env)53 static void JNI_ProfileManagerUtils_FlushPersistentDataForAllProfiles(
54     JNIEnv* env) {
55   std::vector<Profile*> loaded_profiles =
56       g_browser_process->profile_manager()->GetLoadedProfiles();
57   std::for_each(loaded_profiles.begin(), loaded_profiles.end(),
58                 CommitPendingWritesForProfile);
59 
60   if (g_browser_process->local_state())
61     g_browser_process->local_state()->CommitPendingWrite();
62 }
63 
JNI_ProfileManagerUtils_RemoveSessionCookiesForAllProfiles(JNIEnv * env)64 static void JNI_ProfileManagerUtils_RemoveSessionCookiesForAllProfiles(
65     JNIEnv* env) {
66   std::vector<Profile*> loaded_profiles =
67       g_browser_process->profile_manager()->GetLoadedProfiles();
68   std::for_each(loaded_profiles.begin(), loaded_profiles.end(),
69                 RemoveSessionCookiesForProfile);
70 }
71