| 
									
										
										
										
											2016-05-27 08:51:02 +09:00
										 |  |  | // Copyright (c) 2016 GitHub, Inc.
 | 
					
						
							|  |  |  | // Use of this source code is governed by the MIT license that can be
 | 
					
						
							|  |  |  | // found in the LICENSE file.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "atom/browser/render_process_preferences.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "atom/common/api/api_messages.h"
 | 
					
						
							|  |  |  | #include "content/public/browser/notification_service.h"
 | 
					
						
							|  |  |  | #include "content/public/browser/notification_types.h"
 | 
					
						
							|  |  |  | #include "content/public/browser/render_process_host.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace atom { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RenderProcessPreferences::RenderProcessPreferences(const Predicate& predicate) | 
					
						
							| 
									
										
										
										
											2018-05-22 00:18:38 +02:00
										 |  |  |     : predicate_(predicate) { | 
					
						
							| 
									
										
										
										
											2018-04-17 21:55:30 -04:00
										 |  |  |   registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 
					
						
							| 
									
										
										
										
											2016-05-27 08:51:02 +09:00
										 |  |  |                  content::NotificationService::AllBrowserContextsAndSources()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-17 21:55:30 -04:00
										 |  |  | RenderProcessPreferences::~RenderProcessPreferences() {} | 
					
						
							| 
									
										
										
										
											2016-05-27 08:51:02 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | int RenderProcessPreferences::AddEntry(const base::DictionaryValue& entry) { | 
					
						
							|  |  |  |   int id = ++next_id_; | 
					
						
							| 
									
										
										
										
											2018-08-03 14:23:07 -07:00
										 |  |  |   entries_[id] = | 
					
						
							|  |  |  |       base::DictionaryValue::From(base::Value::ToUniquePtrValue(entry.Clone())); | 
					
						
							| 
									
										
										
										
											2016-05-27 08:51:02 +09:00
										 |  |  |   cache_needs_update_ = true; | 
					
						
							|  |  |  |   return id; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RenderProcessPreferences::RemoveEntry(int id) { | 
					
						
							|  |  |  |   cache_needs_update_ = true; | 
					
						
							|  |  |  |   entries_.erase(id); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RenderProcessPreferences::Observe( | 
					
						
							|  |  |  |     int type, | 
					
						
							|  |  |  |     const content::NotificationSource& source, | 
					
						
							|  |  |  |     const content::NotificationDetails& details) { | 
					
						
							|  |  |  |   DCHECK_EQ(type, content::NOTIFICATION_RENDERER_PROCESS_CREATED); | 
					
						
							|  |  |  |   content::RenderProcessHost* process = | 
					
						
							|  |  |  |       content::Source<content::RenderProcessHost>(source).ptr(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!predicate_.Run(process)) | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   UpdateCache(); | 
					
						
							|  |  |  |   process->Send(new AtomMsg_UpdatePreferences(cached_entries_)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RenderProcessPreferences::UpdateCache() { | 
					
						
							|  |  |  |   if (!cache_needs_update_) | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   cached_entries_.Clear(); | 
					
						
							|  |  |  |   for (const auto& iter : entries_) | 
					
						
							| 
									
										
										
										
											2018-09-03 00:38:35 +02:00
										 |  |  |     cached_entries_.Append(base::Value::ToUniquePtrValue(iter.second->Clone())); | 
					
						
							| 
									
										
										
										
											2016-05-27 08:51:02 +09:00
										 |  |  |   cache_needs_update_ = false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace atom
 |