Add API to set render process preferences
This commit is contained in:
parent
b646d7a55c
commit
4fb9e20c33
11 changed files with 347 additions and 0 deletions
74
atom/browser/api/atom_api_render_process_preferences.cc
Normal file
74
atom/browser/api/atom_api_render_process_preferences.cc
Normal file
|
@ -0,0 +1,74 @@
|
|||
// 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/api/atom_api_render_process_preferences.h"
|
||||
|
||||
#include "atom/common/native_mate_converters/value_converter.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "native_mate/object_template_builder.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
namespace {
|
||||
|
||||
bool IsBrowserWindow(content::RenderProcessHost* process) {
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
RenderProcessPreferences::RenderProcessPreferences(
|
||||
v8::Isolate* isolate,
|
||||
const atom::RenderProcessPreferences::Predicate& predicate)
|
||||
: preferences_(predicate) {
|
||||
Init(isolate);
|
||||
}
|
||||
|
||||
RenderProcessPreferences::~RenderProcessPreferences() {
|
||||
}
|
||||
|
||||
int RenderProcessPreferences::AddEntry(const base::DictionaryValue& entry) {
|
||||
return preferences_.AddEntry(entry);
|
||||
}
|
||||
|
||||
void RenderProcessPreferences::RemoveEntry(int id) {
|
||||
preferences_.RemoveEntry(id);
|
||||
}
|
||||
|
||||
// static
|
||||
void RenderProcessPreferences::BuildPrototype(
|
||||
v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> prototype) {
|
||||
mate::ObjectTemplateBuilder(isolate, prototype)
|
||||
.SetMethod("addEntry", &RenderProcessPreferences::AddEntry)
|
||||
.SetMethod("removeEntry", &RenderProcessPreferences::RemoveEntry);
|
||||
}
|
||||
|
||||
// static
|
||||
mate::Handle<RenderProcessPreferences>
|
||||
RenderProcessPreferences::ForAllBrowserWindow(v8::Isolate* isolate) {
|
||||
return mate::CreateHandle(
|
||||
isolate,
|
||||
new RenderProcessPreferences(isolate, base::Bind(&IsBrowserWindow)));
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
namespace {
|
||||
|
||||
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
||||
v8::Local<v8::Context> context, void* priv) {
|
||||
mate::Dictionary dict(context->GetIsolate(), exports);
|
||||
dict.SetMethod("forAllBrowserWindow",
|
||||
&atom::api::RenderProcessPreferences::ForAllBrowserWindow);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_render_process_preferences,
|
||||
Initialize)
|
44
atom/browser/api/atom_api_render_process_preferences.h
Normal file
44
atom/browser/api/atom_api_render_process_preferences.h
Normal file
|
@ -0,0 +1,44 @@
|
|||
// Copyright (c) 2016 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ATOM_BROWSER_API_ATOM_API_RENDER_PROCESS_PREFERENCES_H_
|
||||
#define ATOM_BROWSER_API_ATOM_API_RENDER_PROCESS_PREFERENCES_H_
|
||||
|
||||
#include "atom/browser/render_process_preferences.h"
|
||||
#include "native_mate/handle.h"
|
||||
#include "native_mate/wrappable.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
class RenderProcessPreferences
|
||||
: public mate::Wrappable<RenderProcessPreferences> {
|
||||
public:
|
||||
static mate::Handle<RenderProcessPreferences>
|
||||
ForAllBrowserWindow(v8::Isolate* isolate);
|
||||
|
||||
static void BuildPrototype(v8::Isolate* isolate,
|
||||
v8::Local<v8::ObjectTemplate> prototype);
|
||||
|
||||
int AddEntry(const base::DictionaryValue& entry);
|
||||
void RemoveEntry(int id);
|
||||
|
||||
protected:
|
||||
RenderProcessPreferences(
|
||||
v8::Isolate* isolate,
|
||||
const atom::RenderProcessPreferences::Predicate& predicate);
|
||||
~RenderProcessPreferences() override;
|
||||
|
||||
private:
|
||||
atom::RenderProcessPreferences preferences_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(RenderProcessPreferences);
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_BROWSER_API_ATOM_API_RENDER_PROCESS_PREFERENCES_H_
|
63
atom/browser/render_process_preferences.cc
Normal file
63
atom/browser/render_process_preferences.cc
Normal file
|
@ -0,0 +1,63 @@
|
|||
// 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)
|
||||
: predicate_(predicate),
|
||||
next_id_(0),
|
||||
cache_needs_update_(true) {
|
||||
registrar_.Add(this,
|
||||
content::NOTIFICATION_RENDERER_PROCESS_CREATED,
|
||||
content::NotificationService::AllBrowserContextsAndSources());
|
||||
}
|
||||
|
||||
RenderProcessPreferences::~RenderProcessPreferences() {
|
||||
}
|
||||
|
||||
int RenderProcessPreferences::AddEntry(const base::DictionaryValue& entry) {
|
||||
int id = ++next_id_;
|
||||
entries_[id] = entry.CreateDeepCopy();
|
||||
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_)
|
||||
cached_entries_.Append(iter.second->CreateDeepCopy());
|
||||
cache_needs_update_ = false;
|
||||
}
|
||||
|
||||
} // namespace atom
|
61
atom/browser/render_process_preferences.h
Normal file
61
atom/browser/render_process_preferences.h
Normal file
|
@ -0,0 +1,61 @@
|
|||
// Copyright (c) 2016 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ATOM_BROWSER_RENDER_PROCESS_PREFERENCES_H_
|
||||
#define ATOM_BROWSER_RENDER_PROCESS_PREFERENCES_H_
|
||||
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "base/callback.h"
|
||||
#include "base/values.h"
|
||||
#include "content/public/browser/notification_observer.h"
|
||||
#include "content/public/browser/notification_registrar.h"
|
||||
|
||||
namespace content {
|
||||
class RenderProcessHost;
|
||||
}
|
||||
|
||||
namespace atom {
|
||||
|
||||
// Sets user preferences for render processes.
|
||||
class RenderProcessPreferences : public content::NotificationObserver {
|
||||
public:
|
||||
using Predicate = base::Callback<bool(content::RenderProcessHost*)>;
|
||||
|
||||
// The |predicate| is used to determine whether to set preferences for a
|
||||
// render process.
|
||||
explicit RenderProcessPreferences(const Predicate& predicate);
|
||||
virtual ~RenderProcessPreferences();
|
||||
|
||||
int AddEntry(const base::DictionaryValue& entry);
|
||||
void RemoveEntry(int id);
|
||||
|
||||
private:
|
||||
// content::NotificationObserver:
|
||||
void Observe(int type,
|
||||
const content::NotificationSource& source,
|
||||
const content::NotificationDetails& details) override;
|
||||
|
||||
void UpdateCache();
|
||||
|
||||
// Manages our notification registrations.
|
||||
content::NotificationRegistrar registrar_;
|
||||
|
||||
Predicate predicate_;
|
||||
|
||||
int next_id_;
|
||||
std::unordered_map<int, std::unique_ptr<base::DictionaryValue>> entries_;
|
||||
|
||||
// We need to convert the |entries_| to ListValue for multiple times, this
|
||||
// caches is only updated when we are sending messages.
|
||||
bool cache_needs_update_;
|
||||
base::ListValue cached_entries_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(RenderProcessPreferences);
|
||||
};
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_BROWSER_RENDER_PROCESS_PREFERENCES_H_
|
Loading…
Add table
Add a link
Reference in a new issue