render_process_preferences.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #ifndef ATOM_BROWSER_RENDER_PROCESS_PREFERENCES_H_
  5. #define ATOM_BROWSER_RENDER_PROCESS_PREFERENCES_H_
  6. #include <memory>
  7. #include <unordered_map>
  8. #include "base/callback.h"
  9. #include "base/values.h"
  10. #include "content/public/browser/notification_observer.h"
  11. #include "content/public/browser/notification_registrar.h"
  12. namespace content {
  13. class RenderProcessHost;
  14. }
  15. namespace atom {
  16. // Sets user preferences for render processes.
  17. class RenderProcessPreferences : public content::NotificationObserver {
  18. public:
  19. using Predicate = base::Callback<bool(content::RenderProcessHost*)>;
  20. // The |predicate| is used to determine whether to set preferences for a
  21. // render process.
  22. explicit RenderProcessPreferences(const Predicate& predicate);
  23. ~RenderProcessPreferences() override;
  24. int AddEntry(const base::DictionaryValue& entry);
  25. void RemoveEntry(int id);
  26. private:
  27. // content::NotificationObserver:
  28. void Observe(int type,
  29. const content::NotificationSource& source,
  30. const content::NotificationDetails& details) override;
  31. void UpdateCache();
  32. // Manages our notification registrations.
  33. content::NotificationRegistrar registrar_;
  34. Predicate predicate_;
  35. int next_id_ = 0;
  36. std::unordered_map<int, std::unique_ptr<base::DictionaryValue>> entries_;
  37. // We need to convert the |entries_| to ListValue for multiple times, this
  38. // caches is only updated when we are sending messages.
  39. bool cache_needs_update_ = true;
  40. base::ListValue cached_entries_;
  41. DISALLOW_COPY_AND_ASSIGN(RenderProcessPreferences);
  42. };
  43. } // namespace atom
  44. #endif // ATOM_BROWSER_RENDER_PROCESS_PREFERENCES_H_