| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "chrome/common/chrome_notification_types.h" |
| 10 #include "content/common/content_notification_types.h" |
| 9 #include "content/common/notification_service.h" | 11 #include "content/common/notification_service.h" |
| 10 #include "content/common/notification_type.h" | |
| 11 | 12 |
| 12 using ::base::files::FilePathWatcher; | 13 using ::base::files::FilePathWatcher; |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 // The subdirectory of the profile that contains the style sheet. | 17 // The subdirectory of the profile that contains the style sheet. |
| 17 const char kStyleSheetDir[] = "User StyleSheets"; | 18 const char kStyleSheetDir[] = "User StyleSheets"; |
| 18 // The filename of the stylesheet. | 19 // The filename of the stylesheet. |
| 19 const char kUserStyleSheetFile[] = "Custom.css"; | 20 const char kUserStyleSheetFile[] = "Custom.css"; |
| 20 | 21 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 DISALLOW_COPY_AND_ASSIGN(UserStyleSheetLoader); | 71 DISALLOW_COPY_AND_ASSIGN(UserStyleSheetLoader); |
| 71 }; | 72 }; |
| 72 | 73 |
| 73 UserStyleSheetLoader::UserStyleSheetLoader() | 74 UserStyleSheetLoader::UserStyleSheetLoader() |
| 74 : has_loaded_(false) { | 75 : has_loaded_(false) { |
| 75 } | 76 } |
| 76 | 77 |
| 77 void UserStyleSheetLoader::NotifyLoaded() { | 78 void UserStyleSheetLoader::NotifyLoaded() { |
| 78 if (has_loaded_) { | 79 if (has_loaded_) { |
| 79 NotificationService::current()->Notify( | 80 NotificationService::current()->Notify( |
| 80 NotificationType::USER_STYLE_SHEET_UPDATED, | 81 chrome::NOTIFICATION_USER_STYLE_SHEET_UPDATED, |
| 81 Source<UserStyleSheetLoader>(this), | 82 Source<UserStyleSheetLoader>(this), |
| 82 NotificationService::NoDetails()); | 83 NotificationService::NoDetails()); |
| 83 } | 84 } |
| 84 } | 85 } |
| 85 | 86 |
| 86 void UserStyleSheetLoader::OnFilePathChanged(const FilePath& path) { | 87 void UserStyleSheetLoader::OnFilePathChanged(const FilePath& path) { |
| 87 LoadStyleSheet(path); | 88 LoadStyleSheet(path); |
| 88 } | 89 } |
| 89 | 90 |
| 90 void UserStyleSheetLoader::LoadStyleSheet(const FilePath& style_sheet_file) { | 91 void UserStyleSheetLoader::LoadStyleSheet(const FilePath& style_sheet_file) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 user_style_sheet_ = url; | 125 user_style_sheet_ = url; |
| 125 NotifyLoaded(); | 126 NotifyLoaded(); |
| 126 } | 127 } |
| 127 | 128 |
| 128 UserStyleSheetWatcher::UserStyleSheetWatcher(const FilePath& profile_path) | 129 UserStyleSheetWatcher::UserStyleSheetWatcher(const FilePath& profile_path) |
| 129 : profile_path_(profile_path), | 130 : profile_path_(profile_path), |
| 130 loader_(new UserStyleSheetLoader) { | 131 loader_(new UserStyleSheetLoader) { |
| 131 // Listen for when the first render view host is created. If we load | 132 // Listen for when the first render view host is created. If we load |
| 132 // too fast, the first tab won't hear the notification and won't get | 133 // too fast, the first tab won't hear the notification and won't get |
| 133 // the user style sheet. | 134 // the user style sheet. |
| 134 registrar_.Add(this, NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB, | 135 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB, |
| 135 NotificationService::AllSources()); | 136 NotificationService::AllSources()); |
| 136 } | 137 } |
| 137 | 138 |
| 138 UserStyleSheetWatcher::~UserStyleSheetWatcher() { | 139 UserStyleSheetWatcher::~UserStyleSheetWatcher() { |
| 139 } | 140 } |
| 140 | 141 |
| 141 void UserStyleSheetWatcher::Init() { | 142 void UserStyleSheetWatcher::Init() { |
| 142 // Make sure we run on the file thread. | 143 // Make sure we run on the file thread. |
| 143 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | 144 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { |
| 144 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 145 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 156 LOG(ERROR) << "Failed to setup watch for " << style_sheet_file.value(); | 157 LOG(ERROR) << "Failed to setup watch for " << style_sheet_file.value(); |
| 157 } | 158 } |
| 158 loader_->LoadStyleSheet(style_sheet_file); | 159 loader_->LoadStyleSheet(style_sheet_file); |
| 159 } | 160 } |
| 160 } | 161 } |
| 161 | 162 |
| 162 GURL UserStyleSheetWatcher::user_style_sheet() const { | 163 GURL UserStyleSheetWatcher::user_style_sheet() const { |
| 163 return loader_->user_style_sheet(); | 164 return loader_->user_style_sheet(); |
| 164 } | 165 } |
| 165 | 166 |
| 166 void UserStyleSheetWatcher::Observe(NotificationType type, | 167 void UserStyleSheetWatcher::Observe(int type, |
| 167 const NotificationSource& source, const NotificationDetails& details) { | 168 const NotificationSource& source, const NotificationDetails& details) { |
| 168 DCHECK(type == NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB); | 169 DCHECK(type == content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB); |
| 169 loader_->NotifyLoaded(); | 170 loader_->NotifyLoaded(); |
| 170 registrar_.RemoveAll(); | 171 registrar_.RemoveAll(); |
| 171 } | 172 } |
| OLD | NEW |