summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authortony@chromium.org <tony@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-15 03:39:50 +0000
committertony@chromium.org <tony@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-15 03:39:50 +0000
commit5020d8dceed088b849318c2010427df1a82bd1ef (patch)
tree868d4b9b3d48e6212553c9ba8685409e7de73cc7 /chrome
parentd6f9c9e2adb5c36fe4091f70ecda64e69dc031b4 (diff)
downloadchromium_src-5020d8dceed088b849318c2010427df1a82bd1ef.zip
chromium_src-5020d8dceed088b849318c2010427df1a82bd1ef.tar.gz
chromium_src-5020d8dceed088b849318c2010427df1a82bd1ef.tar.bz2
Connect UserStyleSheetWatcher to FileWatcher to have changes to
Default/User StyleSheets/Custom.css instantly change layout in all your tabs. BUG=2393 Review URL: http://codereview.chromium.org/799005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@41561 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/browser/user_style_sheet_watcher.cc27
-rw-r--r--chrome/browser/user_style_sheet_watcher.h13
2 files changed, 34 insertions, 6 deletions
diff --git a/chrome/browser/user_style_sheet_watcher.cc b/chrome/browser/user_style_sheet_watcher.cc
index 2d3e945..2bc9568 100644
--- a/chrome/browser/user_style_sheet_watcher.cc
+++ b/chrome/browser/user_style_sheet_watcher.cc
@@ -9,6 +9,15 @@
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"
+namespace {
+
+// The subdirectory of the profile that contains the style sheet.
+const char kStyleSheetDir[] = "User StyleSheets";
+// The filename of the stylesheet.
+const char kUserStyleSheetFile[] = "Custom.css";
+
+} // namespace
+
UserStyleSheetWatcher::UserStyleSheetWatcher(const FilePath& profile_path)
: profile_path_(profile_path),
has_loaded_(false) {
@@ -33,6 +42,12 @@ void UserStyleSheetWatcher::Observe(NotificationType type,
registrar_.RemoveAll();
}
+void UserStyleSheetWatcher::OnFileChanged(const FilePath& path) {
+ ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE,
+ NewRunnableMethod(this, &UserStyleSheetWatcher::LoadStyleSheet,
+ profile_path_));
+}
+
void UserStyleSheetWatcher::Init() {
ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE,
NewRunnableMethod(this, &UserStyleSheetWatcher::LoadStyleSheet,
@@ -43,20 +58,20 @@ void UserStyleSheetWatcher::LoadStyleSheet(const FilePath& profile_path) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
// We keep the user style sheet in a subdir so we can watch for changes
// to the file.
- FilePath style_sheet_dir = profile_path.AppendASCII("User StyleSheets");
+ FilePath style_sheet_dir = profile_path.AppendASCII(kStyleSheetDir);
if (!file_util::DirectoryExists(style_sheet_dir)) {
if (!file_util::CreateDirectory(style_sheet_dir))
return;
}
// Create the file if it doesn't exist.
- FilePath css_file = style_sheet_dir.AppendASCII("Custom.css");
+ FilePath css_file = style_sheet_dir.AppendASCII(kUserStyleSheetFile);
if (!file_util::PathExists(css_file))
file_util::WriteFile(css_file, "", 0);
std::string css;
bool rv = file_util::ReadFileToString(css_file, &css);
GURL style_sheet_url;
- if (rv) {
+ if (rv && !css.empty()) {
std::string css_base64;
rv = base::Base64Encode(css, &css_base64);
if (rv) {
@@ -68,6 +83,12 @@ void UserStyleSheetWatcher::LoadStyleSheet(const FilePath& profile_path) {
ChromeThread::PostTask(ChromeThread::UI, FROM_HERE,
NewRunnableMethod(this, &UserStyleSheetWatcher::SetStyleSheet,
style_sheet_url));
+
+ if (!file_watcher_.get()) {
+ file_watcher_.reset(new FileWatcher);
+ file_watcher_->Watch(profile_path_.AppendASCII(kStyleSheetDir)
+ .AppendASCII(kUserStyleSheetFile), this);
+ }
}
void UserStyleSheetWatcher::SetStyleSheet(const GURL& url) {
diff --git a/chrome/browser/user_style_sheet_watcher.h b/chrome/browser/user_style_sheet_watcher.h
index 87fa5ce..14960a0 100644
--- a/chrome/browser/user_style_sheet_watcher.h
+++ b/chrome/browser/user_style_sheet_watcher.h
@@ -9,17 +9,18 @@
#include "base/logging.h"
#include "base/ref_counted.h"
#include "chrome/browser/chrome_thread.h"
+#include "chrome/browser/file_watcher.h"
#include "chrome/common/notification_observer.h"
#include "chrome/common/notification_registrar.h"
#include "googleurl/src/gurl.h"
// This loads the user style sheet on the file thread and sends a notification
// when the style sheet is loaded.
-// TODO(tony): Watch for file changes and send a notification of the update.
class UserStyleSheetWatcher
: public base::RefCountedThreadSafe<UserStyleSheetWatcher,
ChromeThread::DeleteOnUIThread>,
- public NotificationObserver {
+ public NotificationObserver,
+ public FileWatcher::Delegate {
public:
explicit UserStyleSheetWatcher(const FilePath& profile_path);
virtual ~UserStyleSheetWatcher() {}
@@ -35,6 +36,9 @@ class UserStyleSheetWatcher
const NotificationSource& source,
const NotificationDetails& details);
+ // FileWatcher::Delegate interface
+ virtual void OnFileChanged(const FilePath& path);
+
private:
// Load the user style sheet on the file thread and convert it to a
// base64 URL. Posts the base64 URL back to the UI thread.
@@ -42,12 +46,15 @@ class UserStyleSheetWatcher
void SetStyleSheet(const GURL& url);
- // The directory containing the User StyleSheet.
+ // The directory containing User StyleSheets/Custom.css.
FilePath profile_path_;
// The user style sheet as a base64 data:// URL.
GURL user_style_sheet_;
+ // Watches for changes to the css file so we can reload the style sheet.
+ scoped_ptr<FileWatcher> file_watcher_;
+
NotificationRegistrar registrar_;
bool has_loaded_;