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 |
| 13 NetworkController::NetworkController() { |
| 14 } |
| 15 |
| 16 NetworkController::~NetworkController() { |
| 17 } |
| 18 |
| 19 void NetworkController::AddTransaction( |
| 20 uint64 transaction_id, |
| 21 NetworkTransaction* transaction) { |
| 22 transactions_[transaction_id] = transaction; |
| 23 } |
| 24 |
| 25 void NetworkController::RemoveTransaction(uint64 transaction_id) { |
| 26 transactions_.erase(transaction_id); |
| 27 } |
| 28 |
| 29 // static |
| 30 void NetworkController::DisableNetwork( |
| 31 Profile* profile, |
| 32 const std::string& client_id, |
| 33 bool disable_network) { |
| 34 CHECK(profile); |
| 35 content::ResourceContext* resourceContext = profile->GetResourceContext(); |
| 36 CHECK(resourceContext); |
| 37 content::BrowserThread::PostTask( |
| 38 content::BrowserThread::IO, |
| 39 FROM_HERE, |
| 40 base::Bind( |
| 41 &NetworkController::DisableNetworkOnIO, |
| 42 resourceContext, |
| 43 client_id, |
| 44 disable_network)); |
| 45 } |
| 46 |
| 47 // static |
| 48 void NetworkController::DisableNetworkOnIO( |
| 49 content::ResourceContext* resourceContext, |
| 50 const std::string& client_id, |
| 51 bool disable_network) { |
| 52 CHECK(resourceContext); |
| 53 NetworkController* controller = ProfileIOData::FromResourceContext( |
| 54 resourceContext)->network_controller(); |
| 55 CHECK(controller); |
| 56 controller->DisableNetwork(client_id, disable_network); |
| 57 } |
| 58 |
| 59 void NetworkController::DisableNetwork( |
| 60 const std::string& client_id, |
| 61 bool disable_network) { |
| 62 ClientsSet::iterator client = clients_set_.find(client_id); |
| 63 if (client != clients_set_.end()) { |
| 64 if (!disable_network) |
| 65 clients_set_.erase(client); |
| 66 return; |
| 67 } |
| 68 |
| 69 if (!disable_network) |
| 70 return; |
| 71 |
| 72 bool first_client = clients_set_.size() == 0; |
| 73 clients_set_.insert(client_id); |
| 74 if (!first_client) |
| 75 return; |
| 76 |
| 77 Transactions::iterator it = transactions_.begin(); |
| 78 while (it != transactions_.end()) { |
| 79 Transactions::iterator current = it++; |
| 80 NetworkTransaction* transaction = current->second; |
| 81 if (IsBlockedURL(transaction->GetURL())) { |
| 82 transactions_.erase(current); |
| 83 transaction->Stop(); |
| 84 } |
| 85 } |
| 86 } |
| 87 |
| 88 bool NetworkController::IsBlockedURL(const GURL& url) { |
| 89 if (clients_set_.size() == 0) |
| 90 return false; |
| 91 |
| 92 // TODO: Add whitelist. |
| 93 |
| 94 return true; |
| 95 } |
OLD | NEW |