|
| 1 | +/**************************************************************************** |
| 2 | +** |
| 3 | +** Copyright © 1992-2014 Cisco and/or its affiliates. All rights reserved. |
| 4 | +** All rights reserved. |
| 5 | +** |
| 6 | +** $CISCO_BEGIN_LICENSE:LGPL$ |
| 7 | +** |
| 8 | +** GNU Lesser General Public License Usage |
| 9 | +** Alternatively, this file may be used under the terms of the GNU Lesser |
| 10 | +** General Public License version 2.1 as published by the Free Software |
| 11 | +** Foundation and appearing in the file LICENSE.LGPL included in the |
| 12 | +** packaging of this file. Please review the following information to |
| 13 | +** ensure the GNU Lesser General Public License version 2.1 requirements |
| 14 | +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 15 | +** |
| 16 | +** $CISCO_END_LICENSE$ |
| 17 | +** |
| 18 | +****************************************************************************/ |
| 19 | + |
| 20 | +#include "extension_qt/qnetwork_access_manager_tracer.h" |
| 21 | +#include "webdriver_session.h" |
| 22 | +#include "webdriver_util.h" |
| 23 | +#include "web_view_util.h" |
| 24 | + |
| 25 | +#include "base/values.h" |
| 26 | +#include "base/time.h" |
| 27 | +#include "base/threading/platform_thread.h" |
| 28 | +#include "base/string_number_conversions.h" |
| 29 | + |
| 30 | +#include <QtCore/QThread> |
| 31 | +#include <QtNetwork/QNetworkReply> |
| 32 | + |
| 33 | +QNetworkAccessManagerTracer::QNetworkAccessManagerTracer(webdriver::Session* session, QObject* parent) |
| 34 | + : QNetworkAccessManager(parent), session_(session) { |
| 35 | +} |
| 36 | + |
| 37 | +QNetworkAccessManagerTracer::~QNetworkAccessManagerTracer() { } |
| 38 | + |
| 39 | +QNetworkReply* QNetworkAccessManagerTracer::createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData) { |
| 40 | + |
| 41 | + timeStamp_ = static_cast<double>(base::TimeTicks::NowFromSystemTraceTime().ToInternalValue()); |
| 42 | + QNetworkReply* reply = QNetworkAccessManager::createRequest(op, req, outgoingData); |
| 43 | + connect(this, SIGNAL(finished(QNetworkReply*)), this, SLOT(writeReply(QNetworkReply*))); |
| 44 | + |
| 45 | + return reply; |
| 46 | +} |
| 47 | + |
| 48 | +void QNetworkAccessManagerTracer::writeReply(QNetworkReply *reply) { |
| 49 | + webdriver::LogLevel level = session_->GetMinPerfLogLevel(); |
| 50 | + |
| 51 | + double thread_timestamp = static_cast<double>((base::TimeTicks::IsThreadNowSupported() ? |
| 52 | + base::TimeTicks::ThreadNow() : base::TimeTicks()).ToInternalValue()); |
| 53 | + |
| 54 | + std::string file = reply->url().path().toStdString(); |
| 55 | + // delete '/' in the beginning: |
| 56 | + file.erase(0, 1); |
| 57 | + |
| 58 | + //HTTP status code |
| 59 | + QVariant statusCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ); |
| 60 | + QString reason; |
| 61 | + if ( !statusCode.isValid() ) { |
| 62 | + reason = "INVALID"; |
| 63 | + level = webdriver::LogLevelFromString("SEVERE"); |
| 64 | + } else { |
| 65 | + int status = statusCode.toInt(); |
| 66 | + if ( status != 200 ) { |
| 67 | + level = webdriver::LogLevelFromString("WARNING"); |
| 68 | + } else { |
| 69 | + level = webdriver::LogLevelFromString("INFO"); |
| 70 | + } |
| 71 | + reason = reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString(); |
| 72 | + } |
| 73 | + base::DictionaryValue* args_entry = new base::DictionaryValue(); |
| 74 | + args_entry->SetString("method", getMethod(reply->operation())); |
| 75 | + args_entry->SetString("status", reason.toStdString()); |
| 76 | + args_entry->SetString("file", file); |
| 77 | + |
| 78 | + base::DictionaryValue* message_entry = new base::DictionaryValue(); |
| 79 | + message_entry->Set("args", args_entry); |
| 80 | + message_entry->SetDouble("ts", timeStamp_); |
| 81 | + message_entry->SetInteger("tid", static_cast<int>(base::PlatformThread::CurrentId())); |
| 82 | + message_entry->SetDouble("tts", thread_timestamp); |
| 83 | + |
| 84 | + base::DictionaryValue* entry = new base::DictionaryValue; |
| 85 | + std::string webview = webdriver::QWebViewUtil::getWebView(session_,session_->current_view())->metaObject()->className(); |
| 86 | + entry->SetString("webview", webview); |
| 87 | + entry->Set("message", message_entry); |
| 88 | + std::string log_entry = webdriver::JsonStringifyForDisplay(entry); |
| 89 | + |
| 90 | + session_->AddPerfLogEntry(level, log_entry); |
| 91 | + delete entry; |
| 92 | + |
| 93 | +} |
| 94 | + |
| 95 | +std::string QNetworkAccessManagerTracer::getMethod(QNetworkAccessManager::Operation op) { |
| 96 | + std::string method; |
| 97 | + switch (op) { |
| 98 | + case QNetworkAccessManager::HeadOperation: |
| 99 | + method = "HEAD"; |
| 100 | + break; |
| 101 | + case QNetworkAccessManager::GetOperation: |
| 102 | + method = "GET"; |
| 103 | + break; |
| 104 | + case QNetworkAccessManager::PutOperation: |
| 105 | + method= "PUT"; |
| 106 | + break; |
| 107 | + case QNetworkAccessManager::PostOperation: |
| 108 | + method = "POST"; |
| 109 | + break; |
| 110 | + case QNetworkAccessManager::DeleteOperation: |
| 111 | + method = "DELETE"; |
| 112 | + break; |
| 113 | + case QNetworkAccessManager::CustomOperation: |
| 114 | + method = "custom"; |
| 115 | + break; |
| 116 | + default: |
| 117 | + method = "unsupported method"; |
| 118 | + break; |
| 119 | + } |
| 120 | + return method; |
| 121 | +} |
| 122 | + |
0 commit comments