Fixed sometimes focus ring is hidden when context menu is shown by longTap
[framework/web/webkit-efl.git] / Tools / QtTestBrowser / cookiejar.cpp
1 /*
2  * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "config.h"
29
30 #include "cookiejar.h"
31
32 #if HAVE(QT5)
33 #include <QStandardPaths>
34 #else
35 #include <QDesktopServices>
36 #endif
37 #include <QDir>
38 #include <QTextStream>
39
40 TestBrowserCookieJar::TestBrowserCookieJar(QObject* parent)
41     : QNetworkCookieJar(parent)
42     , m_storageEnabled(false)
43 {
44     // We use a timer for the real disk write to avoid multiple IO
45     // syscalls in sequence (when loading pages which set multiple cookies).
46     m_timer.setInterval(10000);
47     m_timer.setSingleShot(true);
48     connect(&m_timer, SIGNAL(timeout()), this, SLOT(saveToDisk()));
49
50 #ifndef QT_NO_DESKTOPSERVICES
51 #if HAVE(QT5)
52     QString path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
53 #else
54     QString path = QDesktopServices::storageLocation(QDesktopServices::CacheLocation);
55 #endif
56 #else
57     QString path = QDir::homePath() + "/.QtTestBrowser";
58 #endif
59
60     QDir().mkpath(path);
61     m_file.setFileName(path + "/cookieJar");
62 }
63
64 TestBrowserCookieJar::~TestBrowserCookieJar()
65 {
66     if (m_storageEnabled) {
67         extractRawCookies();
68         saveToDisk();
69     }
70 }
71
72 bool TestBrowserCookieJar::setCookiesFromUrl(const QList<QNetworkCookie>& cookieList, const QUrl& url)
73 {
74     bool status = QNetworkCookieJar::setCookiesFromUrl(cookieList, url);
75     if (status && m_storageEnabled)
76         scheduleSaveToDisk();
77     return status;
78 }
79
80 void TestBrowserCookieJar::setDiskStorageEnabled(bool enabled)
81 {
82     m_storageEnabled = enabled;
83
84     if (enabled && allCookies().isEmpty())
85         loadFromDisk();
86
87     // When disabling, save current cookies.
88     if (!enabled && !allCookies().isEmpty())
89         scheduleSaveToDisk();
90 }
91
92 void TestBrowserCookieJar::scheduleSaveToDisk()
93 {
94     // We extract the raw cookies here because the user may
95     // enable/disable/clear cookies while the timer is running.
96     extractRawCookies();
97     m_timer.start();
98 }
99
100 void TestBrowserCookieJar::extractRawCookies()
101 {
102     QList<QNetworkCookie> cookies = allCookies();
103     m_rawCookies.clear();
104
105     foreach (const QNetworkCookie &cookie, cookies) {
106         if (!cookie.isSessionCookie())
107             m_rawCookies.append(cookie.toRawForm());
108     }
109 }
110
111 void TestBrowserCookieJar::saveToDisk()
112 {
113     m_timer.stop();
114
115     if (m_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
116         QTextStream out(&m_file);
117         foreach (const QByteArray &cookie, m_rawCookies)
118             out << cookie + "\n";
119         m_file.close();
120     } else
121         qWarning("IO error handling cookiejar file");
122 }
123
124 void TestBrowserCookieJar::loadFromDisk()
125 {
126     if (!m_file.exists())
127         return;
128
129     QList<QNetworkCookie> cookies;
130
131     if (m_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
132         QTextStream in(&m_file);
133         while (!in.atEnd())
134             cookies.append(QNetworkCookie::parseCookies(in.readLine().toUtf8()));
135         m_file.close();
136     } else
137         qWarning("IO error handling cookiejar file");
138
139     setAllCookies(cookies);
140 }
141
142 void TestBrowserCookieJar::reset()
143 {
144     setAllCookies(QList<QNetworkCookie>());
145     if (m_storageEnabled)
146         scheduleSaveToDisk();
147 }