| 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/callback_list.h" |
| 9 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 11 #include "content/public/browser/notification_service.h" | 12 #include "content/public/browser/notification_service.h" |
| 12 #include "content/public/browser/notification_types.h" | 13 #include "content/public/browser/notification_types.h" |
| 13 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 14 | 15 |
| 15 using ::base::FilePathWatcher; | 16 using ::base::FilePathWatcher; |
| 16 using content::BrowserThread; | 17 using content::BrowserThread; |
| 17 using content::WebContents; | 18 using content::WebContents; |
| 18 | 19 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 base::FilePath& style_sheet_file); | 58 void LoadStyleSheet(const base::FilePath& style_sheet_file); |
| 58 | 59 |
| 59 // Register a callback to be called whenever the stylesheet gets updated. | 60 // Register a callback to be called whenever the stylesheet gets updated. |
| 60 void RegisterOnStyleSheetUpdatedCallback(const base::Closure& callback); | 61 base::Closure RegisterOnStyleSheetUpdatedCallback( |
| 62 const base::Closure& callback); |
| 61 | 63 |
| 62 // Send out a notification if the stylesheet has already been loaded. | 64 // Send out a notification if the stylesheet has already been loaded. |
| 63 void NotifyLoaded(); | 65 void NotifyLoaded(); |
| 64 | 66 |
| 65 // FilePathWatcher::Callback method: | 67 // FilePathWatcher::Callback method: |
| 66 void NotifyPathChanged(const base::FilePath& path, bool error); | 68 void NotifyPathChanged(const base::FilePath& path, bool error); |
| 67 | 69 |
| 68 private: | 70 private: |
| 69 friend class base::RefCountedThreadSafe<UserStyleSheetLoader>; | 71 friend class base::RefCountedThreadSafe<UserStyleSheetLoader>; |
| 70 ~UserStyleSheetLoader(); | 72 ~UserStyleSheetLoader(); |
| 71 | 73 |
| 72 // Called on the UI thread after the stylesheet has loaded. | 74 // Called on the UI thread after the stylesheet has loaded. |
| 73 void SetStyleSheet(const GURL& url); | 75 void SetStyleSheet(const GURL& url); |
| 74 | 76 |
| 75 // The user style sheet as a base64 data:// URL. | 77 // The user style sheet as a base64 data:// URL. |
| 76 GURL user_style_sheet_; | 78 GURL user_style_sheet_; |
| 77 | 79 |
| 78 // Whether the stylesheet has been loaded. | 80 // Whether the stylesheet has been loaded. |
| 79 bool has_loaded_; | 81 bool has_loaded_; |
| 80 | 82 |
| 81 std::vector<base::Closure> on_loaded_callbacks_; | 83 CallbackList<base::Closure> on_loaded_callbacks_; |
| 82 | 84 |
| 83 DISALLOW_COPY_AND_ASSIGN(UserStyleSheetLoader); | 85 DISALLOW_COPY_AND_ASSIGN(UserStyleSheetLoader); |
| 84 }; | 86 }; |
| 85 | 87 |
| 86 UserStyleSheetLoader::UserStyleSheetLoader() | 88 UserStyleSheetLoader::UserStyleSheetLoader() |
| 87 : has_loaded_(false) { | 89 : has_loaded_(false) { |
| 88 } | 90 } |
| 89 | 91 |
| 90 void UserStyleSheetLoader::RegisterOnStyleSheetUpdatedCallback( | 92 base::Closure UserStyleSheetLoader::RegisterOnStyleSheetUpdatedCallback( |
| 91 const base::Closure& callback) { | 93 const base::Closure& callback) { |
| 92 on_loaded_callbacks_.push_back(callback); | 94 return on_loaded_callbacks_.RegisterCallback(callback); |
| 93 } | 95 } |
| 94 | 96 |
| 95 void UserStyleSheetLoader::NotifyLoaded() { | 97 void UserStyleSheetLoader::NotifyLoaded() { |
| 96 if (has_loaded_) { | 98 if (has_loaded_) |
| 97 for (size_t i = 0; i < on_loaded_callbacks_.size(); ++i) { | 99 FOR_EACH_CALLBACK(base::Closure, on_loaded_callbacks_); |
| 98 if (!on_loaded_callbacks_[i].is_null()) | |
| 99 on_loaded_callbacks_[i].Run(); | |
| 100 } | |
| 101 } | |
| 102 } | 100 } |
| 103 | 101 |
| 104 void UserStyleSheetLoader::NotifyPathChanged(const base::FilePath& path, | 102 void UserStyleSheetLoader::NotifyPathChanged(const base::FilePath& path, |
| 105 bool error) { | 103 bool error) { |
| 106 if (!error) | 104 if (!error) |
| 107 LoadStyleSheet(path); | 105 LoadStyleSheet(path); |
| 108 } | 106 } |
| 109 | 107 |
| 110 void UserStyleSheetLoader::LoadStyleSheet( | 108 void UserStyleSheetLoader::LoadStyleSheet( |
| 111 const base::FilePath& style_sheet_file) { | 109 const base::FilePath& style_sheet_file) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 132 const char kDataUrlPrefix[] = "data:text/css;charset=utf-8;base64,"; | 130 const char kDataUrlPrefix[] = "data:text/css;charset=utf-8;base64,"; |
| 133 style_sheet_url = GURL(kDataUrlPrefix + css_base64); | 131 style_sheet_url = GURL(kDataUrlPrefix + css_base64); |
| 134 } | 132 } |
| 135 } | 133 } |
| 136 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 134 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 137 base::Bind(&UserStyleSheetLoader::SetStyleSheet, this, | 135 base::Bind(&UserStyleSheetLoader::SetStyleSheet, this, |
| 138 style_sheet_url)); | 136 style_sheet_url)); |
| 139 } | 137 } |
| 140 | 138 |
| 141 UserStyleSheetLoader::~UserStyleSheetLoader() { | 139 UserStyleSheetLoader::~UserStyleSheetLoader() { |
| 142 on_loaded_callbacks_.clear(); | 140 on_loaded_callbacks_.Clear(); |
| 143 } | 141 } |
| 144 | 142 |
| 145 void UserStyleSheetLoader::SetStyleSheet(const GURL& url) { | 143 void UserStyleSheetLoader::SetStyleSheet(const GURL& url) { |
| 146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 147 | 145 |
| 148 has_loaded_ = true; | 146 has_loaded_ = true; |
| 149 user_style_sheet_ = url; | 147 user_style_sheet_ = url; |
| 150 NotifyLoaded(); | 148 NotifyLoaded(); |
| 151 } | 149 } |
| 152 | 150 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 LOG(ERROR) << "Failed to setup watch for " << style_sheet_file.value(); | 185 LOG(ERROR) << "Failed to setup watch for " << style_sheet_file.value(); |
| 188 } | 186 } |
| 189 loader_->LoadStyleSheet(style_sheet_file); | 187 loader_->LoadStyleSheet(style_sheet_file); |
| 190 } | 188 } |
| 191 } | 189 } |
| 192 | 190 |
| 193 GURL UserStyleSheetWatcher::user_style_sheet() const { | 191 GURL UserStyleSheetWatcher::user_style_sheet() const { |
| 194 return loader_->user_style_sheet(); | 192 return loader_->user_style_sheet(); |
| 195 } | 193 } |
| 196 | 194 |
| 197 void UserStyleSheetWatcher::RegisterOnStyleSheetUpdatedCallback( | 195 base::Closure UserStyleSheetWatcher::RegisterOnStyleSheetUpdatedCallback( |
| 198 const base::Closure& callback) { | 196 const base::Closure& callback) { |
| 199 loader_->RegisterOnStyleSheetUpdatedCallback(callback); | 197 return loader_->RegisterOnStyleSheetUpdatedCallback(callback); |
| 200 } | 198 } |
| 201 | 199 |
| 202 void UserStyleSheetWatcher::Observe(int type, | 200 void UserStyleSheetWatcher::Observe(int type, |
| 203 const content::NotificationSource& source, | 201 const content::NotificationSource& source, |
| 204 const content::NotificationDetails& details) { | 202 const content::NotificationDetails& details) { |
| 205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 206 DCHECK(type == content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED); | 204 DCHECK(type == content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED); |
| 207 if (profile_->IsSameProfile(Profile::FromBrowserContext( | 205 if (profile_->IsSameProfile(Profile::FromBrowserContext( |
| 208 content::Source<WebContents>(source)->GetBrowserContext()))) { | 206 content::Source<WebContents>(source)->GetBrowserContext()))) { |
| 209 loader_->NotifyLoaded(); | 207 loader_->NotifyLoaded(); |
| 210 registrar_.RemoveAll(); | 208 registrar_.RemoveAll(); |
| 211 } | 209 } |
| 212 } | 210 } |
| 213 | 211 |
| 214 void UserStyleSheetWatcher::ShutdownOnUIThread() { | 212 void UserStyleSheetWatcher::ShutdownOnUIThread() { |
| 215 registrar_.RemoveAll(); | 213 registrar_.RemoveAll(); |
| 216 } | 214 } |
| OLD | NEW |