render_process_preferences.cc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright (c) 2016 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/render_process_preferences.h"
  5. #include "atom/common/api/api_messages.h"
  6. #include "content/public/browser/notification_service.h"
  7. #include "content/public/browser/notification_types.h"
  8. #include "content/public/browser/render_process_host.h"
  9. namespace atom {
  10. RenderProcessPreferences::RenderProcessPreferences(const Predicate& predicate)
  11. : predicate_(predicate) {
  12. registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
  13. content::NotificationService::AllBrowserContextsAndSources());
  14. }
  15. RenderProcessPreferences::~RenderProcessPreferences() {}
  16. int RenderProcessPreferences::AddEntry(const base::DictionaryValue& entry) {
  17. int id = ++next_id_;
  18. entries_[id] = entry.CreateDeepCopy();
  19. cache_needs_update_ = true;
  20. return id;
  21. }
  22. void RenderProcessPreferences::RemoveEntry(int id) {
  23. cache_needs_update_ = true;
  24. entries_.erase(id);
  25. }
  26. void RenderProcessPreferences::Observe(
  27. int type,
  28. const content::NotificationSource& source,
  29. const content::NotificationDetails& details) {
  30. DCHECK_EQ(type, content::NOTIFICATION_RENDERER_PROCESS_CREATED);
  31. content::RenderProcessHost* process =
  32. content::Source<content::RenderProcessHost>(source).ptr();
  33. if (!predicate_.Run(process))
  34. return;
  35. UpdateCache();
  36. process->Send(new AtomMsg_UpdatePreferences(cached_entries_));
  37. }
  38. void RenderProcessPreferences::UpdateCache() {
  39. if (!cache_needs_update_)
  40. return;
  41. cached_entries_.Clear();
  42. for (const auto& iter : entries_)
  43. cached_entries_.Append(iter.second->CreateDeepCopy());
  44. cache_needs_update_ = false;
  45. }
  46. } // namespace atom