@@ -43,14 +43,21 @@ public class CookieManagerModule extends ReactContextBaseJavaModule {
43
43
private static final String CLEAR_BY_NAME_NOT_SUPPORTED = "Cannot remove a single cookie by name on Android" ;
44
44
private static final String INVALID_DOMAINS = "Cookie URL host %s and domain %s mismatched. The cookie won't set correctly." ;
45
45
46
- private CookieManager mCookieManager ;
47
46
private CookieSyncManager mCookieSyncManager ;
48
47
49
48
CookieManagerModule (ReactApplicationContext context ) {
50
49
super (context );
51
50
this .mCookieSyncManager = CookieSyncManager .createInstance (context );
52
- this .mCookieManager = CookieManager .getInstance ();
53
- mCookieManager .setAcceptCookie (true );
51
+ }
52
+
53
+ private CookieManager getCookieManager () throws Exception {
54
+ try {
55
+ CookieManager cookieManager = CookieManager .getInstance ();
56
+ cookieManager .setAcceptCookie (true );
57
+ return cookieManager ;
58
+ } catch (Exception e ) {
59
+ throw new Exception (e );
60
+ }
54
61
}
55
62
56
63
public String getName () {
@@ -88,7 +95,7 @@ public void setFromResponse(String url, String cookie, final Promise promise) {
88
95
@ ReactMethod
89
96
public void flush (Promise promise ) {
90
97
try {
91
- mCookieManager .flush ();
98
+ getCookieManager () .flush ();
92
99
promise .resolve (true );
93
100
} catch (Exception e ) {
94
101
promise .reject (e );
@@ -112,7 +119,7 @@ public void get(String url, Boolean useWebKit, Promise promise) {
112
119
return ;
113
120
}
114
121
try {
115
- String cookiesString = mCookieManager .getCookie (url );
122
+ String cookiesString = getCookieManager () .getCookie (url );
116
123
117
124
WritableMap cookieMap = createCookieList (cookiesString );
118
125
promise .resolve (cookieMap );
@@ -128,36 +135,42 @@ public void clearByName(String url, String name, Boolean useWebKit, final Promis
128
135
129
136
@ ReactMethod
130
137
public void clearAll (Boolean useWebKit , final Promise promise ) {
131
- if (USES_LEGACY_STORE ) {
132
- mCookieManager .removeAllCookie ();
133
- mCookieManager .removeSessionCookie ();
134
- mCookieSyncManager .sync ();
135
- promise .resolve (true );
136
- } else {
137
- mCookieManager .removeAllCookies (new ValueCallback <Boolean >() {
138
- @ Override
139
- public void onReceiveValue (Boolean value ) {
140
- promise .resolve (value );
141
- }
142
- });
143
- mCookieManager .flush ();
138
+ try {
139
+ CookieManager cookieManager = getCookieManager ();
140
+ if (USES_LEGACY_STORE ) {
141
+ cookieManager .removeAllCookie ();
142
+ cookieManager .removeSessionCookie ();
143
+ mCookieSyncManager .sync ();
144
+ promise .resolve (true );
145
+ } else {
146
+ cookieManager .removeAllCookies (new ValueCallback <Boolean >() {
147
+ @ Override
148
+ public void onReceiveValue (Boolean value ) {
149
+ promise .resolve (value );
150
+ }
151
+ });
152
+ cookieManager .flush ();
153
+ }
154
+ } catch (Exception e ) {
155
+ promise .reject (e );
144
156
}
145
157
}
146
158
147
159
private void addCookies (String url , String cookieString , final Promise promise ) {
148
160
try {
161
+ CookieManager cookieManager = getCookieManager ();
149
162
if (USES_LEGACY_STORE ) {
150
- mCookieManager .setCookie (url , cookieString );
163
+ cookieManager .setCookie (url , cookieString );
151
164
mCookieSyncManager .sync ();
152
165
promise .resolve (true );
153
166
} else {
154
- mCookieManager .setCookie (url , cookieString , new ValueCallback <Boolean >() {
167
+ cookieManager .setCookie (url , cookieString , new ValueCallback <Boolean >() {
155
168
@ Override
156
169
public void onReceiveValue (Boolean value ) {
157
170
promise .resolve (value );
158
171
}
159
172
});
160
- mCookieManager .flush ();
173
+ cookieManager .flush ();
161
174
}
162
175
} catch (Exception e ) {
163
176
promise .reject (e );
0 commit comments