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