Upstream the last native calls in the Application.
[chromium-blink-merge.git] / chrome / browser / android / chromium_application.cc
blob7b5a5a6b896b3ae50cf6b43df1c9021f2ae453b5
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/browser/safe_browsing/protocol_manager.h"
16 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
17 #include "chrome/common/chrome_content_client.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/web_contents.h"
20 #include "jni/ChromiumApplication_jni.h"
21 #include "net/cookies/cookie_monster.h"
22 #include "net/cookies/cookie_store.h"
23 #include "net/url_request/url_request_context.h"
24 #include "net/url_request/url_request_context_getter.h"
26 using base::android::ConvertUTF8ToJavaString;
28 namespace {
30 void FlushCookiesOnIOThread(
31 scoped_refptr<net::URLRequestContextGetter> getter) {
32 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
33 getter->GetURLRequestContext()
34 ->cookie_store()
35 ->GetCookieMonster()
36 ->FlushStore(base::Closure());
39 void CommitPendingWritesForProfile(Profile* profile) {
40 // These calls are asynchronous. They may not finish (and may not even
41 // start!) before the Android OS kills our process. But we can't wait for them
42 // to finish because blocking the UI thread is illegal.
43 profile->GetPrefs()->CommitPendingWrite();
44 content::BrowserThread::PostTask(
45 content::BrowserThread::IO, FROM_HERE,
46 base::Bind(&FlushCookiesOnIOThread,
47 make_scoped_refptr(profile->GetRequestContext())));
48 profile->GetNetworkPredictor()->SaveStateForNextStartupAndTrim();
51 void RemoveSessionCookiesOnIOThread(
52 scoped_refptr<net::URLRequestContextGetter> getter) {
53 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
54 getter->GetURLRequestContext()->cookie_store()->DeleteSessionCookiesAsync(
55 net::CookieStore::DeleteCallback());
58 void RemoveSessionCookiesForProfile(Profile* profile) {
59 content::BrowserThread::PostTask(
60 content::BrowserThread::IO, FROM_HERE,
61 base::Bind(&RemoveSessionCookiesOnIOThread,
62 make_scoped_refptr(profile->GetRequestContext())));
65 void ChangeAppStatusOnIOThread(SafeBrowsingService* sb_service,
66 jboolean foreground) {
67 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
68 SafeBrowsingProtocolManager* proto_manager = sb_service->protocol_manager();
69 if (proto_manager)
70 proto_manager->SetAppInForeground(foreground);
73 } // namespace
75 static jstring GetBrowserUserAgent(JNIEnv* env, jclass clazz) {
76 return ConvertUTF8ToJavaString(env, GetUserAgent()).Release();
79 static void FlushPersistentData(JNIEnv* env, jclass obj) {
80 // Commit the prending writes for all the loaded profiles.
81 std::vector<Profile*> loaded_profiles =
82 g_browser_process->profile_manager()->GetLoadedProfiles();
83 std::for_each(loaded_profiles.begin(), loaded_profiles.end(),
84 CommitPendingWritesForProfile);
86 if (g_browser_process->local_state())
87 g_browser_process->local_state()->CommitPendingWrite();
90 static void RemoveSessionCookies(JNIEnv* env, jclass obj) {
91 std::vector<Profile*> loaded_profiles =
92 g_browser_process->profile_manager()->GetLoadedProfiles();
93 std::for_each(loaded_profiles.begin(), loaded_profiles.end(),
94 RemoveSessionCookiesForProfile);
97 static void ChangeAppStatus(JNIEnv* env, jclass obj, jboolean foreground) {
98 content::BrowserThread::PostTask(
99 content::BrowserThread::IO, FROM_HERE,
100 base::Bind(&ChangeAppStatusOnIOThread,
101 base::Unretained(g_browser_process->safe_browsing_service()),
102 foreground));
105 namespace chrome {
106 namespace android {
108 // static
109 bool ChromiumApplication::RegisterBindings(JNIEnv* env) {
110 return RegisterNativesImpl(env);
113 void ChromiumApplication::OpenProtectedContentSettings() {
114 Java_ChromiumApplication_openProtectedContentSettings(
115 base::android::AttachCurrentThread(),
116 base::android::GetApplicationContext());
119 void ChromiumApplication::ShowAutofillSettings() {
120 Java_ChromiumApplication_showAutofillSettings(
121 base::android::AttachCurrentThread(),
122 base::android::GetApplicationContext());
125 void ChromiumApplication::ShowPasswordSettings() {
126 Java_ChromiumApplication_showPasswordSettings(
127 base::android::AttachCurrentThread(),
128 base::android::GetApplicationContext());
131 void ChromiumApplication::OpenClearBrowsingData(
132 content::WebContents* web_contents) {
133 TabAndroid* tab = TabAndroid::FromWebContents(web_contents);
134 DCHECK(tab);
135 Java_ChromiumApplication_openClearBrowsingData(
136 base::android::AttachCurrentThread(),
137 base::android::GetApplicationContext(),
138 tab->GetJavaObject().obj());
141 bool ChromiumApplication::AreParentalControlsEnabled() {
142 return Java_ChromiumApplication_areParentalControlsEnabled(
143 base::android::AttachCurrentThread(),
144 base::android::GetApplicationContext());
147 } // namespace android
148 } // namespace chrome