Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(426)

Side by Side Diff: chrome/browser/android/cookies/cookies_fetcher.cc

Issue 2282153002: android: Remove CleanupReference from CookiesFetcher (Closed)
Patch Set: fix double delete Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/android/cookies/cookies_fetcher.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/command_line.h" 8 #include "base/command_line.h"
9 #include "base/time/time.h" 9 #include "base/time/time.h"
10 #include "chrome/browser/android/cookies/cookies_fetcher.h" 10 #include "chrome/browser/android/cookies/cookies_fetcher.h"
11 #include "chrome/browser/profiles/profile_manager.h" 11 #include "chrome/browser/profiles/profile_manager.h"
12 #include "content/public/browser/browser_thread.h" 12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/common/content_switches.h" 13 #include "content/public/common/content_switches.h"
14 #include "jni/CookiesFetcher_jni.h" 14 #include "jni/CookiesFetcher_jni.h"
15 #include "net/cookies/cookie_store.h" 15 #include "net/cookies/cookie_store.h"
16 #include "net/url_request/url_request_context.h" 16 #include "net/url_request/url_request_context.h"
17 17
18 using base::android::JavaParamRef; 18 using base::android::JavaParamRef;
19 using base::android::ScopedJavaLocalRef; 19 using base::android::ScopedJavaLocalRef;
20 20
21 CookiesFetcher::CookiesFetcher(JNIEnv* env, jobject obj, Profile* profile) { 21 CookiesFetcher::CookiesFetcher(JNIEnv* env, jobject obj, Profile* profile) {
22 } 22 }
23 23
24 CookiesFetcher::~CookiesFetcher() { 24 CookiesFetcher::~CookiesFetcher() {
25 } 25 }
26 26
27 void CookiesFetcher::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) {
28 delete this;
29 }
30
31 void CookiesFetcher::PersistCookies(JNIEnv* env, 27 void CookiesFetcher::PersistCookies(JNIEnv* env,
32 const JavaParamRef<jobject>& obj) { 28 const JavaParamRef<jobject>& obj) {
33 Profile* profile = ProfileManager::GetPrimaryUserProfile(); 29 Profile* profile = ProfileManager::GetPrimaryUserProfile();
34 if (!profile->HasOffTheRecordProfile()) { 30 if (!profile->HasOffTheRecordProfile()) {
35 // There is no work to be done. We might consider calling 31 // There is no work to be done. We might consider calling
36 // the Java callback if needed. 32 // the Java callback if needed.
37 return; 33 return;
38 } 34 }
39 profile = profile->GetOffTheRecordProfile(); 35 profile = profile->GetOffTheRecordProfile();
40 36
(...skipping 14 matching lines...) Expand all
55 51
56 void CookiesFetcher::PersistCookiesInternal( 52 void CookiesFetcher::PersistCookiesInternal(
57 net::URLRequestContextGetter* getter) { 53 net::URLRequestContextGetter* getter) {
58 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 54 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
59 55
60 net::CookieStore* store = getter->GetURLRequestContext()->cookie_store(); 56 net::CookieStore* store = getter->GetURLRequestContext()->cookie_store();
61 57
62 // Nullable sometimes according to docs. There is no work need to be done 58 // Nullable sometimes according to docs. There is no work need to be done
63 // but we can consider calling the Java callback with empty output. 59 // but we can consider calling the Java callback with empty output.
64 if (!store) { 60 if (!store) {
65 jobject_.Reset(); 61 delete this;
66 return; 62 return;
67 } 63 }
68 64
69 store->GetAllCookiesAsync(base::Bind( 65 store->GetAllCookiesAsync(
70 &CookiesFetcher::OnCookiesFetchFinished, base::Unretained(this))); 66 base::Bind(&CookiesFetcher::OnCookiesFetchFinished, base::Owned(this)));
gone 2016/08/29 18:07:53 What destroys the object on this path? It seems t
boliu 2016/08/29 18:53:45 It's owned and so deleted by the callback. Look up
gone 2016/08/29 18:55:01 Sorry, I don't do many native code reviews :P
71 } 67 }
72 68
73 void CookiesFetcher::OnCookiesFetchFinished(const net::CookieList& cookies) { 69 void CookiesFetcher::OnCookiesFetchFinished(const net::CookieList& cookies) {
74 JNIEnv* env = base::android::AttachCurrentThread(); 70 JNIEnv* env = base::android::AttachCurrentThread();
75 71
76 ScopedJavaLocalRef<jobjectArray> joa = 72 ScopedJavaLocalRef<jobjectArray> joa =
77 Java_CookiesFetcher_createCookiesArray(env, jobject_, cookies.size()); 73 Java_CookiesFetcher_createCookiesArray(env, jobject_, cookies.size());
78 74
79 int index = 0; 75 int index = 0;
80 for (net::CookieList::const_iterator i = cookies.begin(); 76 for (net::CookieList::const_iterator i = cookies.begin();
81 i != cookies.end(); ++i) { 77 i != cookies.end(); ++i) {
82 std::string domain = i->Domain(); 78 std::string domain = i->Domain();
83 if (domain.length() > 1 && domain[0] == '.') 79 if (domain.length() > 1 && domain[0] == '.')
84 domain = domain.substr(1); 80 domain = domain.substr(1);
85 ScopedJavaLocalRef<jobject> java_cookie = Java_CookiesFetcher_createCookie( 81 ScopedJavaLocalRef<jobject> java_cookie = Java_CookiesFetcher_createCookie(
86 env, jobject_, base::android::ConvertUTF8ToJavaString(env, i->Name()), 82 env, jobject_, base::android::ConvertUTF8ToJavaString(env, i->Name()),
87 base::android::ConvertUTF8ToJavaString(env, i->Value()), 83 base::android::ConvertUTF8ToJavaString(env, i->Value()),
88 base::android::ConvertUTF8ToJavaString(env, i->Domain()), 84 base::android::ConvertUTF8ToJavaString(env, i->Domain()),
89 base::android::ConvertUTF8ToJavaString(env, i->Path()), 85 base::android::ConvertUTF8ToJavaString(env, i->Path()),
90 i->CreationDate().ToInternalValue(), i->ExpiryDate().ToInternalValue(), 86 i->CreationDate().ToInternalValue(), i->ExpiryDate().ToInternalValue(),
91 i->LastAccessDate().ToInternalValue(), i->IsSecure(), i->IsHttpOnly(), 87 i->LastAccessDate().ToInternalValue(), i->IsSecure(), i->IsHttpOnly(),
92 static_cast<int>(i->SameSite()), i->Priority()); 88 static_cast<int>(i->SameSite()), 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_, joa); 92 Java_CookiesFetcher_onCookieFetchFinished(env, jobject_, joa);
97
98 // Give up the reference.
99 jobject_.Reset();
100 } 93 }
101 94
102 static void RestoreToCookieJarInternal(net::URLRequestContextGetter* getter, 95 static void RestoreToCookieJarInternal(net::URLRequestContextGetter* getter,
103 const net::CanonicalCookie& cookie) { 96 const net::CanonicalCookie& cookie) {
104 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 97 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
105 98
106 net::CookieStore* store = getter->GetURLRequestContext()->cookie_store(); 99 net::CookieStore* store = getter->GetURLRequestContext()->cookie_store();
107 100
108 // Nullable sometimes according to docs. 101 // Nullable sometimes according to docs.
109 if (!store) { 102 if (!store) {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 172
180 // JNI functions 173 // JNI functions
181 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { 174 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) {
182 return reinterpret_cast<intptr_t>(new CookiesFetcher(env, obj, 0)); 175 return reinterpret_cast<intptr_t>(new CookiesFetcher(env, obj, 0));
183 } 176 }
184 177
185 // Register native methods 178 // Register native methods
186 bool RegisterCookiesFetcher(JNIEnv* env) { 179 bool RegisterCookiesFetcher(JNIEnv* env) {
187 return RegisterNativesImpl(env); 180 return RegisterNativesImpl(env);
188 } 181 }
OLDNEW
« no previous file with comments | « chrome/browser/android/cookies/cookies_fetcher.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698