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"
|
|
|
|
|
2017-05-18 22:29:22 +00:00
|
|
|
#include "base/bind.h"
|
2015-09-26 18:19:27 +00:00
|
|
|
#include "browser/net/devtools_network_conditions.h"
|
|
|
|
#include "browser/net/devtools_network_interceptor.h"
|
|
|
|
#include "browser/net/devtools_network_transaction.h"
|
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
|
|
|
|
using content::BrowserThread;
|
|
|
|
|
|
|
|
namespace brightray {
|
|
|
|
|
|
|
|
DevToolsNetworkController::DevToolsNetworkController()
|
2016-03-13 21:05:22 +00:00
|
|
|
: appcache_interceptor_(new DevToolsNetworkInterceptor) {
|
2015-09-26 18:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DevToolsNetworkController::~DevToolsNetworkController() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void DevToolsNetworkController::SetNetworkState(
|
|
|
|
const std::string& client_id,
|
2016-05-23 01:59:07 +00:00
|
|
|
std::unique_ptr<DevToolsNetworkConditions> conditions) {
|
2015-09-26 18:19:27 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
|
|
|
|
2017-04-04 04:26:50 +00:00
|
|
|
auto it = interceptors_.find(client_id);
|
|
|
|
if (it == interceptors_.end()) {
|
2015-09-26 18:19:27 +00:00
|
|
|
if (!conditions)
|
|
|
|
return;
|
2016-05-23 01:59:07 +00:00
|
|
|
std::unique_ptr<DevToolsNetworkInterceptor> new_interceptor(
|
2015-09-26 18:19:27 +00:00
|
|
|
new DevToolsNetworkInterceptor);
|
2016-03-08 11:59:29 +00:00
|
|
|
new_interceptor->UpdateConditions(std::move(conditions));
|
2017-04-04 04:26:50 +00:00
|
|
|
interceptors_[client_id] = std::move(new_interceptor);
|
2015-09-26 18:19:27 +00:00
|
|
|
} else {
|
|
|
|
if (!conditions) {
|
2016-05-23 01:59:07 +00:00
|
|
|
std::unique_ptr<DevToolsNetworkConditions> online_conditions(
|
2015-09-26 18:19:27 +00:00
|
|
|
new DevToolsNetworkConditions(false));
|
2017-04-04 04:26:50 +00:00
|
|
|
it->second->UpdateConditions(std::move(online_conditions));
|
2015-09-26 18:19:27 +00:00
|
|
|
interceptors_.erase(client_id);
|
|
|
|
} else {
|
2017-04-04 04:26:50 +00:00
|
|
|
it->second->UpdateConditions(std::move(conditions));
|
2015-09-26 18:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-13 21:05:22 +00:00
|
|
|
|
|
|
|
bool has_offline_interceptors = false;
|
2017-04-04 04:26:50 +00:00
|
|
|
for (const auto& interceptor : interceptors_) {
|
|
|
|
if (interceptor.second->IsOffline()) {
|
2016-03-13 21:05:22 +00:00
|
|
|
has_offline_interceptors = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_appcache_offline = appcache_interceptor_->IsOffline();
|
|
|
|
if (is_appcache_offline != has_offline_interceptors) {
|
2016-05-23 01:59:07 +00:00
|
|
|
std::unique_ptr<DevToolsNetworkConditions> appcache_conditions(
|
2016-03-13 21:05:22 +00:00
|
|
|
new DevToolsNetworkConditions(has_offline_interceptors));
|
|
|
|
appcache_interceptor_->UpdateConditions(std::move(appcache_conditions));
|
|
|
|
}
|
2015-09-26 18:19:27 +00:00
|
|
|
}
|
|
|
|
|
2016-03-13 21:05:22 +00:00
|
|
|
DevToolsNetworkInterceptor*
|
|
|
|
DevToolsNetworkController::GetInterceptor(const std::string& client_id) {
|
2015-09-26 18:19:27 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
|
|
|
|
2017-03-30 19:44:15 +00:00
|
|
|
if (interceptors_.empty() || client_id.empty())
|
2016-03-13 21:05:22 +00:00
|
|
|
return nullptr;
|
2015-09-26 18:19:27 +00:00
|
|
|
|
2017-04-04 04:26:50 +00:00
|
|
|
auto it = interceptors_.find(client_id);
|
|
|
|
if (it == interceptors_.end())
|
2016-03-13 21:05:22 +00:00
|
|
|
return nullptr;
|
2015-09-26 18:19:27 +00:00
|
|
|
|
2017-04-04 04:26:50 +00:00
|
|
|
return it->second.get();
|
2015-09-26 18:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace brightray
|