Getting rid of raw pointers to SharedMemory in UserScriptMaster

BUG=

Review URL: https://codereview.chromium.org/282393002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@271288 0039d316-1c4b-4281-b951-d872f2087c98
parent 07ed1e38
......@@ -7,6 +7,7 @@
#include <string>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/version.h"
......@@ -166,12 +167,10 @@ void UserScriptMaster::ScriptReloader::StartLoad(
UserScriptMaster::ScriptReloader::~ScriptReloader() {}
void UserScriptMaster::ScriptReloader::NotifyMaster(
base::SharedMemory* memory) {
// The master went away, so these new scripts aren't useful anymore.
if (!master_)
delete memory;
else
master_->NewScriptsAvailable(memory);
scoped_ptr<base::SharedMemory> memory) {
// The master could go away
if (master_)
master_->NewScriptsAvailable(memory.Pass());
// Drop our self-reference.
// Balances StartLoad().
......@@ -284,7 +283,7 @@ SubstitutionMap* UserScriptMaster::ScriptReloader::GetLocalizationMessages(
}
// Pickle user scripts and return pointer to the shared memory.
static base::SharedMemory* Serialize(const UserScriptList& scripts) {
static scoped_ptr<base::SharedMemory> Serialize(const UserScriptList& scripts) {
Pickle pickle;
pickle.WriteUInt64(scripts.size());
for (size_t i = 0; i < scripts.size(); i++) {
......@@ -312,10 +311,10 @@ static base::SharedMemory* Serialize(const UserScriptList& scripts) {
options.size = pickle.size();
options.share_read_only = true;
if (!shared_memory.Create(options))
return NULL;
return scoped_ptr<base::SharedMemory>();
if (!shared_memory.Map(pickle.size()))
return NULL;
return scoped_ptr<base::SharedMemory>();
// Copy the pickle to shared memory.
memcpy(shared_memory.memory(), pickle.data(), pickle.size());
......@@ -323,9 +322,10 @@ static base::SharedMemory* Serialize(const UserScriptList& scripts) {
base::SharedMemoryHandle readonly_handle;
if (!shared_memory.ShareReadOnlyToProcess(base::GetCurrentProcessHandle(),
&readonly_handle))
return NULL;
return scoped_ptr<base::SharedMemory>();
return new base::SharedMemory(readonly_handle, /*read_only=*/true);
return make_scoped_ptr(new base::SharedMemory(readonly_handle,
/*read_only=*/true));
}
// This method will be called on the file thread.
......@@ -336,10 +336,11 @@ void UserScriptMaster::ScriptReloader::RunLoad(
// Scripts now contains list of up-to-date scripts. Load the content in the
// shared memory and let the master know it's ready. We need to post the task
// back even if no scripts ware found to balance the AddRef/Release calls.
BrowserThread::PostTask(
master_thread_id_, FROM_HERE,
base::Bind(
&ScriptReloader::NotifyMaster, this, Serialize(user_scripts)));
BrowserThread::PostTask(master_thread_id_,
FROM_HERE,
base::Bind(&ScriptReloader::NotifyMaster,
this,
base::Passed(Serialize(user_scripts))));
}
UserScriptMaster::UserScriptMaster(Profile* profile)
......@@ -359,10 +360,8 @@ UserScriptMaster::~UserScriptMaster() {
script_reloader_->DisownMaster();
}
void UserScriptMaster::NewScriptsAvailable(base::SharedMemory* handle) {
// Ensure handle is deleted or released.
scoped_ptr<base::SharedMemory> handle_deleter(handle);
void UserScriptMaster::NewScriptsAvailable(
scoped_ptr<base::SharedMemory> handle) {
if (pending_load_) {
// While we were loading, there were further changes. Don't bother
// notifying about these scripts and instead just immediately reload.
......@@ -385,18 +384,18 @@ void UserScriptMaster::NewScriptsAvailable(base::SharedMemory* handle) {
}
// We've got scripts ready to go.
shared_memory_.swap(handle_deleter);
shared_memory_ = handle.Pass();
for (content::RenderProcessHost::iterator i(
content::RenderProcessHost::AllHostsIterator());
!i.IsAtEnd(); i.Advance()) {
SendUpdate(i.GetCurrentValue(), handle);
SendUpdate(i.GetCurrentValue(), shared_memory_.get());
}
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_USER_SCRIPTS_UPDATED,
content::Source<Profile>(profile_),
content::Details<base::SharedMemory>(handle));
content::Details<base::SharedMemory>(shared_memory_.get()));
}
}
......
......@@ -51,7 +51,7 @@ class UserScriptMaster : public base::RefCountedThreadSafe<UserScriptMaster>,
}
// Called by the script reloader when new scripts have been loaded.
void NewScriptsAvailable(base::SharedMemory* handle);
void NewScriptsAvailable(scoped_ptr<base::SharedMemory> handle);
// Return true if we have any scripts ready.
bool ScriptsReady() const { return shared_memory_.get() != NULL; }
......@@ -105,7 +105,7 @@ class UserScriptMaster : public base::RefCountedThreadSafe<UserScriptMaster>,
// Runs on the master thread.
// Notify the master that new scripts are available.
void NotifyMaster(base::SharedMemory* memory);
void NotifyMaster(scoped_ptr<base::SharedMemory> memory);
// Runs on the File thread.
// Load the specified user scripts, calling NotifyMaster when done.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment