Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(246)

Side by Side Diff: content/browser/renderer_data_memoizing_store.h

Issue 71153005: Pull implementation of CertStore out to be reused. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes for wtc. Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/cert_store_impl.cc ('k') | content/content_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
Avi (use Gerrit) 2013/11/19 16:38:05 No (c) in new files, just "Copyright 2013..."
alcutter 2013/11/19 16:45:10 Done.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CONTENT_BROWSER_RENDERER_DATA_MEMOIZING_STORE_H_
6 #define CONTENT_BROWSER_RENDERER_DATA_MEMOIZING_STORE_H_
7
8 #include <map>
9
10 #include "base/bind.h"
11 #include "base/synchronization/lock.h"
12 #include "content/browser/renderer_host/render_process_host_impl.h"
13 #include "content/browser/renderer_host/render_view_host_impl.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "content/public/browser/notification_service.h"
18 #include "content/public/browser/notification_types.h"
19
20 namespace content {
21
22 // RendererDataMemoizingStore is a thread-safe container that retains reference
23 // counted objects that are associated with one or more render processes.
24 // Objects are identified by an int and only a single reference to a given
25 // object is retained. RendererDataMemoizingStore watches for render process
26 // termination and releases objects that are no longer associated with any
27 // render process.
28 template <typename T>
29 class RendererDataMemoizingStore : public NotificationObserver {
30 public:
31 RendererDataMemoizingStore() : next_item_id_(1) {
32 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
33 RegisterForNotification();
34 } else {
35 BrowserThread::PostTask(
36 BrowserThread::UI,
37 FROM_HERE,
38 base::Bind(&RendererDataMemoizingStore::RegisterForNotification,
39 base::Unretained(this)));
40 }
41 }
42
43 // Store adds |item| to this collection, associates it with the given render
44 // process id and returns an opaque identifier for it. If |item| is already
45 // known, the same identifier will be returned.
46 int Store(T* item, int process_id) {
47 DCHECK(item);
48 base::AutoLock auto_lock(lock_);
49
50 int item_id;
51
52 // Do we already know this item?
53 typename ReverseItemMap::iterator item_iter = item_to_id_.find(item);
54 if (item_iter == item_to_id_.end()) {
55 item_id = next_item_id_++;
56 // We use 0 as an invalid item_id value. In the unlikely event that
57 // next_item_id_ wraps around, we reset it to 1.
58 if (next_item_id_ == 0)
59 next_item_id_ = 1;
60 item->AddRef();
61 id_to_item_[item_id] = item;
62 item_to_id_[item] = item_id;
63 } else {
64 item_id = item_iter->second;
65 }
66
67 // Let's update process_id_to_item_id_.
68 std::pair<IDMap::iterator, IDMap::iterator> process_ids =
69 process_id_to_item_id_.equal_range(process_id);
70 if (std::find_if(process_ids.first, process_ids.second,
71 MatchSecond<int>(item_id)) == process_ids.second) {
72 process_id_to_item_id_.insert(std::make_pair(process_id, item_id));
73 }
74
75 // And item_id_to_process_id_.
76 std::pair<IDMap::iterator, IDMap::iterator> item_ids =
77 item_id_to_process_id_.equal_range(item_id);
78 if (std::find_if(item_ids.first, item_ids.second,
79 MatchSecond<int>(process_id)) == item_ids.second) {
80 item_id_to_process_id_.insert(std::make_pair(item_id, process_id));
81 }
82
83 return item_id;
84 }
85
86 // Retrieve fetches a previously Stored() item, identified by |item_id|.
87 // If |item_id| is recognized, |item| will be updated and Retrieve() will
88 // return true, it will otherwise return false.
89 bool Retrieve(int item_id, scoped_refptr<T>* item) {
90 base::AutoLock auto_lock(lock_);
91
92 typename ItemMap::iterator iter = id_to_item_.find(item_id);
93 if (iter == id_to_item_.end())
94 return false;
95 if (item)
96 *item = iter->second;
97 return true;
98 }
99
100 private:
101 typedef std::multimap<int, int> IDMap;
102 typedef std::map<int, scoped_refptr<T> > ItemMap;
103 typedef std::map<T*, int, typename T::LessThan> ReverseItemMap;
104
105 template <typename M>
106 struct MatchSecond {
107 explicit MatchSecond(const M& t) : value(t) {}
108
109 template <typename Pair>
110 bool operator()(const Pair& p) const {
111 return (value == p.second);
112 }
113
114 M value;
115 };
116
117 void RegisterForNotification() {
118 // We watch for RenderProcess termination, as this is how we clear
119 // items for now.
120 // TODO(jcampan): we should be listening to events such as resource cached/
121 // removed from cache, and remove the item when we know it
122 // is not used anymore.
123
124 registrar_.Add(this,
125 NOTIFICATION_RENDERER_PROCESS_TERMINATED,
126 NotificationService::AllBrowserContextsAndSources());
127 registrar_.Add(this,
128 NOTIFICATION_RENDERER_PROCESS_CLOSED,
129 NotificationService::AllBrowserContextsAndSources());
130 }
131
132 // Remove the item specified by |item_id| from id_to_item and item_to_id_.
133 // NOTE: the caller (RemoveForRenderProcesHost) must hold lock_.
134 void RemoveInternal(int item_id) {
135 typename ItemMap::iterator item_iter = id_to_item_.find(item_id);
136 DCHECK(item_iter != id_to_item_.end());
137
138 typename ReverseItemMap::iterator id_iter =
139 item_to_id_.find(item_iter->second.get());
140 DCHECK(id_iter != item_to_id_.end());
141 item_to_id_.erase(id_iter);
142
143 item_iter->second->Release();
144 id_to_item_.erase(item_iter);
145 }
146
147 // Removes all the items associated with the specified process from the store.
148 void RemoveForRenderProcessHost(int process_id) {
149 base::AutoLock auto_lock(lock_);
150
151 // We iterate through all the item ids for that process.
152 std::pair<IDMap::iterator, IDMap::iterator> process_ids =
153 process_id_to_item_id_.equal_range(process_id);
154 for (IDMap::iterator ids_iter = process_ids.first;
155 ids_iter != process_ids.second; ++ids_iter) {
156 int item_id = ids_iter->second;
157 // Find all the processes referring to this item id in
158 // item_id_to_process_id_, then locate the process being removed within
159 // that range.
160 std::pair<IDMap::iterator, IDMap::iterator> item_ids =
161 item_id_to_process_id_.equal_range(item_id);
162 IDMap::iterator proc_iter = std::find_if(
163 item_ids.first, item_ids.second, MatchSecond<int>(process_id));
164 DCHECK(proc_iter != item_ids.second);
165
166 // Before removing, determine if no other processes refer to the current
167 // item id. If |proc_iter| (the current process) is the lower bound of
168 // processes containing the current item id and if |next_proc_iter| is the
169 // upper bound (the first process that does not), then only one process,
170 // the one being removed, refers to the item id.
171 IDMap::iterator next_proc_iter = proc_iter;
172 ++next_proc_iter;
173 bool last_process_for_item_id =
174 (proc_iter == item_ids.first && next_proc_iter == item_ids.second);
175 item_id_to_process_id_.erase(proc_iter);
176
177 if (last_process_for_item_id) {
178 // The current item id is not referenced by any other processes, so
179 // remove it from id_to_item_ and item_to_id_.
180 RemoveInternal(item_id);
181 }
182 }
183 if (process_ids.first != process_ids.second)
184 process_id_to_item_id_.erase(process_ids.first, process_ids.second);
185 }
186
187 // NotificationObserver implementation.
188 void Observe(int type,
189 const NotificationSource& source,
190 const NotificationDetails& details) OVERRIDE {
191 DCHECK(type == NOTIFICATION_RENDERER_PROCESS_TERMINATED ||
192 type == NOTIFICATION_RENDERER_PROCESS_CLOSED);
193 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr();
194 DCHECK(rph);
195 RemoveForRenderProcessHost(rph->GetID());
196 }
197
198 // Is only used on the UI Thread.
199 NotificationRegistrar registrar_;
200
201 IDMap process_id_to_item_id_;
202 IDMap item_id_to_process_id_;
203 ItemMap id_to_item_;
204 ReverseItemMap item_to_id_;
205
206 int next_item_id_;
207
208 // This lock protects: process_id_to_item_id_, item_id_to_process_id_,
209 // id_to_item_, and item_to_id_.
210 base::Lock lock_;
211 };
212
213 } // namespace content
214
215 #endif // CONTENT_BROWSER_RENDERER_DATA_MEMOIZING_STORE_H_
OLDNEW
« no previous file with comments | « content/browser/cert_store_impl.cc ('k') | content/content_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698