CredentialStorage.cpp [plain text]
#include "config.h"
#include "CredentialStorage.h"
#include "Credential.h"
#include "KURL.h"
#include "ProtectionSpaceHash.h"
#include <wtf/text/WTFString.h>
#include <wtf/text/StringHash.h>
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
#include <wtf/MainThread.h>
#include <wtf/StdLibExtras.h>
#if PLATFORM(IOS)
#include "WebCoreThread.h"
#endif
namespace WebCore {
typedef HashMap<ProtectionSpace, Credential> ProtectionSpaceToCredentialMap;
static ProtectionSpaceToCredentialMap& protectionSpaceToCredentialMap()
{
#if !PLATFORM(IOS)
ASSERT(isMainThread());
#else
ASSERT((isMainThread() || pthread_main_np()) && WebThreadIsLockedOrDisabled());
#endif // !PLATFORM(IOS)
DEFINE_STATIC_LOCAL(ProtectionSpaceToCredentialMap, map, ());
return map;
}
static HashSet<String>& originsWithCredentials()
{
#if !PLATFORM(IOS)
ASSERT(isMainThread());
#else
ASSERT((isMainThread() || pthread_main_np()) && WebThreadIsLockedOrDisabled());
#endif // !PLATFORM(IOS)
DEFINE_STATIC_LOCAL(HashSet<String>, set, ());
return set;
}
typedef HashMap<String, ProtectionSpace> PathToDefaultProtectionSpaceMap;
static PathToDefaultProtectionSpaceMap& pathToDefaultProtectionSpaceMap()
{
#if !PLATFORM(IOS)
ASSERT(isMainThread());
#else
ASSERT((isMainThread() || pthread_main_np()) && WebThreadIsLockedOrDisabled());
#endif // !PLATFORM(IOS)
DEFINE_STATIC_LOCAL(PathToDefaultProtectionSpaceMap, map, ());
return map;
}
static String originStringFromURL(const KURL& url)
{
if (url.port())
return url.protocol() + "://" + url.host() + ':' + String::number(url.port()) + '/';
return url.protocol() + "://" + url.host() + '/';
}
static String protectionSpaceMapKeyFromURL(const KURL& url)
{
ASSERT(url.isValid());
String directoryURL = url.string().substring(0, url.pathEnd());
unsigned directoryURLPathStart = url.pathStart();
ASSERT(directoryURL[directoryURLPathStart] == '/');
if (directoryURL.length() > directoryURLPathStart + 1) {
size_t index = directoryURL.reverseFind('/');
ASSERT(index != notFound);
directoryURL = directoryURL.substring(0, (index != directoryURLPathStart) ? index : directoryURLPathStart + 1);
}
return directoryURL;
}
void CredentialStorage::set(const Credential& credential, const ProtectionSpace& protectionSpace, const KURL& url)
{
ASSERT(protectionSpace.isProxy() || url.protocolIsInHTTPFamily());
ASSERT(protectionSpace.isProxy() || url.isValid());
protectionSpaceToCredentialMap().set(protectionSpace, credential);
#if PLATFORM(IOS)
saveToPersistentStorage(protectionSpace, credential);
#endif
if (!protectionSpace.isProxy()) {
originsWithCredentials().add(originStringFromURL(url));
ProtectionSpaceAuthenticationScheme scheme = protectionSpace.authenticationScheme();
if (scheme == ProtectionSpaceAuthenticationSchemeHTTPBasic || scheme == ProtectionSpaceAuthenticationSchemeDefault) {
pathToDefaultProtectionSpaceMap().set(protectionSpaceMapKeyFromURL(url), protectionSpace);
}
}
}
Credential CredentialStorage::get(const ProtectionSpace& protectionSpace)
{
return protectionSpaceToCredentialMap().get(protectionSpace);
}
void CredentialStorage::remove(const ProtectionSpace& protectionSpace)
{
protectionSpaceToCredentialMap().remove(protectionSpace);
}
static PathToDefaultProtectionSpaceMap::iterator findDefaultProtectionSpaceForURL(const KURL& url)
{
ASSERT(url.protocolIsInHTTPFamily());
ASSERT(url.isValid());
PathToDefaultProtectionSpaceMap& map = pathToDefaultProtectionSpaceMap();
if (!originsWithCredentials().contains(originStringFromURL(url)))
return map.end();
String directoryURL = protectionSpaceMapKeyFromURL(url);
unsigned directoryURLPathStart = url.pathStart();
while (true) {
PathToDefaultProtectionSpaceMap::iterator iter = map.find(directoryURL);
if (iter != map.end())
return iter;
if (directoryURL.length() == directoryURLPathStart + 1) return map.end();
size_t index = directoryURL.reverseFind('/', directoryURL.length() - 2);
ASSERT(index != notFound);
directoryURL = directoryURL.substring(0, (index == directoryURLPathStart) ? index + 1 : index);
ASSERT(directoryURL.length() > directoryURLPathStart);
ASSERT(directoryURL.length() == directoryURLPathStart + 1 || directoryURL[directoryURL.length() - 1] != '/');
}
}
bool CredentialStorage::set(const Credential& credential, const KURL& url)
{
ASSERT(url.protocolIsInHTTPFamily());
ASSERT(url.isValid());
PathToDefaultProtectionSpaceMap::iterator iter = findDefaultProtectionSpaceForURL(url);
if (iter == pathToDefaultProtectionSpaceMap().end())
return false;
ASSERT(originsWithCredentials().contains(originStringFromURL(url)));
protectionSpaceToCredentialMap().set(iter->value, credential);
return true;
}
Credential CredentialStorage::get(const KURL& url)
{
PathToDefaultProtectionSpaceMap::iterator iter = findDefaultProtectionSpaceForURL(url);
if (iter == pathToDefaultProtectionSpaceMap().end())
return Credential();
return protectionSpaceToCredentialMap().get(iter->value);
}
#if PLATFORM(IOS)
void CredentialStorage::clearCredentials()
{
pathToDefaultProtectionSpaceMap().clear();
originsWithCredentials().clear();
protectionSpaceToCredentialMap().clear();
}
#endif
void CredentialStorage::setPrivateMode(bool mode)
{
if (!mode)
protectionSpaceToCredentialMap().clear();
}
}