| Index: chrome/browser/net/network_controller.cc
|
| diff --git a/chrome/browser/net/network_controller.cc b/chrome/browser/net/network_controller.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..8d93e9a5643d0ad6c9635f5268554b9d98d1d826
|
| --- /dev/null
|
| +++ b/chrome/browser/net/network_controller.cc
|
| @@ -0,0 +1,136 @@
|
| +// 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/net/network_controller.h"
|
| +
|
| +#include "chrome/browser/net/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"
|
| +#include "net/base/request_priority.h"
|
| +#include "net/http/http_network_layer.h"
|
| +#include "net/http/http_network_session.h"
|
| +#include "net/http/http_network_transaction.h"
|
| +
|
| +NetworkController::NetworkController(
|
| + net::HttpNetworkSession* session)
|
| + : network_layer_(new net::HttpNetworkLayer(session)) {
|
| +}
|
| +
|
| +NetworkController::~NetworkController() {
|
| +}
|
| +
|
| +void NetworkController::AddTransaction(
|
| + uint64 transaction_id,
|
| + NetworkTransaction* transaction) {
|
| + transactions_[transaction_id] = transaction;
|
| +}
|
| +
|
| +void NetworkController::RemoveTransaction(uint64 transaction_id) {
|
| + transactions_.erase(transaction_id);
|
| +}
|
| +
|
| +// static
|
| +void NetworkController::SetBlockedDomains(
|
| + Profile* profile,
|
| + const std::string& client_id,
|
| + const std::vector<std::string>& blocked_domains) {
|
| + CHECK(profile);
|
| + content::ResourceContext* resourceContext = profile->GetResourceContext();
|
| + CHECK(resourceContext);
|
| + content::BrowserThread::PostTask(
|
| + content::BrowserThread::IO,
|
| + FROM_HERE,
|
| + base::Bind(
|
| + &NetworkController::SetBlockedDomainsOnIO,
|
| + resourceContext,
|
| + client_id,
|
| + blocked_domains));
|
| +}
|
| +
|
| +// static
|
| +void NetworkController::SetBlockedDomainsOnIO(
|
| + content::ResourceContext* resourceContext,
|
| + const std::string& client_id,
|
| + const std::vector<std::string>& blocked_domains) {
|
| + CHECK(resourceContext);
|
| + NetworkController* controller = ProfileIOData::FromResourceContext(
|
| + resourceContext)->network_controller();
|
| + CHECK(controller);
|
| + controller->SetBlockedDomains(client_id, blocked_domains);
|
| +}
|
| +
|
| +void NetworkController::SetBlockedDomains(
|
| + const std::string& client_id,
|
| + const std::vector<std::string>& blocked_domains) {
|
| + BlockedDomains* items = NULL;
|
| + BlockedDomainsMap::iterator client = blocked_domains_map_.find(client_id);
|
| + if (client != blocked_domains_map_.end())
|
| + items = client->second;
|
| + size_t size = blocked_domains.size();
|
| + if (!size) {
|
| + if (items) {
|
| + delete items;
|
| + blocked_domains_map_.erase(client);
|
| + }
|
| + } else {
|
| + if (items) {
|
| + items->clear();
|
| + } else {
|
| + items = new BlockedDomains();
|
| + blocked_domains_map_[client_id] = items;
|
| + }
|
| + for (size_t i = 0; i < size; ++i) {
|
| + items->push_back(blocked_domains[i]);
|
| + }
|
| + }
|
| +
|
| + Transactions::iterator it = transactions_.begin();
|
| + while (it != transactions_.end()) {
|
| + Transactions::iterator current = it++;
|
| + NetworkTransaction* transaction = current->second;
|
| + const GURL& url = transaction->GetURL();
|
| + for (size_t i = 0; i < size; ++i) {
|
| + if (url.DomainIs(blocked_domains[i].data())) {
|
| + transactions_.erase(current);
|
| + transaction->Stop();
|
| + break;
|
| + }
|
| + }
|
| + }
|
| +}
|
| +
|
| +bool NetworkController::IsBlockedURL(const GURL& url) {
|
| + BlockedDomainsMap::const_iterator client = blocked_domains_map_.begin();
|
| + for (; client != blocked_domains_map_.end(); ++client) {
|
| + BlockedDomains* items = client->second;
|
| + BlockedDomains::const_iterator domain = items->begin();
|
| + for (; domain != items->end(); ++domain) {
|
| + if (url.DomainIs(domain->data()))
|
| + return true;
|
| + }
|
| + }
|
| + return false;
|
| +}
|
| +
|
| +int NetworkController::CreateTransaction(
|
| + net::RequestPriority priority,
|
| + scoped_ptr<net::HttpTransaction>* trans) {
|
| + scoped_ptr<net::HttpTransaction> network_transaction;
|
| + int rv = network_layer_->CreateTransaction(priority, &network_transaction);
|
| + if (rv != net::OK) {
|
| + return rv;
|
| + }
|
| + trans->reset(new NetworkTransaction(this, network_transaction));
|
| + return net::OK;
|
| +}
|
| +
|
| +net::HttpCache* NetworkController::GetCache() {
|
| + return network_layer_->GetCache();
|
| +}
|
| +
|
| +net::HttpNetworkSession* NetworkController::GetSession() {
|
| + return network_layer_->GetSession();
|
| +}
|
|
|