1 // Copyright 2013 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.
5 #include "chrome/browser/android/chrome_application.h"
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/prefs/pref_service.h"
12 #include "chrome/browser/android/tab_android.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/net/predictor.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/safe_browsing/protocol_manager.h"
18 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
19 #include "chrome/common/chrome_content_client.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/storage_partition.h"
22 #include "content/public/browser/web_contents.h"
23 #include "jni/ChromeApplication_jni.h"
24 #include "net/cookies/cookie_monster.h"
25 #include "net/cookies/cookie_store.h"
26 #include "net/url_request/url_request_context.h"
27 #include "net/url_request/url_request_context_getter.h"
29 using base::android::ConvertUTF8ToJavaString
;
33 void FlushCookiesOnIOThread(
34 scoped_refptr
<net::URLRequestContextGetter
> getter
) {
35 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
36 getter
->GetURLRequestContext()
39 ->FlushStore(base::Closure());
42 void FlushStoragePartition(content::StoragePartition
* partition
) {
46 void CommitPendingWritesForProfile(Profile
* profile
) {
47 // These calls are asynchronous. They may not finish (and may not even
48 // start!) before the Android OS kills our process. But we can't wait for them
49 // to finish because blocking the UI thread is illegal.
50 profile
->GetPrefs()->CommitPendingWrite();
51 content::BrowserThread::PostTask(
52 content::BrowserThread::IO
, FROM_HERE
,
53 base::Bind(&FlushCookiesOnIOThread
,
54 make_scoped_refptr(profile
->GetRequestContext())));
55 profile
->GetNetworkPredictor()->SaveStateForNextStartupAndTrim();
56 content::BrowserContext::ForEachStoragePartition(
57 profile
, base::Bind(FlushStoragePartition
));
60 void RemoveSessionCookiesOnIOThread(
61 scoped_refptr
<net::URLRequestContextGetter
> getter
) {
62 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
63 getter
->GetURLRequestContext()->cookie_store()->DeleteSessionCookiesAsync(
64 net::CookieStore::DeleteCallback());
67 void RemoveSessionCookiesForProfile(Profile
* profile
) {
68 content::BrowserThread::PostTask(
69 content::BrowserThread::IO
, FROM_HERE
,
70 base::Bind(&RemoveSessionCookiesOnIOThread
,
71 make_scoped_refptr(profile
->GetRequestContext())));
74 void ChangeAppStatusOnIOThread(SafeBrowsingService
* sb_service
,
75 jboolean foreground
) {
76 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
77 SafeBrowsingProtocolManager
* proto_manager
= sb_service
->protocol_manager();
79 proto_manager
->SetAppInForeground(foreground
);
84 static ScopedJavaLocalRef
<jstring
> GetBrowserUserAgent(
86 const JavaParamRef
<jclass
>& clazz
) {
87 return ConvertUTF8ToJavaString(env
, GetUserAgent());
90 static void FlushPersistentData(JNIEnv
* env
, const JavaParamRef
<jclass
>& obj
) {
91 // Commit the prending writes for all the loaded profiles.
92 std::vector
<Profile
*> loaded_profiles
=
93 g_browser_process
->profile_manager()->GetLoadedProfiles();
94 std::for_each(loaded_profiles
.begin(), loaded_profiles
.end(),
95 CommitPendingWritesForProfile
);
97 if (g_browser_process
->local_state())
98 g_browser_process
->local_state()->CommitPendingWrite();
101 static void RemoveSessionCookies(JNIEnv
* env
, const JavaParamRef
<jclass
>& obj
) {
102 std::vector
<Profile
*> loaded_profiles
=
103 g_browser_process
->profile_manager()->GetLoadedProfiles();
104 std::for_each(loaded_profiles
.begin(), loaded_profiles
.end(),
105 RemoveSessionCookiesForProfile
);
108 static void ChangeAppStatus(JNIEnv
* env
,
109 const JavaParamRef
<jclass
>& obj
,
110 jboolean foreground
) {
111 content::BrowserThread::PostTask(
112 content::BrowserThread::IO
, FROM_HERE
,
113 base::Bind(&ChangeAppStatusOnIOThread
,
114 base::Unretained(g_browser_process
->safe_browsing_service()),
122 bool ChromeApplication::RegisterBindings(JNIEnv
* env
) {
123 return RegisterNativesImpl(env
);
126 void ChromeApplication::ShowAutofillSettings() {
127 Java_ChromeApplication_showAutofillSettings(
128 base::android::AttachCurrentThread(),
129 base::android::GetApplicationContext());
132 void ChromeApplication::ShowPasswordSettings() {
133 Java_ChromeApplication_showPasswordSettings(
134 base::android::AttachCurrentThread(),
135 base::android::GetApplicationContext());
138 void ChromeApplication::OpenClearBrowsingData(
139 content::WebContents
* web_contents
) {
140 TabAndroid
* tab
= TabAndroid::FromWebContents(web_contents
);
142 Java_ChromeApplication_openClearBrowsingData(
143 base::android::AttachCurrentThread(),
144 base::android::GetApplicationContext(),
145 tab
->GetJavaObject().obj());
148 bool ChromeApplication::AreParentalControlsEnabled() {
149 return Java_ChromeApplication_areParentalControlsEnabled(
150 base::android::AttachCurrentThread(),
151 base::android::GetApplicationContext());
154 } // namespace android
155 } // namespace chrome