Android _ underlying implementation of cookiemanager in Android (12)

Source: Internet
Author: User

A few days ago, the project team had a technical problem and wanted to use the method of loading HTML locally and writing cookies by JS.

There is no problem with the idea, but during the test, we found that every time we re-open the app, we couldn't get the value set in the previous cookie.

Later, I went to the app/data and looked for webview. dB. The cookie information was not stored.

The reason is that expire is not specified when JS writes a cookie. WebKit treats it as a temporary cookie by default and is lost after webview ends.

This problem is not complicated, but I took the opportunity to read the source root of the cookie from the underlying Android ID:

 

The following source root is reprinted. Please indicate that it is from the Southern Lake's cabin.

 

1. Cookie retrieval API

[Java]
Cookiemanager. getinstance (). getcookie (URL );

2. getinstance () of cookiemanager ()

[Java]
/**
* Gets the singleton cookiemanager instance. If this method is used
* Before the application instantiates a {@ link webview} instance,
* {@ Link cookiesyncmanager # createinstance (context)} must be called
* First.
*
* @ Return the singleton cookiemanager instance
*/
Public static synchronized cookiemanager getinstance (){
Return webviewfactory. getprovider (). getcookiemanager ();
}

3. webviewfactory is a factory model.
[Java]
Static synchronized webviewfactoryprovider getprovider (){
// For now the main purpose of this function (and the factory defined action) is to keep
// Us honest and minimize usage of webviewclassic internals when binding the proxy.
If (sproviderinstance! = NULL) return sproviderinstance;
 
Sproviderinstance = getfactorybyname (default_web_view_factory );
If (sproviderinstance = NULL ){
If (Debug) log. V (logtag, "falling back to explicit linkage ");
Sproviderinstance = new webviewclassic. Factory ();
}
Return sproviderinstance;
}

4. layer-by-layer nesting. The last generated instance is cookiemanagerclassic.
[Java]
@ Override
Public cookiemanager getcookiemanager (){
Return cookiemanagerclassic. getinstance ();
}

5. The getcookie method is implemented on cookiemanagerclassic.
[Java]
@ Override
Public String getcookie (string URL ){
Return getcookie (URL, false );
}
 
@ Override
Public String getcookie (string URL, Boolean privatebrowsing ){
Webaddress URI;
Try {
Uri = new webaddress (URL );
} Catch (parseexception ex ){
Log. E (logtag, "Bad address:" + URL );
Return NULL;
}
 
Return nativegetcookie (URI. tostring (), privatebrowsing );
}

6. nativegetcookie is defined in cookiemanager. cpp .... JNI...
[Java]
Static jstring getcookie (jnienv * ENV, jobject, jstring URL, jboolean privatebrowsing)
{
Gurl (jstringtostdstring (ENV, URL ));
Cookieoptions;
Options. set_include_httponly ();
STD: String cookies = webcookiejar: Get (privatebrowsing)-> cookiestore ()-> getcookiemonster ()-> getcookieswithoptions (gurl, options );
Return stdstringtojstring (ENV, cookies );
}

7. webcookiejar. cpp defines where to obtain Cookie Information
[Java]
Webcookiejar * webcookiejar: Get (bool isprivatebrowsing)
{
Mutexlocker lock (instancemutex );
If (! Isfirstinstancecreated & fileschemecookiesenabled)
Net: cookiemonster: enablefilescheme ();
Isfirstinstancecreated = true;
Scoped_refptr <webcookiejar> * instanceptr = instance (isprivatebrowsing );
If (! Instanceptr-> get ())
* Instanceptr = new webcookiejar (databasedirectory (isprivatebrowsing ));
Return instanceptr-> get ();
}

[Java]
Static STD: String databasedirectory (bool isprivatebrowsing)
{
Static const char * const kdatabasefilename = "/webviewcookieschromium. DB ";
Static const char * const kdatabasefilenameprivatebrowsing = "/webviewcookieschromiumprivate. DB ";
 
STD: String databasefilepath = databasedirectory ();
Databasefilepath. append (isprivatebrowsing? Kdatabasefilenameprivatebrowsing: kdatabasefilename );
Return databasefilepath;
}

※Android3.0 and above are webviewcookieschromium. dB. The following is the webview. DB file.

8. Finally, the cookie information is read in cookie_monster.cc. Amount, and finally transferred to C ++ to read the. DB file, invincible

[Java]
STD: String cookiemonster: getcookieswithoptions (const gurl & URL,
Const cookieoptions & options ){
Base: autolock (Lock _);
Initifnecessary ();
 
If (! Hascookieablescheme (URL )){
Return STD: string ();
}
 
Timeticks start_time (timeticks: Now ());
 
// Get the cookies for this host and Its Domain (s ).
STD: vector <canonicalcookie *> cookies;
Findcookiesforhostanddomain (URL, options, true, & cookies );
STD: Sort (cookies. Begin (), cookies. End (), cookiesorter );
 
STD: String cookie_line;
For (STD: vector <canonicalcookie *>: const_iterator it = cookies. Begin ();
It! = Cookies. End (); ++ it ){
If (it! = Cookies. Begin ())
Cookie_line + = ";";
// In Mozilla if you set a cookie like AAAA, it will have an empty token
// And a value of AAAA. When it sends the cookie back, it will send AAAA,
// So we need to avoid sending = AAAA for a blank token value.
If (! (* It)-> name (). Empty ())
Cookie_line + = (* It)-> name () + "= ";
Cookie_line + = (* It)-> value ();
}
 
Histogram_time_get _-> addtime (timeticks: Now ()-start_time );
 
Vlog (kvloggetcookies) <"getcookies () Result:" <cookie_line;
 
Return cookie_line;
}

# Above #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.