Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(588)

Side by Side Diff: chrome/browser/user_style_sheet_watcher.cc

Issue 23514056: Function-type templated CallbackRegistry (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Pure merge 2 Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/user_style_sheet_watcher.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/file_util.h" 9 #include "base/file_util.h"
10 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 50
51 GURL user_style_sheet() const { 51 GURL user_style_sheet() const {
52 return user_style_sheet_; 52 return user_style_sheet_;
53 } 53 }
54 54
55 // Load the user style sheet on the file thread and convert it to a 55 // 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. 56 // base64 URL. Posts the base64 URL back to the UI thread.
57 void LoadStyleSheet(const base::FilePath& style_sheet_file); 57 void LoadStyleSheet(const base::FilePath& style_sheet_file);
58 58
59 // Register a callback to be called whenever the stylesheet gets updated. 59 // Register a callback to be called whenever the stylesheet gets updated.
60 scoped_ptr<base::CallbackRegistry<void>::Subscription> 60 scoped_ptr<base::CallbackRegistry<void(void)>::Subscription>
61 RegisterOnStyleSheetUpdatedCallback(const base::Closure& callback); 61 RegisterOnStyleSheetUpdatedCallback(const base::Closure& callback);
62 62
63 // Send out a notification if the stylesheet has already been loaded. 63 // Send out a notification if the stylesheet has already been loaded.
64 void NotifyLoaded(); 64 void NotifyLoaded();
65 65
66 // FilePathWatcher::Callback method: 66 // FilePathWatcher::Callback method:
67 void NotifyPathChanged(const base::FilePath& path, bool error); 67 void NotifyPathChanged(const base::FilePath& path, bool error);
68 68
69 private: 69 private:
70 friend class base::RefCountedThreadSafe<UserStyleSheetLoader>; 70 friend class base::RefCountedThreadSafe<UserStyleSheetLoader>;
71 ~UserStyleSheetLoader(); 71 ~UserStyleSheetLoader();
72 72
73 // Called on the UI thread after the stylesheet has loaded. 73 // Called on the UI thread after the stylesheet has loaded.
74 void SetStyleSheet(const GURL& url); 74 void SetStyleSheet(const GURL& url);
75 75
76 // The user style sheet as a base64 data:// URL. 76 // The user style sheet as a base64 data:// URL.
77 GURL user_style_sheet_; 77 GURL user_style_sheet_;
78 78
79 // Whether the stylesheet has been loaded. 79 // Whether the stylesheet has been loaded.
80 bool has_loaded_; 80 bool has_loaded_;
81 81
82 base::CallbackRegistry<void> style_sheet_updated_callbacks_; 82 base::CallbackRegistry<void(void)> style_sheet_updated_callbacks_;
83 83
84 DISALLOW_COPY_AND_ASSIGN(UserStyleSheetLoader); 84 DISALLOW_COPY_AND_ASSIGN(UserStyleSheetLoader);
85 }; 85 };
86 86
87 UserStyleSheetLoader::UserStyleSheetLoader() 87 UserStyleSheetLoader::UserStyleSheetLoader()
88 : has_loaded_(false) { 88 : has_loaded_(false) {
89 } 89 }
90 90
91 UserStyleSheetLoader::~UserStyleSheetLoader() { 91 UserStyleSheetLoader::~UserStyleSheetLoader() {
92 } 92 }
93 93
94 scoped_ptr<base::CallbackRegistry<void>::Subscription> 94 scoped_ptr<base::CallbackRegistry<void(void)>::Subscription>
95 UserStyleSheetLoader::RegisterOnStyleSheetUpdatedCallback( 95 UserStyleSheetLoader::RegisterOnStyleSheetUpdatedCallback(
96 const base::Closure& callback) { 96 const base::Closure& callback) {
97 return style_sheet_updated_callbacks_.Add(callback); 97 return style_sheet_updated_callbacks_.Add(callback);
98 } 98 }
99 99
100 void UserStyleSheetLoader::NotifyLoaded() { 100 void UserStyleSheetLoader::NotifyLoaded() {
101 if (has_loaded_) { 101 if (has_loaded_) {
102 style_sheet_updated_callbacks_.Notify(); 102 style_sheet_updated_callbacks_.Notify();
103 } 103 }
104 } 104 }
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 LOG(ERROR) << "Failed to setup watch for " << style_sheet_file.value(); 185 LOG(ERROR) << "Failed to setup watch for " << style_sheet_file.value();
186 } 186 }
187 loader_->LoadStyleSheet(style_sheet_file); 187 loader_->LoadStyleSheet(style_sheet_file);
188 } 188 }
189 } 189 }
190 190
191 GURL UserStyleSheetWatcher::user_style_sheet() const { 191 GURL UserStyleSheetWatcher::user_style_sheet() const {
192 return loader_->user_style_sheet(); 192 return loader_->user_style_sheet();
193 } 193 }
194 194
195 scoped_ptr<base::CallbackRegistry<void>::Subscription> 195 scoped_ptr<base::CallbackRegistry<void(void)>::Subscription>
196 UserStyleSheetWatcher::RegisterOnStyleSheetUpdatedCallback( 196 UserStyleSheetWatcher::RegisterOnStyleSheetUpdatedCallback(
197 const base::Closure& callback) { 197 const base::Closure& callback) {
198 return loader_->RegisterOnStyleSheetUpdatedCallback(callback); 198 return loader_->RegisterOnStyleSheetUpdatedCallback(callback);
199 } 199 }
200 200
201 void UserStyleSheetWatcher::Observe(int type, 201 void UserStyleSheetWatcher::Observe(int type,
202 const content::NotificationSource& source, 202 const content::NotificationSource& source,
203 const content::NotificationDetails& details) { 203 const content::NotificationDetails& details) {
204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
205 DCHECK(type == content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED); 205 DCHECK(type == content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED);
206 if (profile_->IsSameProfile(Profile::FromBrowserContext( 206 if (profile_->IsSameProfile(Profile::FromBrowserContext(
207 content::Source<WebContents>(source)->GetBrowserContext()))) { 207 content::Source<WebContents>(source)->GetBrowserContext()))) {
208 loader_->NotifyLoaded(); 208 loader_->NotifyLoaded();
209 registrar_.RemoveAll(); 209 registrar_.RemoveAll();
210 } 210 }
211 } 211 }
212 212
213 void UserStyleSheetWatcher::ShutdownOnUIThread() { 213 void UserStyleSheetWatcher::ShutdownOnUIThread() {
214 registrar_.RemoveAll(); 214 registrar_.RemoveAll();
215 } 215 }
OLDNEW
« no previous file with comments | « chrome/browser/user_style_sheet_watcher.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698