| 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/bind.h" |
| 8 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/common/chrome_notification_types.h" | 11 #include "chrome/common/chrome_notification_types.h" |
| 11 #include "content/browser/tab_contents/tab_contents.h" | 12 #include "content/browser/tab_contents/tab_contents.h" |
| 12 #include "content/public/browser/notification_service.h" | 13 #include "content/public/browser/notification_service.h" |
| 13 #include "content/public/browser/notification_types.h" | 14 #include "content/public/browser/notification_types.h" |
| 14 | 15 |
| 15 using ::base::files::FilePathWatcher; | 16 using ::base::files::FilePathWatcher; |
| 16 using content::BrowserThread; | 17 using content::BrowserThread; |
| 17 | 18 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 if (rv && !css.empty()) { | 111 if (rv && !css.empty()) { |
| 111 std::string css_base64; | 112 std::string css_base64; |
| 112 rv = base::Base64Encode(css, &css_base64); | 113 rv = base::Base64Encode(css, &css_base64); |
| 113 if (rv) { | 114 if (rv) { |
| 114 // WebKit knows about data urls, so convert the file to a data url. | 115 // WebKit knows about data urls, so convert the file to a data url. |
| 115 const char kDataUrlPrefix[] = "data:text/css;charset=utf-8;base64,"; | 116 const char kDataUrlPrefix[] = "data:text/css;charset=utf-8;base64,"; |
| 116 style_sheet_url = GURL(kDataUrlPrefix + css_base64); | 117 style_sheet_url = GURL(kDataUrlPrefix + css_base64); |
| 117 } | 118 } |
| 118 } | 119 } |
| 119 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 120 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 120 NewRunnableMethod(this, &UserStyleSheetLoader::SetStyleSheet, | 121 base::Bind(&UserStyleSheetLoader::SetStyleSheet, this, |
| 121 style_sheet_url)); | 122 style_sheet_url)); |
| 122 } | 123 } |
| 123 | 124 |
| 124 void UserStyleSheetLoader::SetStyleSheet(const GURL& url) { | 125 void UserStyleSheetLoader::SetStyleSheet(const GURL& url) { |
| 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 126 | 127 |
| 127 has_loaded_ = true; | 128 has_loaded_ = true; |
| 128 user_style_sheet_ = url; | 129 user_style_sheet_ = url; |
| 129 NotifyLoaded(); | 130 NotifyLoaded(); |
| 130 } | 131 } |
| 131 | 132 |
| 132 UserStyleSheetWatcher::UserStyleSheetWatcher(Profile* profile, | 133 UserStyleSheetWatcher::UserStyleSheetWatcher(Profile* profile, |
| 133 const FilePath& profile_path) | 134 const FilePath& profile_path) |
| 134 : profile_(profile), | 135 : profile_(profile), |
| 135 profile_path_(profile_path), | 136 profile_path_(profile_path), |
| 136 loader_(new UserStyleSheetLoader) { | 137 loader_(new UserStyleSheetLoader) { |
| 137 // Listen for when the first render view host is created. If we load | 138 // Listen for when the first render view host is created. If we load |
| 138 // too fast, the first tab won't hear the notification and won't get | 139 // too fast, the first tab won't hear the notification and won't get |
| 139 // the user style sheet. | 140 // the user style sheet. |
| 140 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB, | 141 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB, |
| 141 content::NotificationService::AllBrowserContextsAndSources()); | 142 content::NotificationService::AllBrowserContextsAndSources()); |
| 142 } | 143 } |
| 143 | 144 |
| 144 UserStyleSheetWatcher::~UserStyleSheetWatcher() { | 145 UserStyleSheetWatcher::~UserStyleSheetWatcher() { |
| 145 } | 146 } |
| 146 | 147 |
| 147 void UserStyleSheetWatcher::Init() { | 148 void UserStyleSheetWatcher::Init() { |
| 148 // Make sure we run on the file thread. | 149 // Make sure we run on the file thread. |
| 149 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | 150 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { |
| 150 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 151 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 151 NewRunnableMethod(this, &UserStyleSheetWatcher::Init)); | 152 base::Bind(&UserStyleSheetWatcher::Init, this)); |
| 152 return; | 153 return; |
| 153 } | 154 } |
| 154 | 155 |
| 155 if (!file_watcher_.get()) { | 156 if (!file_watcher_.get()) { |
| 156 file_watcher_.reset(new FilePathWatcher); | 157 file_watcher_.reset(new FilePathWatcher); |
| 157 FilePath style_sheet_file = profile_path_.AppendASCII(kStyleSheetDir) | 158 FilePath style_sheet_file = profile_path_.AppendASCII(kStyleSheetDir) |
| 158 .AppendASCII(kUserStyleSheetFile); | 159 .AppendASCII(kUserStyleSheetFile); |
| 159 if (!file_watcher_->Watch( | 160 if (!file_watcher_->Watch( |
| 160 style_sheet_file, | 161 style_sheet_file, |
| 161 loader_.get())) { | 162 loader_.get())) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 173 const content::NotificationSource& source, | 174 const content::NotificationSource& source, |
| 174 const content::NotificationDetails& details) { | 175 const content::NotificationDetails& details) { |
| 175 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 176 DCHECK(type == content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB); | 177 DCHECK(type == content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB); |
| 177 if (profile_->IsSameProfile(Profile::FromBrowserContext( | 178 if (profile_->IsSameProfile(Profile::FromBrowserContext( |
| 178 content::Source<TabContents>(source)->browser_context()))) { | 179 content::Source<TabContents>(source)->browser_context()))) { |
| 179 loader_->NotifyLoaded(); | 180 loader_->NotifyLoaded(); |
| 180 registrar_.RemoveAll(); | 181 registrar_.RemoveAll(); |
| 181 } | 182 } |
| 182 } | 183 } |
| OLD | NEW |