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/chromium_application.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/android/tab_android.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/net/predictor.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/common/chrome_content_client.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/web_contents.h"
18 #include "jni/ChromiumApplication_jni.h"
19 #include "net/cookies/cookie_monster.h"
20 #include "net/url_request/url_request_context.h"
21 #include "net/url_request/url_request_context_getter.h"
23 using base::android::ConvertUTF8ToJavaString
;
26 void FlushCookiesOnIOThread(
27 scoped_refptr
<net::URLRequestContextGetter
> getter
) {
28 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO
));
29 getter
->GetURLRequestContext()
32 ->FlushStore(base::Closure());
35 void CommitPendingWritesForProfile(Profile
* profile
) {
36 // These calls are asynchronous. They may not finish (and may not even
37 // start!) before the Android OS kills our process. But we can't wait for them
38 // to finish because blocking the UI thread is illegal.
39 profile
->GetPrefs()->CommitPendingWrite();
40 content::BrowserThread::PostTask(
41 content::BrowserThread::IO
, FROM_HERE
,
42 base::Bind(&FlushCookiesOnIOThread
,
43 make_scoped_refptr(profile
->GetRequestContext())));
44 profile
->GetNetworkPredictor()->SaveStateForNextStartupAndTrim();
48 static jstring
GetBrowserUserAgent(JNIEnv
* env
, jclass clazz
) {
49 return ConvertUTF8ToJavaString(env
, GetUserAgent()).Release();
52 static void FlushPersistentData(JNIEnv
* env
, jclass obj
) {
53 // Commit the prending writes for all the loaded profiles.
54 std::vector
<Profile
*> loaded_profiles
=
55 g_browser_process
->profile_manager()->GetLoadedProfiles();
56 std::for_each(loaded_profiles
.begin(), loaded_profiles
.end(),
57 CommitPendingWritesForProfile
);
59 if (g_browser_process
->local_state())
60 g_browser_process
->local_state()->CommitPendingWrite();
67 bool ChromiumApplication::RegisterBindings(JNIEnv
* env
) {
68 return RegisterNativesImpl(env
);
71 void ChromiumApplication::OpenProtectedContentSettings() {
72 Java_ChromiumApplication_openProtectedContentSettings(
73 base::android::AttachCurrentThread(),
74 base::android::GetApplicationContext());
77 void ChromiumApplication::ShowAutofillSettings() {
78 Java_ChromiumApplication_showAutofillSettings(
79 base::android::AttachCurrentThread(),
80 base::android::GetApplicationContext());
83 void ChromiumApplication::ShowPasswordSettings() {
84 Java_ChromiumApplication_showPasswordSettings(
85 base::android::AttachCurrentThread(),
86 base::android::GetApplicationContext());
89 void ChromiumApplication::OpenClearBrowsingData(
90 content::WebContents
* web_contents
) {
91 TabAndroid
* tab
= TabAndroid::FromWebContents(web_contents
);
93 Java_ChromiumApplication_openClearBrowsingData(
94 base::android::AttachCurrentThread(),
95 base::android::GetApplicationContext(),
96 tab
->GetJavaObject().obj());
99 bool ChromiumApplication::AreParentalControlsEnabled() {
100 return Java_ChromiumApplication_areParentalControlsEnabled(
101 base::android::AttachCurrentThread(),
102 base::android::GetApplicationContext());
105 } // namespace android
106 } // namespace chrome