Android中CookieManager的底层实现
前几天,项目组有个技术问题,想用本地加载html, js写cookie的方法,绕过去。
想法是没有问题的, 但是测试的时候发现, 每次重新打开App, 都取不到以前Cookie里面设的值。
后来去App/data下面找WebView.db,打开来一看,cookie情报根本没有存。
原因是js写cookie的时候,没有指明expire, WebKit默认把它当成临时cookie, webview终了之后就丢失了。
这个问题本身不复杂,不过趁机我看了一把Andorid底层读取Cookie的source root:
以下source root,转载请注明出自《南湖边上的小木屋》
1. 取Cookie的API
[java]
CookieManager.getInstance().getCookie(url);
2.CookieManager的getInstance()
[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是个工厂模式,额
[java]
static synchronized WebViewFactoryProvider getProvider() {
// For now the main purpose of this function (and the factory abstraction) 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.层层嵌套,最后生成的instance是CookieManagerClassic
[java]
@Override
public CookieManager getCookieManager() {
return CookieManagerClassic.getInstance();
}
5. getCookie方法 实现在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定义在CookieManager.cpp里, 恩。。。。JNI。。。
[java]
static jstring getCookie(JNIEnv* env, jobject, jstring url, jboolean privateBrowsing)
{
GURL gurl(jstringToStdString(env, url));
CookieOptions options;
options.set_include_httponly();
std::string cookies = WebCookieJar::get(privateBrowsing)->cookieStore()->GetCookieMonster()->GetCookiesWithOptions(gurl, options);
return stdStringToJstring(env, cookies);
}
7. WebCookieJar.cpp中,定义了从哪里去取得Cookie情报
[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以上是webviewCookiesChromium.db, 以下是WebView.db文件
8.最后读取Cookie情报发生在cookie_monster.cc。 额,最后调到C++, 去读.db文件,无敌了
[java]
std::string CookieMonster::GetCookiesWithOptions(const GURL& url,
const CookieOptions& options) {
base::AutoLock 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 += "; ";
 
补充:移动开发 , Android ,