| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/user_style_sheet_watcher.h" | 5 #include "chrome/browser/user_style_sheet_watcher.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 // V | | 37 // V | |
| 38 // .----------------------. | | 38 // .----------------------. | |
| 39 // | UserStyleSheetLoader |<--------------------' | 39 // | UserStyleSheetLoader |<--------------------' |
| 40 // '----------------------' | 40 // '----------------------' |
| 41 // | 41 // |
| 42 // FilePathWatcher's reference to UserStyleSheetLoader is used for delivering | 42 // FilePathWatcher's reference to UserStyleSheetLoader is used for delivering |
| 43 // the change notifications. Since they happen asynchronously, | 43 // the change notifications. Since they happen asynchronously, |
| 44 // UserStyleSheetWatcher and its FilePathWatcher may be destroyed while a | 44 // UserStyleSheetWatcher and its FilePathWatcher may be destroyed while a |
| 45 // callback to UserStyleSheetLoader is in progress, in which case the | 45 // callback to UserStyleSheetLoader is in progress, in which case the |
| 46 // UserStyleSheetLoader object outlives the watchers. | 46 // UserStyleSheetLoader object outlives the watchers. |
| 47 class UserStyleSheetLoader : public FilePathWatcher::Delegate { | 47 class UserStyleSheetLoader |
| 48 : public base::RefCountedThreadSafe<UserStyleSheetLoader> { |
| 48 public: | 49 public: |
| 49 UserStyleSheetLoader(); | 50 UserStyleSheetLoader(); |
| 50 | 51 |
| 51 GURL user_style_sheet() const { | 52 GURL user_style_sheet() const { |
| 52 return user_style_sheet_; | 53 return user_style_sheet_; |
| 53 } | 54 } |
| 54 | 55 |
| 55 // Load the user style sheet on the file thread and convert it to a | 56 // Load the user style sheet on the file thread and convert it to a |
| 56 // base64 URL. Posts the base64 URL back to the UI thread. | 57 // base64 URL. Posts the base64 URL back to the UI thread. |
| 57 void LoadStyleSheet(const FilePath& style_sheet_file); | 58 void LoadStyleSheet(const FilePath& style_sheet_file); |
| 58 | 59 |
| 59 // Send out a notification if the stylesheet has already been loaded. | 60 // Send out a notification if the stylesheet has already been loaded. |
| 60 void NotifyLoaded(); | 61 void NotifyLoaded(); |
| 61 | 62 |
| 62 // FilePathWatcher::Delegate interface | 63 // FilePathWatcher::Callback method: |
| 63 virtual void OnFilePathChanged(const FilePath& path); | 64 void NotifyPathChanged(const FilePath& path, bool error); |
| 64 | 65 |
| 65 private: | 66 private: |
| 66 virtual ~UserStyleSheetLoader() {} | 67 friend class base::RefCountedThreadSafe<UserStyleSheetLoader>; |
| 68 ~UserStyleSheetLoader() {} |
| 67 | 69 |
| 68 // Called on the UI thread after the stylesheet has loaded. | 70 // Called on the UI thread after the stylesheet has loaded. |
| 69 void SetStyleSheet(const GURL& url); | 71 void SetStyleSheet(const GURL& url); |
| 70 | 72 |
| 71 // The user style sheet as a base64 data:// URL. | 73 // The user style sheet as a base64 data:// URL. |
| 72 GURL user_style_sheet_; | 74 GURL user_style_sheet_; |
| 73 | 75 |
| 74 // Whether the stylesheet has been loaded. | 76 // Whether the stylesheet has been loaded. |
| 75 bool has_loaded_; | 77 bool has_loaded_; |
| 76 | 78 |
| 77 DISALLOW_COPY_AND_ASSIGN(UserStyleSheetLoader); | 79 DISALLOW_COPY_AND_ASSIGN(UserStyleSheetLoader); |
| 78 }; | 80 }; |
| 79 | 81 |
| 80 UserStyleSheetLoader::UserStyleSheetLoader() | 82 UserStyleSheetLoader::UserStyleSheetLoader() |
| 81 : has_loaded_(false) { | 83 : has_loaded_(false) { |
| 82 } | 84 } |
| 83 | 85 |
| 84 void UserStyleSheetLoader::NotifyLoaded() { | 86 void UserStyleSheetLoader::NotifyLoaded() { |
| 85 if (has_loaded_) { | 87 if (has_loaded_) { |
| 86 content::NotificationService::current()->Notify( | 88 content::NotificationService::current()->Notify( |
| 87 chrome::NOTIFICATION_USER_STYLE_SHEET_UPDATED, | 89 chrome::NOTIFICATION_USER_STYLE_SHEET_UPDATED, |
| 88 content::Source<UserStyleSheetLoader>(this), | 90 content::Source<UserStyleSheetLoader>(this), |
| 89 content::NotificationService::NoDetails()); | 91 content::NotificationService::NoDetails()); |
| 90 } | 92 } |
| 91 } | 93 } |
| 92 | 94 |
| 93 void UserStyleSheetLoader::OnFilePathChanged(const FilePath& path) { | 95 void UserStyleSheetLoader::NotifyPathChanged(const FilePath& path, bool error) { |
| 94 LoadStyleSheet(path); | 96 if (!error) |
| 97 LoadStyleSheet(path); |
| 95 } | 98 } |
| 96 | 99 |
| 97 void UserStyleSheetLoader::LoadStyleSheet(const FilePath& style_sheet_file) { | 100 void UserStyleSheetLoader::LoadStyleSheet(const FilePath& style_sheet_file) { |
| 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 99 // We keep the user style sheet in a subdir so we can watch for changes | 102 // We keep the user style sheet in a subdir so we can watch for changes |
| 100 // to the file. | 103 // to the file. |
| 101 FilePath style_sheet_dir = style_sheet_file.DirName(); | 104 FilePath style_sheet_dir = style_sheet_file.DirName(); |
| 102 if (!file_util::DirectoryExists(style_sheet_dir)) { | 105 if (!file_util::DirectoryExists(style_sheet_dir)) { |
| 103 if (!file_util::CreateDirectory(style_sheet_dir)) | 106 if (!file_util::CreateDirectory(style_sheet_dir)) |
| 104 return; | 107 return; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 158 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 156 base::Bind(&UserStyleSheetWatcher::Init, this)); | 159 base::Bind(&UserStyleSheetWatcher::Init, this)); |
| 157 return; | 160 return; |
| 158 } | 161 } |
| 159 | 162 |
| 160 if (!file_watcher_.get()) { | 163 if (!file_watcher_.get()) { |
| 161 file_watcher_.reset(new FilePathWatcher); | 164 file_watcher_.reset(new FilePathWatcher); |
| 162 FilePath style_sheet_file = profile_path_.AppendASCII(kStyleSheetDir) | 165 FilePath style_sheet_file = profile_path_.AppendASCII(kStyleSheetDir) |
| 163 .AppendASCII(kUserStyleSheetFile); | 166 .AppendASCII(kUserStyleSheetFile); |
| 164 if (!file_watcher_->Watch( | 167 if (!file_watcher_->Watch( |
| 165 style_sheet_file, | 168 style_sheet_file, |
| 166 loader_.get())) { | 169 false, |
| 170 base::Bind(&UserStyleSheetLoader::NotifyPathChanged, |
| 171 loader_.get()))) { |
| 167 LOG(ERROR) << "Failed to setup watch for " << style_sheet_file.value(); | 172 LOG(ERROR) << "Failed to setup watch for " << style_sheet_file.value(); |
| 168 } | 173 } |
| 169 loader_->LoadStyleSheet(style_sheet_file); | 174 loader_->LoadStyleSheet(style_sheet_file); |
| 170 } | 175 } |
| 171 } | 176 } |
| 172 | 177 |
| 173 GURL UserStyleSheetWatcher::user_style_sheet() const { | 178 GURL UserStyleSheetWatcher::user_style_sheet() const { |
| 174 return loader_->user_style_sheet(); | 179 return loader_->user_style_sheet(); |
| 175 } | 180 } |
| 176 | 181 |
| 177 void UserStyleSheetWatcher::Observe(int type, | 182 void UserStyleSheetWatcher::Observe(int type, |
| 178 const content::NotificationSource& source, | 183 const content::NotificationSource& source, |
| 179 const content::NotificationDetails& details) { | 184 const content::NotificationDetails& details) { |
| 180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 181 DCHECK(type == content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED); | 186 DCHECK(type == content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED); |
| 182 if (profile_->IsSameProfile(Profile::FromBrowserContext( | 187 if (profile_->IsSameProfile(Profile::FromBrowserContext( |
| 183 content::Source<WebContents>(source)->GetBrowserContext()))) { | 188 content::Source<WebContents>(source)->GetBrowserContext()))) { |
| 184 loader_->NotifyLoaded(); | 189 loader_->NotifyLoaded(); |
| 185 registrar_.RemoveAll(); | 190 registrar_.RemoveAll(); |
| 186 } | 191 } |
| 187 } | 192 } |
| 188 | 193 |
| 189 void UserStyleSheetWatcher::ShutdownOnUIThread() { | 194 void UserStyleSheetWatcher::ShutdownOnUIThread() { |
| 190 registrar_.RemoveAll(); | 195 registrar_.RemoveAll(); |
| 191 } | 196 } |
| OLD | NEW |