2015-09-26 18:19:27 +00:00
|
|
|
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE-CHROMIUM file.
|
|
|
|
|
|
|
|
#include "browser/net/devtools_network_controller.h"
|
|
|
|
|
|
|
|
#include "browser/net/devtools_network_conditions.h"
|
|
|
|
#include "browser/net/devtools_network_interceptor.h"
|
|
|
|
#include "browser/net/devtools_network_transaction.h"
|
|
|
|
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
|
|
|
|
using content::BrowserThread;
|
|
|
|
|
|
|
|
namespace brightray {
|
|
|
|
|
|
|
|
DevToolsNetworkController::DevToolsNetworkController()
|
|
|
|
: default_interceptor_(new DevToolsNetworkInterceptor) {
|
|
|
|
}
|
|
|
|
|
|
|
|
DevToolsNetworkController::~DevToolsNetworkController() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void DevToolsNetworkController::SetNetworkState(
|
|
|
|
const std::string& client_id,
|
|
|
|
scoped_ptr<DevToolsNetworkConditions> conditions) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
|
|
|
|
|
|
|
if (client_id.empty()) {
|
|
|
|
if (!conditions)
|
|
|
|
return;
|
2016-03-08 11:59:29 +00:00
|
|
|
default_interceptor_->UpdateConditions(std::move(conditions));
|
2015-09-26 18:19:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto interceptor = interceptors_.get(client_id);
|
|
|
|
if (!interceptor) {
|
|
|
|
if (!conditions)
|
|
|
|
return;
|
|
|
|
scoped_ptr<DevToolsNetworkInterceptor> new_interceptor(
|
|
|
|
new DevToolsNetworkInterceptor);
|
2016-03-08 11:59:29 +00:00
|
|
|
new_interceptor->UpdateConditions(std::move(conditions));
|
|
|
|
interceptors_.set(client_id, std::move(new_interceptor));
|
2015-09-26 18:19:27 +00:00
|
|
|
} else {
|
|
|
|
if (!conditions) {
|
|
|
|
scoped_ptr<DevToolsNetworkConditions> online_conditions(
|
|
|
|
new DevToolsNetworkConditions(false));
|
2016-03-08 11:59:29 +00:00
|
|
|
interceptor->UpdateConditions(std::move(online_conditions));
|
2015-09-26 18:19:27 +00:00
|
|
|
interceptors_.erase(client_id);
|
|
|
|
} else {
|
2016-03-08 11:59:29 +00:00
|
|
|
interceptor->UpdateConditions(std::move(conditions));
|
2015-09-26 18:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
base::WeakPtr<DevToolsNetworkInterceptor>
|
|
|
|
DevToolsNetworkController::GetInterceptor(DevToolsNetworkTransaction* transaction) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
|
|
|
DCHECK(transaction->request());
|
|
|
|
|
|
|
|
if (!interceptors_.size())
|
|
|
|
return default_interceptor_->GetWeakPtr();
|
|
|
|
|
|
|
|
transaction->ProcessRequest();
|
|
|
|
auto& client_id = transaction->client_id();
|
|
|
|
auto interceptor = interceptors_.get(client_id);
|
|
|
|
if (!interceptor)
|
|
|
|
return default_interceptor_->GetWeakPtr();
|
|
|
|
|
|
|
|
return interceptor->GetWeakPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace brightray
|