OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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/chrome_plugin_browsing_context.h" | |
6 | |
7 #include "base/message_loop.h" | |
8 #include "base/singleton.h" | |
9 #include "content/browser/browser_thread.h" | |
10 #include "content/common/notification_service.h" | |
11 | |
12 CPBrowsingContextManager* CPBrowsingContextManager::GetInstance() { | |
13 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
14 return Singleton<CPBrowsingContextManager>::get(); | |
15 } | |
16 | |
17 CPBrowsingContextManager::CPBrowsingContextManager() { | |
18 registrar_.Add(this, NotificationType::URL_REQUEST_CONTEXT_RELEASED, | |
19 NotificationService::AllSources()); | |
20 } | |
21 | |
22 CPBrowsingContextManager::~CPBrowsingContextManager() { | |
23 } | |
24 | |
25 CPBrowsingContext CPBrowsingContextManager::Allocate( | |
26 net::URLRequestContext* context) { | |
27 int32 map_id = map_.Add(context); | |
28 return static_cast<CPBrowsingContext>(map_id); | |
29 } | |
30 | |
31 net::URLRequestContext* CPBrowsingContextManager::ToURLRequestContext( | |
32 CPBrowsingContext id) { | |
33 return map_.Lookup(static_cast<int32>(id)); | |
34 } | |
35 | |
36 CPBrowsingContext CPBrowsingContextManager::Lookup( | |
37 net::URLRequestContext* context) { | |
38 ReverseMap::const_iterator it = reverse_map_.find(context); | |
39 if (it == reverse_map_.end()) { | |
40 CPBrowsingContext id = Allocate(context); | |
41 reverse_map_[context] = id; | |
42 return id; | |
43 } else { | |
44 return it->second; | |
45 } | |
46 } | |
47 | |
48 void CPBrowsingContextManager::Observe(NotificationType type, | |
49 const NotificationSource& source, | |
50 const NotificationDetails& details) { | |
51 DCHECK(type == NotificationType::URL_REQUEST_CONTEXT_RELEASED); | |
52 | |
53 net::URLRequestContext* context = | |
54 Source<net::URLRequestContext>(source).ptr(); | |
55 | |
56 // Multiple CPBrowsingContexts may refer to the same net::URLRequestContext. | |
57 for (Map::iterator it(&map_); !it.IsAtEnd(); it.Advance()) { | |
58 if (it.GetCurrentValue() == context) | |
59 map_.Remove(it.GetCurrentKey()); | |
60 } | |
61 | |
62 reverse_map_.erase(context); | |
63 } | |
OLD | NEW |