atom_api_render_process_preferences.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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/api/atom_api_render_process_preferences.h"
  5. #include "atom/browser/api/atom_api_web_contents.h"
  6. #include "atom/browser/atom_browser_client.h"
  7. #include "atom/common/native_mate_converters/value_converter.h"
  8. #include "content/public/browser/render_process_host.h"
  9. #include "native_mate/dictionary.h"
  10. #include "native_mate/object_template_builder.h"
  11. #include "atom/common/node_includes.h"
  12. namespace atom {
  13. namespace api {
  14. namespace {
  15. bool IsWebContents(v8::Isolate* isolate, content::RenderProcessHost* process) {
  16. content::WebContents* web_contents =
  17. static_cast<AtomBrowserClient*>(AtomBrowserClient::Get())
  18. ->GetWebContentsFromProcessID(process->GetID());
  19. if (!web_contents)
  20. return false;
  21. auto api_web_contents = WebContents::CreateFrom(isolate, web_contents);
  22. auto type = api_web_contents->GetType();
  23. return type == WebContents::Type::BROWSER_WINDOW ||
  24. type == WebContents::Type::WEB_VIEW;
  25. }
  26. } // namespace
  27. RenderProcessPreferences::RenderProcessPreferences(
  28. v8::Isolate* isolate,
  29. const atom::RenderProcessPreferences::Predicate& predicate)
  30. : preferences_(predicate) {
  31. Init(isolate);
  32. }
  33. RenderProcessPreferences::~RenderProcessPreferences() {}
  34. int RenderProcessPreferences::AddEntry(const base::DictionaryValue& entry) {
  35. return preferences_.AddEntry(entry);
  36. }
  37. void RenderProcessPreferences::RemoveEntry(int id) {
  38. preferences_.RemoveEntry(id);
  39. }
  40. // static
  41. void RenderProcessPreferences::BuildPrototype(
  42. v8::Isolate* isolate,
  43. v8::Local<v8::FunctionTemplate> prototype) {
  44. prototype->SetClassName(
  45. mate::StringToV8(isolate, "RenderProcessPreferences"));
  46. mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
  47. .SetMethod("addEntry", &RenderProcessPreferences::AddEntry)
  48. .SetMethod("removeEntry", &RenderProcessPreferences::RemoveEntry);
  49. }
  50. // static
  51. mate::Handle<RenderProcessPreferences>
  52. RenderProcessPreferences::ForAllWebContents(v8::Isolate* isolate) {
  53. return mate::CreateHandle(isolate,
  54. new RenderProcessPreferences(
  55. isolate, base::Bind(&IsWebContents, isolate)));
  56. }
  57. } // namespace api
  58. } // namespace atom
  59. namespace {
  60. void Initialize(v8::Local<v8::Object> exports,
  61. v8::Local<v8::Value> unused,
  62. v8::Local<v8::Context> context,
  63. void* priv) {
  64. mate::Dictionary dict(context->GetIsolate(), exports);
  65. dict.SetMethod("forAllWebContents",
  66. &atom::api::RenderProcessPreferences::ForAllWebContents);
  67. }
  68. } // namespace
  69. NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_browser_render_process_preferences,
  70. Initialize)