diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-09 20:37:35 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-09 20:37:35 +0000 |
commit | 0938d3cb82dfd2424117f73573e757424ebede00 (patch) | |
tree | f1d0503af86c6c8bf6be409c1744437384d7cb86 /chrome/browser/extensions | |
parent | 4f2321dbb2e034601fed172d222a90009e4674a0 (diff) | |
download | chromium_src-0938d3cb82dfd2424117f73573e757424ebede00.zip chromium_src-0938d3cb82dfd2424117f73573e757424ebede00.tar.gz chromium_src-0938d3cb82dfd2424117f73573e757424ebede00.tar.bz2 |
This is a rename of the term 'Greasemonkey' to 'user script' in Chromium.
I'm doing this to avoid confusion with the Firefox version of Greasemonkey and
also because 'user script' is really the correct generic term.
At the same time, I also moved user_script_master* into extensions/ because I want these two pieces to get closer and closer such that standalone user scripts are just a very small extension. Also extensions will be relying on most of the user script code.
Review URL: http://codereview.chromium.org/17281
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@7827 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions')
-rw-r--r-- | chrome/browser/extensions/user_script_master.cc | 219 | ||||
-rw-r--r-- | chrome/browser/extensions/user_script_master.h | 76 | ||||
-rw-r--r-- | chrome/browser/extensions/user_script_master_unittest.cc | 118 |
3 files changed, 413 insertions, 0 deletions
diff --git a/chrome/browser/extensions/user_script_master.cc b/chrome/browser/extensions/user_script_master.cc new file mode 100644 index 0000000..dcd912a --- /dev/null +++ b/chrome/browser/extensions/user_script_master.cc @@ -0,0 +1,219 @@ +// Copyright (c) 2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/extensions/user_script_master.h" + +#include <vector> + +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/logging.h" +#include "base/message_loop.h" +#include "base/path_service.h" +#include "base/pickle.h" +#include "base/string_util.h" +#include "chrome/common/notification_service.h" +#include "googleurl/src/gurl.h" +#include "net/base/net_util.h" + +// We reload user scripts on the file thread to prevent blocking the UI. +// ScriptReloader lives on the file thread and does the reload +// work, and then sends a message back to its master with a new SharedMemory*. + +// ScriptReloader is the worker that manages running the script scan +// on the file thread. +// It must be created on, and its public API must only be called from, +// the master's thread. +class UserScriptMaster::ScriptReloader + : public base::RefCounted<UserScriptMaster::ScriptReloader> { + public: + ScriptReloader(UserScriptMaster* master) + : master_(master), master_message_loop_(MessageLoop::current()) {} + + // Start a scan for scripts. + // Will always send a message to the master upon completion. + void StartScan(MessageLoop* work_loop, const FilePath& script_dir); + + // The master is going away; don't call it back. + void DisownMaster() { + master_ = NULL; + } + + private: + // Where functions are run: + // master file + // StartScan -> RunScan + // GetNewScripts() + // NotifyMaster <- RunScan + + // Runs on the master thread. + // Notify the master that new scripts are available. + void NotifyMaster(base::SharedMemory* memory); + + // Runs on the File thread. + // Scan the script directory for scripts, calling NotifyMaster when done. + // The path is intentionally passed by value so its lifetime isn't tied + // to the caller. + void RunScan(const FilePath script_dir); + + // Runs on the File thread. + // Scan the script directory for scripts, returning either a new SharedMemory + // or NULL on error. + base::SharedMemory* GetNewScripts(const FilePath& script_dir); + + // A pointer back to our master. + // May be NULL if DisownMaster() is called. + UserScriptMaster* master_; + + // The message loop to call our master back on. + // Expected to always outlive us. + MessageLoop* master_message_loop_; + + DISALLOW_COPY_AND_ASSIGN(ScriptReloader); +}; + +void UserScriptMaster::ScriptReloader::StartScan( + MessageLoop* work_loop, + const FilePath& script_dir) { + // Add a reference to ourselves to keep ourselves alive while we're running. + // Balanced by NotifyMaster(). + AddRef(); + work_loop->PostTask(FROM_HERE, + NewRunnableMethod(this, + &UserScriptMaster::ScriptReloader::RunScan, + script_dir)); +} + +void UserScriptMaster::ScriptReloader::NotifyMaster( + base::SharedMemory* memory) { + if (!master_) { + // The master went away, so these new scripts aren't useful anymore. + delete memory; + } else { + master_->NewScriptsAvailable(memory); + } + + // Drop our self-reference. + // Balances StartScan(). + Release(); +} + +void UserScriptMaster::ScriptReloader::RunScan(const FilePath script_dir) { + base::SharedMemory* shared_memory = GetNewScripts(script_dir); + + // Post the new scripts back to the master's message loop. + master_message_loop_->PostTask(FROM_HERE, + NewRunnableMethod(this, + &UserScriptMaster::ScriptReloader::NotifyMaster, + shared_memory)); +} + +base::SharedMemory* UserScriptMaster::ScriptReloader::GetNewScripts( + const FilePath& script_dir) { + std::vector<std::wstring> scripts; + + file_util::FileEnumerator enumerator(script_dir, false, + file_util::FileEnumerator::FILES, + FILE_PATH_LITERAL("*.user.js")); + for (FilePath file = enumerator.Next(); !file.value().empty(); + file = enumerator.Next()) { + scripts.push_back(file.ToWStringHack()); + } + + if (scripts.empty()) + return NULL; + + // Pickle scripts data. + Pickle pickle; + pickle.WriteSize(scripts.size()); + for (std::vector<std::wstring>::iterator path = scripts.begin(); + path != scripts.end(); ++path) { + std::string file_url = net::FilePathToFileURL(*path).spec(); + std::string contents; + // TODO(aa): Support unicode script files. + file_util::ReadFileToString(*path, &contents); + + // Write scripts as 'data' so that we can read it out in the slave without + // allocating a new string. + pickle.WriteData(file_url.c_str(), file_url.length()); + pickle.WriteData(contents.c_str(), contents.length()); + } + + // Create the shared memory object. + scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory()); + + if (!shared_memory->Create(std::wstring(), // anonymous + false, // read-only + false, // open existing + pickle.size())) { + return NULL; + } + + // Map into our process. + if (!shared_memory->Map(pickle.size())) + return NULL; + + // Copy the pickle to shared memory. + memcpy(shared_memory->memory(), pickle.data(), pickle.size()); + + return shared_memory.release(); +} + + +UserScriptMaster::UserScriptMaster(MessageLoop* worker_loop, + const FilePath& script_dir) + : user_script_dir_(new FilePath(script_dir)), + dir_watcher_(new DirectoryWatcher), + worker_loop_(worker_loop), + pending_scan_(false) { + // Watch our scripts directory for modifications. + if (dir_watcher_->Watch(script_dir, this)) { + // (Asynchronously) scan for our initial set of scripts. + StartScan(); + } +} + +UserScriptMaster::~UserScriptMaster() { + if (script_reloader_) + script_reloader_->DisownMaster(); +} + +void UserScriptMaster::NewScriptsAvailable(base::SharedMemory* handle) { + // Ensure handle is deleted or released. + scoped_ptr<base::SharedMemory> handle_deleter(handle); + + if (pending_scan_) { + // While we were scanning, there were further changes. Don't bother + // notifying about these scripts and instead just immediately rescan. + pending_scan_ = false; + StartScan(); + } else { + // We're no longer scanning. + script_reloader_ = NULL; + // We've got scripts ready to go. + shared_memory_.swap(handle_deleter); + + NotificationService::current()->Notify(NOTIFY_USER_SCRIPTS_LOADED, + NotificationService::AllSources(), + Details<base::SharedMemory>(handle)); + } +} + +void UserScriptMaster::OnDirectoryChanged(const FilePath& path) { + if (script_reloader_.get()) { + // We're already scanning for scripts. We note that we should rescan when + // we get the chance. + pending_scan_ = true; + return; + } + + StartScan(); +} + +void UserScriptMaster::StartScan() { + if (!script_reloader_) + script_reloader_ = new ScriptReloader(this); + + script_reloader_->StartScan(worker_loop_, *user_script_dir_); +} diff --git a/chrome/browser/extensions/user_script_master.h b/chrome/browser/extensions/user_script_master.h new file mode 100644 index 0000000..4798bb2 --- /dev/null +++ b/chrome/browser/extensions/user_script_master.h @@ -0,0 +1,76 @@ +// Copyright (c) 2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ +#define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ + +#include "base/directory_watcher.h" +#include "base/file_path.h" +#include "base/process.h" +#include "base/scoped_ptr.h" +#include "base/shared_memory.h" + +class MessageLoop; + +// Manages a segment of shared memory that contains the user scripts the user +// has installed. Lives on the UI thread. +class UserScriptMaster : public base::RefCounted<UserScriptMaster>, + public DirectoryWatcher::Delegate { + public: + // For testability, the constructor takes the MessageLoop to run the + // script-reloading worker on as well as the path the scripts live in. + // These are normally the file thread and a directory inside the profile. + UserScriptMaster(MessageLoop* worker, const FilePath& script_dir); + ~UserScriptMaster(); + + // Gets the segment of shared memory for the scripts. + base::SharedMemory* GetSharedMemory() const { + return shared_memory_.get(); + } + + // Called by the script reloader when new scripts have been loaded. + void NewScriptsAvailable(base::SharedMemory* handle); + + // Return true if we have any scripts ready. + bool ScriptsReady() const { return shared_memory_.get() != NULL; } + + // Returns the path to the directory user scripts are stored in. + FilePath user_script_dir() const { return *user_script_dir_; } + + private: + class ScriptReloader; + + // DirectoryWatcher::Delegate implementation. + virtual void OnDirectoryChanged(const FilePath& path); + + // Kicks off a process on the file thread to reload scripts from disk + // into a new chunk of shared memory and notify renderers. + void StartScan(); + + // The directory containing user scripts. + scoped_ptr<FilePath> user_script_dir_; + + // The watcher watches the profile's user scripts directory for new scripts. + scoped_ptr<DirectoryWatcher> dir_watcher_; + + // The MessageLoop that the scanner worker runs on. + // Typically the file thread; configurable for testing. + MessageLoop* worker_loop_; + + // ScriptReloader (in another thread) reloads script off disk. + // We hang on to our pointer to know if we've already got one running. + scoped_refptr<ScriptReloader> script_reloader_; + + // Contains the scripts that were found the last time scripts were updated. + scoped_ptr<base::SharedMemory> shared_memory_; + + // If the script directory is modified while we're rescanning it, we note + // that we're currently mid-scan and then start over again once the scan + // finishes. This boolean tracks whether another scan is pending. + bool pending_scan_; + + DISALLOW_COPY_AND_ASSIGN(UserScriptMaster); +}; + +#endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ diff --git a/chrome/browser/extensions/user_script_master_unittest.cc b/chrome/browser/extensions/user_script_master_unittest.cc new file mode 100644 index 0000000..c630bbd --- /dev/null +++ b/chrome/browser/extensions/user_script_master_unittest.cc @@ -0,0 +1,118 @@ +// Copyright (c) 2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/extensions/user_script_master.h" + +#include <fstream> + +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/message_loop.h" +#include "base/path_service.h" +#include "base/string_util.h" +#include "chrome/common/notification_service.h" +#include "testing/gtest/include/gtest/gtest.h" + +// Test bringing up a master on a specific directory, putting a script in there, etc. + +class UserScriptMasterTest : public testing::Test, + public NotificationObserver { + public: + UserScriptMasterTest() : shared_memory_(NULL) {} + + virtual void SetUp() { + // Name a subdirectory of the temp directory. + std::wstring path_str; + ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &path_str)); + script_dir_ = FilePath(path_str).Append( + FILE_PATH_LITERAL("UserScriptTest")); + + // Create a fresh, empty copy of this directory. + file_util::Delete(script_dir_.value(), true); + file_util::CreateDirectory(script_dir_.value()); + + // Register for all user script notifications. + NotificationService::current()->AddObserver(this, + NOTIFY_USER_SCRIPTS_LOADED, + NotificationService::AllSources()); + } + + virtual void TearDown() { + NotificationService::current()->RemoveObserver(this, + NOTIFY_USER_SCRIPTS_LOADED, + NotificationService::AllSources()); + + // Clean up test directory. + ASSERT_TRUE(file_util::Delete(script_dir_.value(), true)); + ASSERT_FALSE(file_util::PathExists(script_dir_.value())); + } + + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + DCHECK(type == NOTIFY_USER_SCRIPTS_LOADED); + + shared_memory_ = Details<base::SharedMemory>(details).ptr(); + if (MessageLoop::current() == &message_loop_) + MessageLoop::current()->Quit(); + } + + // MessageLoop used in tests. + MessageLoop message_loop_; + + // Directory containing user scripts. + FilePath script_dir_; + + // Updated to the script shared memory when we get notified. + base::SharedMemory* shared_memory_; +}; + +// Test that we *don't* get spurious notifications. +TEST_F(UserScriptMasterTest, NoScripts) { + // Set shared_memory_ to something non-NULL, so we can check it became NULL. + shared_memory_ = reinterpret_cast<base::SharedMemory*>(1); + + scoped_refptr<UserScriptMaster> master( + new UserScriptMaster(MessageLoop::current(), script_dir_)); + message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); + message_loop_.Run(); + + // There were no scripts in the script dir, so we shouldn't have gotten + // a notification. + ASSERT_EQ(NULL, shared_memory_); +} + +// Test that we get notified about new scripts after they're added. +TEST_F(UserScriptMasterTest, NewScripts) { + scoped_refptr<UserScriptMaster> master( + new UserScriptMaster(MessageLoop::current(), script_dir_)); + + FilePath path = script_dir_.Append(FILE_PATH_LITERAL("script.user.js")); + + std::ofstream file; + file.open(WideToUTF8(path.value()).c_str()); + file << "some content"; + file.close(); + + message_loop_.Run(); + + ASSERT_TRUE(shared_memory_ != NULL); +} + +// Test that we get notified about scripts if they're already in the test dir. +TEST_F(UserScriptMasterTest, ExistingScripts) { + FilePath path = script_dir_.Append(FILE_PATH_LITERAL("script.user.js")); + std::ofstream file; + file.open(WideToUTF8(path.value()).c_str()); + file << "some content"; + file.close(); + + scoped_refptr<UserScriptMaster> master( + new UserScriptMaster(MessageLoop::current(), script_dir_)); + + message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); + message_loop_.Run(); + + ASSERT_TRUE(shared_memory_ != NULL); +} |