| 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 "content/browser/service_worker/service_worker_provider_host_registry.h
" |
| 6 |
| 7 #include "content/browser/service_worker/service_worker_provider_host.h" |
| 8 #include "content/public/browser/browser_thread.h" |
| 9 |
| 10 namespace content { |
| 11 |
| 12 ServiceWorkerProviderHostRegistry::Iterator::~Iterator() {} |
| 13 |
| 14 ServiceWorkerProviderHost* |
| 15 ServiceWorkerProviderHostRegistry::Iterator::GetProviderHost() { |
| 16 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 17 DCHECK(!IsAtEnd()); |
| 18 return provider_host_iterator_->GetCurrentValue(); |
| 19 } |
| 20 |
| 21 void ServiceWorkerProviderHostRegistry::Iterator::Advance() { |
| 22 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 23 DCHECK(!IsAtEnd()); |
| 24 DCHECK(!provider_host_iterator_->IsAtEnd()); |
| 25 DCHECK(!provider_iterator_->IsAtEnd()); |
| 26 |
| 27 // Advance the inner iterator. If an element is reached, we're done. |
| 28 provider_host_iterator_->Advance(); |
| 29 if (!provider_host_iterator_->IsAtEnd()) |
| 30 return; |
| 31 |
| 32 // Advance the outer iterator until an element is reached, or end is hit. |
| 33 while (true) { |
| 34 provider_iterator_->Advance(); |
| 35 if (provider_iterator_->IsAtEnd()) |
| 36 return; |
| 37 ProviderMap* provider_map = provider_iterator_->GetCurrentValue(); |
| 38 provider_host_iterator_.reset(new ProviderMap::iterator(provider_map)); |
| 39 if (!provider_host_iterator_->IsAtEnd()) |
| 40 return; |
| 41 } |
| 42 } |
| 43 |
| 44 bool ServiceWorkerProviderHostRegistry::Iterator::IsAtEnd() { |
| 45 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 46 return provider_iterator_->IsAtEnd() && |
| 47 (!provider_host_iterator_ || provider_host_iterator_->IsAtEnd()); |
| 48 } |
| 49 |
| 50 ServiceWorkerProviderHostRegistry::Iterator::Iterator( |
| 51 ProcessToProviderMap* map) |
| 52 : map_(map) { |
| 53 DCHECK(map); |
| 54 Initialize(); |
| 55 } |
| 56 |
| 57 void ServiceWorkerProviderHostRegistry::Iterator::Initialize() { |
| 58 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 59 provider_iterator_.reset(new ProcessToProviderMap::iterator(map_)); |
| 60 // Advance to the first element. |
| 61 while (!provider_iterator_->IsAtEnd()) { |
| 62 ProviderMap* provider_map = provider_iterator_->GetCurrentValue(); |
| 63 provider_host_iterator_.reset(new ProviderMap::iterator(provider_map)); |
| 64 if (!provider_host_iterator_->IsAtEnd()) |
| 65 return; |
| 66 provider_iterator_->Advance(); |
| 67 } |
| 68 } |
| 69 |
| 70 ServiceWorkerProviderHostRegistry::ServiceWorkerProviderHostRegistry() { |
| 71 } |
| 72 |
| 73 ServiceWorkerProviderHostRegistry::~ServiceWorkerProviderHostRegistry() { |
| 74 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 75 } |
| 76 |
| 77 ServiceWorkerProviderHost* ServiceWorkerProviderHostRegistry::GetProviderHost( |
| 78 int process_id, int provider_id) { |
| 79 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 80 ProviderMap* map = GetProviderMapForProcess(process_id); |
| 81 if (!map) |
| 82 return NULL; |
| 83 return map->Lookup(provider_id); |
| 84 } |
| 85 |
| 86 void ServiceWorkerProviderHostRegistry::AddProviderHost( |
| 87 scoped_ptr<ServiceWorkerProviderHost> host) { |
| 88 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 89 ServiceWorkerProviderHost* host_ptr = host.release(); // we take ownership |
| 90 ProviderMap* map = GetProviderMapForProcess(host_ptr->process_id()); |
| 91 if (!map) { |
| 92 map = new ProviderMap; |
| 93 providers_.AddWithID(map, host_ptr->process_id()); |
| 94 } |
| 95 map->AddWithID(host_ptr, host_ptr->provider_id()); |
| 96 } |
| 97 |
| 98 void ServiceWorkerProviderHostRegistry::RemoveProviderHost( |
| 99 int process_id, int provider_id) { |
| 100 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 101 ProviderMap* map = GetProviderMapForProcess(process_id); |
| 102 DCHECK(map); |
| 103 map->Remove(provider_id); |
| 104 } |
| 105 |
| 106 void ServiceWorkerProviderHostRegistry::RemoveAllProviderHostsForProcess( |
| 107 int process_id) { |
| 108 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 109 if (providers_.Lookup(process_id)) |
| 110 providers_.Remove(process_id); |
| 111 } |
| 112 |
| 113 scoped_ptr<ServiceWorkerProviderHostRegistry::Iterator> |
| 114 ServiceWorkerProviderHostRegistry::GetProviderHostIterator() { |
| 115 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 116 return make_scoped_ptr(new Iterator(&providers_)); |
| 117 } |
| 118 |
| 119 } // namespace content |
| OLD | NEW |