OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/net/network_controller.h" |
| 6 |
| 7 #include "chrome/browser/net/network_transaction.h" |
| 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/profiles/profile_io_data.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/resource_context.h" |
| 12 #include "net/base/request_priority.h" |
| 13 #include "net/http/http_network_layer.h" |
| 14 #include "net/http/http_network_session.h" |
| 15 #include "net/http/http_network_transaction.h" |
| 16 |
| 17 NetworkController::NetworkController( |
| 18 net::HttpNetworkSession* session) |
| 19 : network_layer_(new net::HttpNetworkLayer(session)) { |
| 20 } |
| 21 |
| 22 NetworkController::~NetworkController() { |
| 23 } |
| 24 |
| 25 void NetworkController::AddTransaction( |
| 26 uint64 transaction_id, |
| 27 NetworkTransaction* transaction) { |
| 28 transactions_[transaction_id] = transaction; |
| 29 } |
| 30 |
| 31 void NetworkController::RemoveTransaction(uint64 transaction_id) { |
| 32 transactions_.erase(transaction_id); |
| 33 } |
| 34 |
| 35 // static |
| 36 void NetworkController::SetBlockedDomains( |
| 37 Profile* profile, |
| 38 const std::string& client_id, |
| 39 const std::vector<std::string>& blocked_domains) { |
| 40 CHECK(profile); |
| 41 content::ResourceContext* resourceContext = profile->GetResourceContext(); |
| 42 CHECK(resourceContext); |
| 43 content::BrowserThread::PostTask( |
| 44 content::BrowserThread::IO, |
| 45 FROM_HERE, |
| 46 base::Bind( |
| 47 &NetworkController::SetBlockedDomainsOnIO, |
| 48 resourceContext, |
| 49 client_id, |
| 50 blocked_domains)); |
| 51 } |
| 52 |
| 53 // static |
| 54 void NetworkController::SetBlockedDomainsOnIO( |
| 55 content::ResourceContext* resourceContext, |
| 56 const std::string& client_id, |
| 57 const std::vector<std::string>& blocked_domains) { |
| 58 CHECK(resourceContext); |
| 59 NetworkController* controller = ProfileIOData::FromResourceContext( |
| 60 resourceContext)->network_controller(); |
| 61 CHECK(controller); |
| 62 controller->SetBlockedDomains(client_id, blocked_domains); |
| 63 } |
| 64 |
| 65 void NetworkController::SetBlockedDomains( |
| 66 const std::string& client_id, |
| 67 const std::vector<std::string>& blocked_domains) { |
| 68 BlockedDomains* items = NULL; |
| 69 BlockedDomainsMap::iterator client = blocked_domains_map_.find(client_id); |
| 70 if (client != blocked_domains_map_.end()) |
| 71 items = client->second; |
| 72 size_t size = blocked_domains.size(); |
| 73 if (!size) { |
| 74 if (items) { |
| 75 delete items; |
| 76 blocked_domains_map_.erase(client); |
| 77 } |
| 78 } else { |
| 79 if (items) { |
| 80 items->clear(); |
| 81 } else { |
| 82 items = new BlockedDomains(); |
| 83 blocked_domains_map_[client_id] = items; |
| 84 } |
| 85 for (size_t i = 0; i < size; ++i) { |
| 86 items->push_back(blocked_domains[i]); |
| 87 } |
| 88 } |
| 89 |
| 90 Transactions::iterator it = transactions_.begin(); |
| 91 while (it != transactions_.end()) { |
| 92 Transactions::iterator current = it++; |
| 93 NetworkTransaction* transaction = current->second; |
| 94 const GURL& url = transaction->GetURL(); |
| 95 for (size_t i = 0; i < size; ++i) { |
| 96 if (url.DomainIs(blocked_domains[i].data())) { |
| 97 transactions_.erase(current); |
| 98 transaction->Stop(); |
| 99 break; |
| 100 } |
| 101 } |
| 102 } |
| 103 } |
| 104 |
| 105 bool NetworkController::IsBlockedURL(const GURL& url) { |
| 106 BlockedDomainsMap::const_iterator client = blocked_domains_map_.begin(); |
| 107 for (; client != blocked_domains_map_.end(); ++client) { |
| 108 BlockedDomains* items = client->second; |
| 109 BlockedDomains::const_iterator domain = items->begin(); |
| 110 for (; domain != items->end(); ++domain) { |
| 111 if (url.DomainIs(domain->data())) |
| 112 return true; |
| 113 } |
| 114 } |
| 115 return false; |
| 116 } |
| 117 |
| 118 int NetworkController::CreateTransaction( |
| 119 net::RequestPriority priority, |
| 120 scoped_ptr<net::HttpTransaction>* trans) { |
| 121 scoped_ptr<net::HttpTransaction> network_transaction; |
| 122 int rv = network_layer_->CreateTransaction(priority, &network_transaction); |
| 123 if (rv != net::OK) { |
| 124 return rv; |
| 125 } |
| 126 trans->reset(new NetworkTransaction(this, network_transaction)); |
| 127 return net::OK; |
| 128 } |
| 129 |
| 130 net::HttpCache* NetworkController::GetCache() { |
| 131 return network_layer_->GetCache(); |
| 132 } |
| 133 |
| 134 net::HttpNetworkSession* NetworkController::GetSession() { |
| 135 return network_layer_->GetSession(); |
| 136 } |
OLD | NEW |