OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/notification_service.h" | 9 #include "chrome/common/notification_service.h" |
10 #include "chrome/common/notification_type.h" | 10 #include "chrome/common/notification_type.h" |
11 | 11 |
| 12 namespace { |
| 13 |
| 14 // The subdirectory of the profile that contains the style sheet. |
| 15 const char kStyleSheetDir[] = "User StyleSheets"; |
| 16 // The filename of the stylesheet. |
| 17 const char kUserStyleSheetFile[] = "Custom.css"; |
| 18 |
| 19 } // namespace |
| 20 |
12 UserStyleSheetWatcher::UserStyleSheetWatcher(const FilePath& profile_path) | 21 UserStyleSheetWatcher::UserStyleSheetWatcher(const FilePath& profile_path) |
13 : profile_path_(profile_path), | 22 : profile_path_(profile_path), |
14 has_loaded_(false) { | 23 has_loaded_(false) { |
15 // Listen for when the first render view host is created. If we load | 24 // Listen for when the first render view host is created. If we load |
16 // too fast, the first tab won't hear the notification and won't get | 25 // too fast, the first tab won't hear the notification and won't get |
17 // the user style sheet. | 26 // the user style sheet. |
18 registrar_.Add(this, NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB, | 27 registrar_.Add(this, NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB, |
19 NotificationService::AllSources()); | 28 NotificationService::AllSources()); |
20 } | 29 } |
21 | 30 |
22 void UserStyleSheetWatcher::Observe(NotificationType type, | 31 void UserStyleSheetWatcher::Observe(NotificationType type, |
23 const NotificationSource& source, const NotificationDetails& details) { | 32 const NotificationSource& source, const NotificationDetails& details) { |
24 DCHECK(type == NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB); | 33 DCHECK(type == NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB); |
25 | 34 |
26 if (has_loaded_) { | 35 if (has_loaded_) { |
27 NotificationService::current()->Notify( | 36 NotificationService::current()->Notify( |
28 NotificationType::USER_STYLE_SHEET_UPDATED, | 37 NotificationType::USER_STYLE_SHEET_UPDATED, |
29 Source<UserStyleSheetWatcher>(this), | 38 Source<UserStyleSheetWatcher>(this), |
30 NotificationService::NoDetails()); | 39 NotificationService::NoDetails()); |
31 } | 40 } |
32 | 41 |
33 registrar_.RemoveAll(); | 42 registrar_.RemoveAll(); |
34 } | 43 } |
35 | 44 |
| 45 void UserStyleSheetWatcher::OnFileChanged(const FilePath& path) { |
| 46 ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, |
| 47 NewRunnableMethod(this, &UserStyleSheetWatcher::LoadStyleSheet, |
| 48 profile_path_)); |
| 49 } |
| 50 |
36 void UserStyleSheetWatcher::Init() { | 51 void UserStyleSheetWatcher::Init() { |
37 ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, | 52 ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, |
38 NewRunnableMethod(this, &UserStyleSheetWatcher::LoadStyleSheet, | 53 NewRunnableMethod(this, &UserStyleSheetWatcher::LoadStyleSheet, |
39 profile_path_)); | 54 profile_path_)); |
40 } | 55 } |
41 | 56 |
42 void UserStyleSheetWatcher::LoadStyleSheet(const FilePath& profile_path) { | 57 void UserStyleSheetWatcher::LoadStyleSheet(const FilePath& profile_path) { |
43 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); | 58 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
44 // We keep the user style sheet in a subdir so we can watch for changes | 59 // We keep the user style sheet in a subdir so we can watch for changes |
45 // to the file. | 60 // to the file. |
46 FilePath style_sheet_dir = profile_path.AppendASCII("User StyleSheets"); | 61 FilePath style_sheet_dir = profile_path.AppendASCII(kStyleSheetDir); |
47 if (!file_util::DirectoryExists(style_sheet_dir)) { | 62 if (!file_util::DirectoryExists(style_sheet_dir)) { |
48 if (!file_util::CreateDirectory(style_sheet_dir)) | 63 if (!file_util::CreateDirectory(style_sheet_dir)) |
49 return; | 64 return; |
50 } | 65 } |
51 // Create the file if it doesn't exist. | 66 // Create the file if it doesn't exist. |
52 FilePath css_file = style_sheet_dir.AppendASCII("Custom.css"); | 67 FilePath css_file = style_sheet_dir.AppendASCII(kUserStyleSheetFile); |
53 if (!file_util::PathExists(css_file)) | 68 if (!file_util::PathExists(css_file)) |
54 file_util::WriteFile(css_file, "", 0); | 69 file_util::WriteFile(css_file, "", 0); |
55 | 70 |
56 std::string css; | 71 std::string css; |
57 bool rv = file_util::ReadFileToString(css_file, &css); | 72 bool rv = file_util::ReadFileToString(css_file, &css); |
58 GURL style_sheet_url; | 73 GURL style_sheet_url; |
59 if (rv) { | 74 if (rv && !css.empty()) { |
60 std::string css_base64; | 75 std::string css_base64; |
61 rv = base::Base64Encode(css, &css_base64); | 76 rv = base::Base64Encode(css, &css_base64); |
62 if (rv) { | 77 if (rv) { |
63 // WebKit knows about data urls, so convert the file to a data url. | 78 // WebKit knows about data urls, so convert the file to a data url. |
64 const char kDataUrlPrefix[] = "data:text/css;charset=utf-8;base64,"; | 79 const char kDataUrlPrefix[] = "data:text/css;charset=utf-8;base64,"; |
65 style_sheet_url = GURL(kDataUrlPrefix + css_base64); | 80 style_sheet_url = GURL(kDataUrlPrefix + css_base64); |
66 } | 81 } |
67 } | 82 } |
68 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, | 83 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, |
69 NewRunnableMethod(this, &UserStyleSheetWatcher::SetStyleSheet, | 84 NewRunnableMethod(this, &UserStyleSheetWatcher::SetStyleSheet, |
70 style_sheet_url)); | 85 style_sheet_url)); |
| 86 |
| 87 if (!file_watcher_.get()) { |
| 88 file_watcher_.reset(new FileWatcher); |
| 89 file_watcher_->Watch(profile_path_.AppendASCII(kStyleSheetDir) |
| 90 .AppendASCII(kUserStyleSheetFile), this); |
| 91 } |
71 } | 92 } |
72 | 93 |
73 void UserStyleSheetWatcher::SetStyleSheet(const GURL& url) { | 94 void UserStyleSheetWatcher::SetStyleSheet(const GURL& url) { |
74 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 95 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
75 | 96 |
76 has_loaded_ = true; | 97 has_loaded_ = true; |
77 user_style_sheet_ = url; | 98 user_style_sheet_ = url; |
78 NotificationService::current()->Notify( | 99 NotificationService::current()->Notify( |
79 NotificationType::USER_STYLE_SHEET_UPDATED, | 100 NotificationType::USER_STYLE_SHEET_UPDATED, |
80 Source<UserStyleSheetWatcher>(this), | 101 Source<UserStyleSheetWatcher>(this), |
81 NotificationService::NoDetails()); | 102 NotificationService::NoDetails()); |
82 } | 103 } |
OLD | NEW |