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/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/common/chrome_notification_types.h" | 10 #include "chrome/common/chrome_notification_types.h" |
11 #include "content/browser/tab_contents/tab_contents.h" | 11 #include "content/browser/tab_contents/tab_contents.h" |
12 #include "content/common/notification_service.h" | 12 #include "content/public/browser/notification_service.h" |
13 #include "content/public/browser/notification_types.h" | 13 #include "content/public/browser/notification_types.h" |
14 | 14 |
15 using ::base::files::FilePathWatcher; | 15 using ::base::files::FilePathWatcher; |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 // The subdirectory of the profile that contains the style sheet. | 19 // The subdirectory of the profile that contains the style sheet. |
20 const char kStyleSheetDir[] = "User StyleSheets"; | 20 const char kStyleSheetDir[] = "User StyleSheets"; |
21 // The filename of the stylesheet. | 21 // The filename of the stylesheet. |
22 const char kUserStyleSheetFile[] = "Custom.css"; | 22 const char kUserStyleSheetFile[] = "Custom.css"; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 | 72 |
73 DISALLOW_COPY_AND_ASSIGN(UserStyleSheetLoader); | 73 DISALLOW_COPY_AND_ASSIGN(UserStyleSheetLoader); |
74 }; | 74 }; |
75 | 75 |
76 UserStyleSheetLoader::UserStyleSheetLoader() | 76 UserStyleSheetLoader::UserStyleSheetLoader() |
77 : has_loaded_(false) { | 77 : has_loaded_(false) { |
78 } | 78 } |
79 | 79 |
80 void UserStyleSheetLoader::NotifyLoaded() { | 80 void UserStyleSheetLoader::NotifyLoaded() { |
81 if (has_loaded_) { | 81 if (has_loaded_) { |
82 NotificationService::current()->Notify( | 82 content::NotificationService::current()->Notify( |
83 chrome::NOTIFICATION_USER_STYLE_SHEET_UPDATED, | 83 chrome::NOTIFICATION_USER_STYLE_SHEET_UPDATED, |
84 content::Source<UserStyleSheetLoader>(this), | 84 content::Source<UserStyleSheetLoader>(this), |
85 NotificationService::NoDetails()); | 85 content::NotificationService::NoDetails()); |
86 } | 86 } |
87 } | 87 } |
88 | 88 |
89 void UserStyleSheetLoader::OnFilePathChanged(const FilePath& path) { | 89 void UserStyleSheetLoader::OnFilePathChanged(const FilePath& path) { |
90 LoadStyleSheet(path); | 90 LoadStyleSheet(path); |
91 } | 91 } |
92 | 92 |
93 void UserStyleSheetLoader::LoadStyleSheet(const FilePath& style_sheet_file) { | 93 void UserStyleSheetLoader::LoadStyleSheet(const FilePath& style_sheet_file) { |
94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
95 // We keep the user style sheet in a subdir so we can watch for changes | 95 // We keep the user style sheet in a subdir so we can watch for changes |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 | 130 |
131 UserStyleSheetWatcher::UserStyleSheetWatcher(Profile* profile, | 131 UserStyleSheetWatcher::UserStyleSheetWatcher(Profile* profile, |
132 const FilePath& profile_path) | 132 const FilePath& profile_path) |
133 : profile_(profile), | 133 : profile_(profile), |
134 profile_path_(profile_path), | 134 profile_path_(profile_path), |
135 loader_(new UserStyleSheetLoader) { | 135 loader_(new UserStyleSheetLoader) { |
136 // Listen for when the first render view host is created. If we load | 136 // Listen for when the first render view host is created. If we load |
137 // too fast, the first tab won't hear the notification and won't get | 137 // too fast, the first tab won't hear the notification and won't get |
138 // the user style sheet. | 138 // the user style sheet. |
139 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB, | 139 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB, |
140 NotificationService::AllBrowserContextsAndSources()); | 140 content::NotificationService::AllBrowserContextsAndSources()); |
141 } | 141 } |
142 | 142 |
143 UserStyleSheetWatcher::~UserStyleSheetWatcher() { | 143 UserStyleSheetWatcher::~UserStyleSheetWatcher() { |
144 } | 144 } |
145 | 145 |
146 void UserStyleSheetWatcher::Init() { | 146 void UserStyleSheetWatcher::Init() { |
147 // Make sure we run on the file thread. | 147 // Make sure we run on the file thread. |
148 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | 148 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { |
149 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 149 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
150 NewRunnableMethod(this, &UserStyleSheetWatcher::Init)); | 150 NewRunnableMethod(this, &UserStyleSheetWatcher::Init)); |
(...skipping 21 matching lines...) Expand all Loading... |
172 const content::NotificationSource& source, | 172 const content::NotificationSource& source, |
173 const content::NotificationDetails& details) { | 173 const content::NotificationDetails& details) { |
174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
175 DCHECK(type == content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB); | 175 DCHECK(type == content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB); |
176 if (profile_->IsSameProfile(Profile::FromBrowserContext( | 176 if (profile_->IsSameProfile(Profile::FromBrowserContext( |
177 content::Source<TabContents>(source)->browser_context()))) { | 177 content::Source<TabContents>(source)->browser_context()))) { |
178 loader_->NotifyLoaded(); | 178 loader_->NotifyLoaded(); |
179 registrar_.RemoveAll(); | 179 registrar_.RemoveAll(); |
180 } | 180 } |
181 } | 181 } |
OLD | NEW |