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(JNIEnv
* env
,
86 return ConvertUTF8ToJavaString(env
, GetUserAgent());
89 static void FlushPersistentData(JNIEnv
* env
, jclass obj
) {
90 // Commit the prending writes for all the loaded profiles.
91 std::vector
<Profile
*> loaded_profiles
=
92 g_browser_process
->profile_manager()->GetLoadedProfiles();
93 std::for_each(loaded_profiles
.begin(), loaded_profiles
.end(),
94 CommitPendingWritesForProfile
);
96 if (g_browser_process
->local_state())
97 g_browser_process
->local_state()->CommitPendingWrite();
100 static void RemoveSessionCookies(JNIEnv
* env
, jclass obj
) {
101 std::vector
<Profile
*> loaded_profiles
=
102 g_browser_process
->profile_manager()->GetLoadedProfiles();
103 std::for_each(loaded_profiles
.begin(), loaded_profiles
.end(),
104 RemoveSessionCookiesForProfile
);
107 static void ChangeAppStatus(JNIEnv
* env
, jclass obj
, jboolean foreground
) {
108 content::BrowserThread::PostTask(
109 content::BrowserThread::IO
, FROM_HERE
,
110 base::Bind(&ChangeAppStatusOnIOThread
,
111 base::Unretained(g_browser_process
->safe_browsing_service()),
119 bool ChromeApplication::RegisterBindings(JNIEnv
* env
) {
120 return RegisterNativesImpl(env
);
123 void ChromeApplication::ShowAutofillSettings() {
124 Java_ChromeApplication_showAutofillSettings(
125 base::android::AttachCurrentThread(),
126 base::android::GetApplicationContext());
129 void ChromeApplication::ShowPasswordSettings() {
130 Java_ChromeApplication_showPasswordSettings(
131 base::android::AttachCurrentThread(),
132 base::android::GetApplicationContext());
135 void ChromeApplication::OpenClearBrowsingData(
136 content::WebContents
* web_contents
) {
137 TabAndroid
* tab
= TabAndroid::FromWebContents(web_contents
);
139 Java_ChromeApplication_openClearBrowsingData(
140 base::android::AttachCurrentThread(),
141 base::android::GetApplicationContext(),
142 tab
->GetJavaObject().obj());
145 bool ChromeApplication::AreParentalControlsEnabled() {
146 return Java_ChromeApplication_areParentalControlsEnabled(
147 base::android::AttachCurrentThread(),
148 base::android::GetApplicationContext());
151 } // namespace android
152 } // namespace chrome