Chromium Code Reviews| Index: chrome/browser/devtools/devtools_network_controller.cc |
| diff --git a/chrome/browser/devtools/devtools_network_controller.cc b/chrome/browser/devtools/devtools_network_controller.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..599e0f490d056aba7c95cd49d1db8964637ed3b5 |
| --- /dev/null |
| +++ b/chrome/browser/devtools/devtools_network_controller.cc |
| @@ -0,0 +1,97 @@ |
| +// Copyright 2014 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 file. |
| + |
| +#include "chrome/browser/devtools/devtools_network_controller.h" |
| + |
| +#include "chrome/browser/devtools/devtools_network_transaction.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/profiles/profile_io_data.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/resource_context.h" |
| + |
| +DevToolsNetworkController::DevToolsNetworkController() { |
| +} |
| + |
| +DevToolsNetworkController::~DevToolsNetworkController() { |
| +} |
| + |
| +void DevToolsNetworkController::AddTransaction( |
| + DevToolsNetworkTransaction* transaction) { |
| + transactions_.insert(transaction); |
| +} |
| + |
| +void DevToolsNetworkController::RemoveTransaction( |
| + DevToolsNetworkTransaction* transaction) { |
| + DCHECK(transactions_.find(transaction) != transactions_.end()); |
| + transactions_.erase(transaction); |
| +} |
| + |
| +// static |
| +void DevToolsNetworkController::DisableNetwork( |
| + Profile* profile, |
| + const std::string& client_id, |
| + bool disable_network) { |
| + DCHECK(profile); |
| + content::ResourceContext* resource_context = profile->GetResourceContext(); |
| + DCHECK(resource_context); |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind( |
| + &DevToolsNetworkController::DisableNetworkOnIO, |
| + resource_context, |
| + client_id, |
| + disable_network)); |
| +} |
| + |
| +// static |
| +void DevToolsNetworkController::DisableNetworkOnIO( |
| + content::ResourceContext* resource_context, |
| + const std::string& client_id, |
| + bool disable_network) { |
| + DCHECK(resource_context); |
| + DevToolsNetworkController* controller = ProfileIOData::FromResourceContext( |
| + resource_context)->network_controller(); |
| + DCHECK(controller); |
| + controller->DisableNetwork(client_id, disable_network); |
| +} |
| + |
| +void DevToolsNetworkController::DisableNetwork( |
| + const std::string& client_id, |
| + bool disable_network) { |
| + if (!disable_network) { |
| + clients_.erase(client_id); |
| + return; |
| + } |
| + clients_.insert(client_id); |
| + |
| + // Iterate over a copy of set, because failing of transaction could result in |
| + // creating a new one, or (theoretically) destroying one. |
| + std::vector<DevToolsNetworkTransaction*> transactions(transactions_.size()); |
|
mmenke
2014/04/21 17:16:51
Suggest calling this old_transactions, for clarity
eustas
2014/04/22 14:23:09
Done.
|
| + std::copy(transactions_.begin(), transactions_.end(), transactions.begin()); |
|
mmenke
2014/04/21 17:16:51
Is there a reason you're not just using:
std::set
eustas
2014/04/22 14:23:09
No reason, just old java habits =)
Done.
|
| + std::vector<DevToolsNetworkTransaction*>::iterator it = transactions.begin(); |
| + while (it != transactions.end()) { |
| + DevToolsNetworkTransaction* transaction = *it++; |
|
mmenke
2014/04/21 17:16:51
No need for the extra complexity now - Can just ma
eustas
2014/04/22 14:23:09
Done.
|
| + if (transactions_.find(transaction) == transactions_.end()) |
| + continue; |
| + if (!transaction->started() || transaction->failed()) |
| + continue; |
| + if (ShouldFail(transaction)) |
| + transaction->Fail(); |
| + } |
| +} |
| + |
| +bool DevToolsNetworkController::ShouldFail( |
| + const DevToolsNetworkTransaction* transaction) { |
| + DCHECK(transaction); |
| + if (clients_.empty()) |
| + return false; |
| + |
| + if (transaction->has_devtools_request_header()) |
| + return false; |
| + |
| + // TODO: Add domain blacklist. |
| + |
| + return true; |
| +} |