OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/android/jni_android.h" | 5 #include "base/android/jni_android.h" |
6 #include "base/android/jni_string.h" | 6 #include "base/android/jni_string.h" |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
9 #include "chrome/browser/android/cookies/cookies_fetcher.h" | 9 #include "chrome/browser/android/cookies/cookies_fetcher.h" |
10 #include "chrome/browser/profiles/profile_manager.h" | 10 #include "chrome/browser/profiles/profile_manager.h" |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 void CookiesFetcher::OnCookiesFetchFinished(const net::CookieList& cookies) { | 69 void CookiesFetcher::OnCookiesFetchFinished(const net::CookieList& cookies) { |
70 JNIEnv* env = base::android::AttachCurrentThread(); | 70 JNIEnv* env = base::android::AttachCurrentThread(); |
71 | 71 |
72 ScopedJavaLocalRef<jobjectArray> joa = | 72 ScopedJavaLocalRef<jobjectArray> joa = |
73 Java_CookiesFetcher_createCookiesArray( | 73 Java_CookiesFetcher_createCookiesArray( |
74 env, jobject_.obj(), cookies.size()); | 74 env, jobject_.obj(), cookies.size()); |
75 | 75 |
76 int index = 0; | 76 int index = 0; |
77 for (net::CookieList::const_iterator i = cookies.begin(); | 77 for (net::CookieList::const_iterator i = cookies.begin(); |
78 i != cookies.end(); ++i) { | 78 i != cookies.end(); ++i) { |
79 ScopedJavaLocalRef<jobject> java_cookie = | 79 ScopedJavaLocalRef<jobject> java_cookie = Java_CookiesFetcher_createCookie( |
80 Java_CookiesFetcher_createCookie( | 80 env, jobject_.obj(), |
81 env, jobject_.obj(), | 81 base::android::ConvertUTF8ToJavaString(env, i->Source()).obj(), |
82 base::android::ConvertUTF8ToJavaString(env, i->Source()).obj(), | 82 base::android::ConvertUTF8ToJavaString(env, i->Name()).obj(), |
83 base::android::ConvertUTF8ToJavaString(env, i->Name()).obj(), | 83 base::android::ConvertUTF8ToJavaString(env, i->Value()).obj(), |
84 base::android::ConvertUTF8ToJavaString(env, i->Value()).obj(), | 84 base::android::ConvertUTF8ToJavaString(env, i->Domain()).obj(), |
85 base::android::ConvertUTF8ToJavaString(env, i->Domain()).obj(), | 85 base::android::ConvertUTF8ToJavaString(env, i->Path()).obj(), |
86 base::android::ConvertUTF8ToJavaString(env, i->Path()).obj(), | 86 i->CreationDate().ToInternalValue(), i->ExpiryDate().ToInternalValue(), |
87 i->CreationDate().ToInternalValue(), | 87 i->LastAccessDate().ToInternalValue(), i->IsSecure(), i->IsHttpOnly(), |
88 i->ExpiryDate().ToInternalValue(), | 88 i->IsFirstPartyOnly(), i->Priority()); |
89 i->LastAccessDate().ToInternalValue(), | |
90 i->IsSecure(), | |
91 i->IsHttpOnly(), | |
92 i->Priority()); | |
93 env->SetObjectArrayElement(joa.obj(), index++, java_cookie.obj()); | 89 env->SetObjectArrayElement(joa.obj(), index++, java_cookie.obj()); |
94 } | 90 } |
95 | 91 |
96 Java_CookiesFetcher_onCookieFetchFinished(env, jobject_.obj(), joa.obj()); | 92 Java_CookiesFetcher_onCookieFetchFinished(env, jobject_.obj(), joa.obj()); |
97 | 93 |
98 // Give up the reference. | 94 // Give up the reference. |
99 jobject_.Reset(); | 95 jobject_.Reset(); |
100 } | 96 } |
101 | 97 |
102 void CookiesFetcher::RestoreCookies(JNIEnv* env, | 98 void CookiesFetcher::RestoreCookies(JNIEnv* env, |
103 jobject obj, | 99 jobject obj, |
104 jstring url, | 100 jstring url, |
105 jstring name, | 101 jstring name, |
106 jstring value, | 102 jstring value, |
107 jstring domain, | 103 jstring domain, |
108 jstring path, | 104 jstring path, |
109 int64 creation, | 105 int64 creation, |
110 int64 expiration, | 106 int64 expiration, |
111 int64 last_access, | 107 int64 last_access, |
112 bool secure, | 108 bool secure, |
113 bool httponly, | 109 bool httponly, |
| 110 bool firstpartyonly, |
114 int priority) { | 111 int priority) { |
115 Profile* profile = ProfileManager::GetPrimaryUserProfile(); | 112 Profile* profile = ProfileManager::GetPrimaryUserProfile(); |
116 if (!profile->HasOffTheRecordProfile()) { | 113 if (!profile->HasOffTheRecordProfile()) { |
117 return; // Don't create it. There is nothing to do. | 114 return; // Don't create it. There is nothing to do. |
118 } | 115 } |
119 profile = profile->GetOffTheRecordProfile(); | 116 profile = profile->GetOffTheRecordProfile(); |
120 | 117 |
121 scoped_refptr<net::URLRequestContextGetter> getter( | 118 scoped_refptr<net::URLRequestContextGetter> getter( |
122 profile->GetRequestContext()); | 119 profile->GetRequestContext()); |
123 | 120 |
124 net::CanonicalCookie cookie( | 121 net::CanonicalCookie cookie( |
125 GURL(base::android::ConvertJavaStringToUTF8(env, url)), | 122 GURL(base::android::ConvertJavaStringToUTF8(env, url)), |
126 base::android::ConvertJavaStringToUTF8(env, name), | 123 base::android::ConvertJavaStringToUTF8(env, name), |
127 base::android::ConvertJavaStringToUTF8(env, value), | 124 base::android::ConvertJavaStringToUTF8(env, value), |
128 base::android::ConvertJavaStringToUTF8(env, domain), | 125 base::android::ConvertJavaStringToUTF8(env, domain), |
129 base::android::ConvertJavaStringToUTF8(env, path), | 126 base::android::ConvertJavaStringToUTF8(env, path), |
130 base::Time::FromInternalValue(creation), | 127 base::Time::FromInternalValue(creation), |
131 base::Time::FromInternalValue(expiration), | 128 base::Time::FromInternalValue(expiration), |
132 base::Time::FromInternalValue(last_access), | 129 base::Time::FromInternalValue(last_access), secure, httponly, |
133 secure, httponly, static_cast<net::CookiePriority>(priority)); | 130 firstpartyonly, static_cast<net::CookiePriority>(priority)); |
134 | 131 |
135 // The rest must be done from the IO thread. | 132 // The rest must be done from the IO thread. |
136 content::BrowserThread::PostTask( | 133 content::BrowserThread::PostTask( |
137 content::BrowserThread::IO, | 134 content::BrowserThread::IO, |
138 FROM_HERE, | 135 FROM_HERE, |
139 base::Bind(&CookiesFetcher::RestoreToCookieJarInternal, | 136 base::Bind(&CookiesFetcher::RestoreToCookieJarInternal, |
140 base::Unretained(this), | 137 base::Unretained(this), |
141 getter, | 138 getter, |
142 cookie)); | 139 cookie)); |
143 } | 140 } |
144 | 141 |
145 void CookiesFetcher::RestoreToCookieJarInternal( | 142 void CookiesFetcher::RestoreToCookieJarInternal( |
146 net::URLRequestContextGetter* getter, | 143 net::URLRequestContextGetter* getter, |
147 const net::CanonicalCookie& cookie) { | 144 const net::CanonicalCookie& cookie) { |
148 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 145 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
149 | 146 |
150 net::CookieStore* store = getter->GetURLRequestContext()->cookie_store(); | 147 net::CookieStore* store = getter->GetURLRequestContext()->cookie_store(); |
151 | 148 |
152 // Nullable sometimes according to docs. | 149 // Nullable sometimes according to docs. |
153 if (!store) { | 150 if (!store) { |
154 return; | 151 return; |
155 } | 152 } |
156 | 153 |
157 net::CookieMonster* monster = store->GetCookieMonster(); | 154 net::CookieMonster* monster = store->GetCookieMonster(); |
158 base::Callback<void(bool success)> cb; | 155 base::Callback<void(bool success)> cb; |
159 | 156 |
160 monster->SetCookieWithDetailsAsync( | 157 monster->SetCookieWithDetailsAsync( |
161 GURL(cookie.Source()), | 158 GURL(cookie.Source()), cookie.Name(), cookie.Value(), cookie.Domain(), |
162 cookie.Name(), | 159 cookie.Path(), cookie.ExpiryDate(), cookie.IsSecure(), |
163 cookie.Value(), | 160 cookie.IsHttpOnly(), cookie.IsFirstPartyOnly(), cookie.Priority(), cb); |
164 cookie.Domain(), | |
165 cookie.Path(), | |
166 cookie.ExpiryDate(), | |
167 cookie.IsSecure(), | |
168 cookie.IsHttpOnly(), | |
169 cookie.Priority(), | |
170 cb | |
171 ); | |
172 } | 161 } |
173 | 162 |
174 // JNI functions | 163 // JNI functions |
175 static jlong Init(JNIEnv* env, jobject obj) { | 164 static jlong Init(JNIEnv* env, jobject obj) { |
176 return reinterpret_cast<intptr_t>(new CookiesFetcher(env, obj, 0)); | 165 return reinterpret_cast<intptr_t>(new CookiesFetcher(env, obj, 0)); |
177 } | 166 } |
178 | 167 |
179 // Register native methods | 168 // Register native methods |
180 bool RegisterCookiesFetcher(JNIEnv* env) { | 169 bool RegisterCookiesFetcher(JNIEnv* env) { |
181 return RegisterNativesImpl(env); | 170 return RegisterNativesImpl(env); |
182 } | 171 } |
OLD | NEW |