1 /*
2 * Copyright 2010, The Android Open Source Project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "WebRequestContext.h"
28
29 #include "ChromiumIncludes.h"
30 #include "ChromiumInit.h"
31 #include "WebCache.h"
32 #include "WebCookieJar.h"
33
34 #include <wtf/text/CString.h>
35
36 static std::string acceptLanguageStdString("");
37 static WTF::String acceptLanguageWtfString("");
38 static WTF::Mutex acceptLanguageMutex;
39
40 static int numPrivateBrowsingInstances;
41
42 extern void ANPSystemInterface_CleanupIncognito();
43
44 using namespace WTF;
45
46 namespace android {
47
WebRequestContext(bool isPrivateBrowsing)48 WebRequestContext::WebRequestContext(bool isPrivateBrowsing)
49 : m_isPrivateBrowsing(isPrivateBrowsing)
50 {
51 // Initialize chromium logging, needs to be done before any chromium code is called.
52 initChromium();
53
54 if (m_isPrivateBrowsing) {
55 // Delete the old files if this is the first private browsing instance
56 // They are probably leftovers from a power cycle
57 // We do not need to clear the cache as it is in memory only for private browsing
58 if (!numPrivateBrowsingInstances)
59 WebCookieJar::cleanup(true);
60 numPrivateBrowsingInstances++;
61 }
62
63 WebCache* cache = WebCache::get(m_isPrivateBrowsing);
64 set_host_resolver(cache->hostResolver());
65 set_http_transaction_factory(cache->cache());
66
67 WebCookieJar* cookieJar = WebCookieJar::get(m_isPrivateBrowsing);
68 set_cookie_store(cookieJar->cookieStore());
69 set_cookie_policy(cookieJar);
70
71 // Also hardcoded in FrameLoader.java
72 set_accept_charset("utf-8, iso-8859-1, utf-16, *;q=0.7");
73 }
74
~WebRequestContext()75 WebRequestContext::~WebRequestContext()
76 {
77 if (m_isPrivateBrowsing) {
78 numPrivateBrowsingInstances--;
79
80 // This is the last private browsing context, delete the cookies and cache
81 if (!numPrivateBrowsingInstances) {
82 WebCookieJar::cleanup(true);
83 WebCache::cleanup(true);
84 ANPSystemInterface_CleanupIncognito();
85 }
86 }
87 }
88
setUserAgent(const String & string)89 void WebRequestContext::setUserAgent(const String& string)
90 {
91 MutexLocker lock(m_userAgentMutex);
92 m_userAgent = string.utf8().data();
93 }
94
setCacheMode(int mode)95 void WebRequestContext::setCacheMode(int mode)
96 {
97 m_cacheMode = mode;
98 }
99
getCacheMode()100 int WebRequestContext::getCacheMode()
101 {
102 return m_cacheMode;
103 }
104
GetUserAgent(const GURL & url) const105 const std::string& WebRequestContext::GetUserAgent(const GURL& url) const
106 {
107 MutexLocker lock(m_userAgentMutex);
108 return m_userAgent;
109 }
110
setAcceptLanguage(const String & string)111 void WebRequestContext::setAcceptLanguage(const String& string)
112 {
113 MutexLocker lock(acceptLanguageMutex);
114 acceptLanguageStdString = string.utf8().data();
115 acceptLanguageWtfString = string;
116 }
117
accept_language() const118 const std::string& WebRequestContext::accept_language() const
119 {
120 MutexLocker lock(acceptLanguageMutex);
121 return acceptLanguageStdString;
122 }
123
acceptLanguage()124 const String& WebRequestContext::acceptLanguage()
125 {
126 MutexLocker lock(acceptLanguageMutex);
127 return acceptLanguageWtfString;
128 }
129
130 } // namespace android
131