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

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: update gypi too 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
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 #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/stl_util.h"
12 #include "base/synchronization/lock.h"
13 #include "content/browser/renderer_host/render_process_host_impl.h"
14 #include "content/browser/renderer_host/render_view_host_impl.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/notification_types.h"
20
21 namespace content {
22
23 // RendererDataMemoizingStore is a thread-safe container that retains reference
24 // counted objects that are associated with one or more render processes.
25 // Objects
26 // are identified by an int and only a single instance of a given object is
wtc 2013/11/14 22:04:58 Nit: a single instance => a single reference?
alcutter 2013/11/15 13:33:07 Done.
27 // retained. RendererDataMemoizingStore watches for render process termination
28 // and
29 // releases objects that are no longer associated with any render process.
wtc 2013/11/14 22:04:58 The comment block should be reformatted. For examp
alcutter 2013/11/15 13:33:07 Not sure what happened there, done.
30 template <typename T>
31 class RendererDataMemoizingStore : public NotificationObserver {
wtc 2013/11/14 22:04:58 Is it better to define the class inline in the hea
agl 2013/11/14 22:09:28 I think, because it's a template, it must be in th
alcutter 2013/11/15 13:33:07 As Adams says, it has to be in the header because
32 private:
wtc 2013/11/14 22:04:58 The three "private" sections should be merged into
alcutter 2013/11/15 13:33:07 Done.
33 template <typename M>
34 struct MatchSecond {
35 explicit MatchSecond(const M& t) : value(t) {}
36
37 template <typename Pair>
38 bool operator()(const Pair& p) const {
39 return (value == p.second);
40 }
41 M value;
wtc 2013/11/14 22:04:58 Nit: add a blank line before this line. (I know yo
alcutter 2013/11/15 13:33:07 Done.
42 };
43
44 public:
45 RendererDataMemoizingStore() : next_item_id_(1) {
46 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
47 RegisterForNotification();
48 } else {
49 BrowserThread::PostTask(
50 BrowserThread::UI,
51 FROM_HERE,
52 base::Bind(&RendererDataMemoizingStore::RegisterForNotification,
53 base::Unretained(this)));
54 }
55 }
56
57 // Store adds |item| to this collection, associates it with the given render
58 // process id and returns an opaque identifier for it. If |item| is already
59 // known, the same identifier will be returned.
60 int Store(T* item, int process_id) {
61 DCHECK(item);
62 base::AutoLock auto_lock(lock_);
63
64 int item_id;
65
66 // Do we already know this item?
67 typename ReverseItemMap::iterator item_iter = item_to_id_.find(item);
wtc 2013/11/14 22:04:58 Is it necessary to use "typename" here? (There ar
alcutter 2013/11/15 13:33:07 Yes, because ReverseItemMap (==std::map<T*, int, T
68 if (item_iter == item_to_id_.end()) {
69 item_id = next_item_id_++;
70 // We use 0 as an invalid item_id value. In the unlikely event that
71 // next_item_id_ wraps around, we reset it to 1.
72 if (next_item_id_ == 0)
73 next_item_id_ = 1;
74 item->AddRef();
75 id_to_item_[item_id] = item;
76 item_to_id_[item] = item_id;
77 } else {
78 item_id = item_iter->second;
79 }
80
81 // Let's update process_id_to_item_id_.
82 std::pair<IDMap::iterator, IDMap::iterator> process_ids =
83 process_id_to_item_id_.equal_range(process_id);
84 if (std::find_if(process_ids.first,
85 process_ids.second,
wtc 2013/11/14 22:04:58 Nit: I would list the first two arguments on the s
alcutter 2013/11/15 13:33:07 Done.
86 MatchSecond<int>(item_id)) == process_ids.second) {
87 process_id_to_item_id_.insert(std::make_pair(process_id, item_id));
88 }
89
90 // And item_id_to_process_id_.
91 std::pair<IDMap::iterator, IDMap::iterator> item_ids =
92 item_id_to_process_id_.equal_range(item_id);
93 if (std::find_if(item_ids.first,
94 item_ids.second,
95 MatchSecond<int>(process_id)) == item_ids.second) {
96 item_id_to_process_id_.insert(std::make_pair(item_id, process_id));
97 }
98
99 return item_id;
100 }
101
102 // Retrieve fetches a previously Stored() item, identified by |item_id|.
103 // If |item_id| is recognized, |item| will be updated and Retrieve() will
104 // return true, it will otherwise return false.
105 bool Retrieve(int item_id, scoped_refptr<T>* item) {
106 base::AutoLock auto_lock(lock_);
107
108 typename ItemMap::iterator iter = id_to_item_.find(item_id);
109 if (iter == id_to_item_.end())
110 return false;
111 if (item)
112 *item = iter->second;
113 return true;
114 }
115
116 private:
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 void RemoveInternal(int item_id) {
wtc 2013/11/14 22:04:58 Please copy the original comment (after updating i
alcutter 2013/11/15 13:33:07 Whoops, done.
133 typename ItemMap::iterator item_iter = id_to_item_.find(item_id);
134 DCHECK(item_iter != id_to_item_.end());
135
136 typename ReverseItemMap::iterator id_iter =
137 item_to_id_.find(item_iter->second.get());
138 DCHECK(id_iter != item_to_id_.end());
139 item_to_id_.erase(id_iter);
140
141 item_iter->second->Release();
142 id_to_item_.erase(item_iter);
143 }
144
145 void RemoveForRenderProcessHost(int process_id) {
wtc 2013/11/14 22:04:58 Please copy the original comment (after updating i
alcutter 2013/11/15 13:33:07 Done.
146 base::AutoLock auto_lock(lock_);
147
148 // We iterate through all the item ids for that process.
149 std::pair<IDMap::iterator, IDMap::iterator> process_ids =
150 process_id_to_item_id_.equal_range(process_id);
151 for (IDMap::iterator ids_iter = process_ids.first;
152 ids_iter != process_ids.second;
153 ++ids_iter) {
154 int item_id = ids_iter->second;
155 // Find all the processes referring to this item id in
156 // item_id_to_process_id_, then locate the process being removed within
157 // that range.
158 std::pair<IDMap::iterator, IDMap::iterator> item_ids =
159 item_id_to_process_id_.equal_range(item_id);
160 IDMap::iterator proc_iter = std::find_if(
161 item_ids.first, item_ids.second, MatchSecond<int>(process_id));
162 DCHECK(proc_iter != item_ids.second);
163
164 // Before removing, determine if no other processes refer to the current
165 // item id. If |proc_iter| (the current process) is the lower bound of
166 // processes containing the current item id and if |next_proc_iter| is the
167 // upper bound (the first process that does not), then only one process,
168 // the one being removed, refers to the item id.
169 IDMap::iterator next_proc_iter = proc_iter;
170 ++next_proc_iter;
171 bool last_process_for_item_id =
172 (proc_iter == item_ids.first && next_proc_iter == item_ids.second);
173 item_id_to_process_id_.erase(proc_iter);
174
175 if (last_process_for_item_id) {
176 // The current item id is not referenced by any other processes, so
177 // remove it from id_to_item_ and item_to_id_.
178 RemoveInternal(item_id);
179 }
180 }
181 if (process_ids.first != process_ids.second)
182 process_id_to_item_id_.erase(process_ids.first, process_ids.second);
183 }
184
185 void Observe(int type,
wtc 2013/11/14 22:04:58 Add the comment // NotificationObserver implem
alcutter 2013/11/15 13:33:07 Done.
186 const NotificationSource& source,
187 const NotificationDetails& details) {
wtc 2013/11/14 22:04:58 Add "OVERRIDE" between ')' and '{'.
alcutter 2013/11/15 13:33:07 Done.
188 DCHECK(type == NOTIFICATION_RENDERER_PROCESS_TERMINATED ||
189 type == NOTIFICATION_RENDERER_PROCESS_CLOSED);
190 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr();
191 DCHECK(rph);
192 RemoveForRenderProcessHost(rph->GetID());
193 }
194
195 private:
196 typedef std::multimap<int, int> IDMap;
197 typedef std::map<int, scoped_refptr<T> > ItemMap;
198 typedef std::map<T*, int, typename T::LessThan> ReverseItemMap;
wtc 2013/11/14 22:04:58 I am not familiar with templates. Is it necessary
alcutter 2013/11/15 13:33:07 Yep, same story here (it's because someone could c
199
200 // Is only used on the UI Thread.
201 NotificationRegistrar registrar_;
202
203 IDMap process_id_to_item_id_;
204 IDMap item_id_to_process_id_;
205 ItemMap id_to_item_;
206 ReverseItemMap item_to_id_;
207
208 int next_item_id_;
209
210 // This lock protects: process_to_item_id_, item_id_to_process_id_,
wtc 2013/11/14 22:04:58 Typo: process_to_item_id_ => process_id_to_item_id
alcutter 2013/11/15 13:33:07 Good catch! Done.
211 // id_to_item_, and item_to_id_.
212 base::Lock lock_;
213 };
214
215 } // namespace content
216
217 #endif // CONTENT_BROWSER_RENDERER_DATA_MEMOIZING_STORE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698