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

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

Issue 660349: First cut at custom user style sheets. (Closed)
Patch Set: compile Created 10 years, 9 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/user_style_sheet_watcher.h"
6
7 #include "base/base64.h"
8 #include "base/file_util.h"
9 #include "chrome/common/notification_service.h"
10 #include "chrome/common/notification_type.h"
11
12 UserStyleSheetWatcher::UserStyleSheetWatcher(const FilePath& profile_path)
13 : profile_path_(profile_path),
14 has_loaded_(false) {
15 // 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
17 // the user style sheet.
18 registrar_.Add(this, NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
19 NotificationService::AllSources());
20 }
21
22 void UserStyleSheetWatcher::Observe(NotificationType type,
23 const NotificationSource& source, const NotificationDetails& details) {
24 DCHECK(type == NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB);
25
26 if (has_loaded_) {
27 NotificationService::current()->Notify(
28 NotificationType::USER_STYLE_SHEET_UPDATED,
29 Source<UserStyleSheetWatcher>(this),
30 NotificationService::NoDetails());
31 }
32
33 registrar_.RemoveAll();
34 }
35
36 void UserStyleSheetWatcher::Init() {
37 ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE,
38 NewRunnableMethod(this, &UserStyleSheetWatcher::LoadStyleSheet,
39 profile_path_));
40 }
41
42 void UserStyleSheetWatcher::LoadStyleSheet(const FilePath& profile_path) {
43 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
44 // We keep the user style sheet in a subdir so we can watch for changes
45 // to the file.
46 FilePath style_sheet_dir = profile_path.AppendASCII("User StyleSheets");
47 if (!file_util::DirectoryExists(style_sheet_dir)) {
48 if (!file_util::CreateDirectory(style_sheet_dir))
49 return;
50 }
51 // Create the file if it doesn't exist.
52 FilePath css_file = style_sheet_dir.AppendASCII("Custom.css");
53 if (!file_util::PathExists(css_file))
54 file_util::WriteFile(css_file, "", 0);
55
56 std::string css;
57 bool rv = file_util::ReadFileToString(css_file, &css);
58 GURL style_sheet_url;
59 if (rv) {
60 std::string css_base64;
61 rv = base::Base64Encode(css, &css_base64);
62 if (rv) {
63 // WebKit knows about data urls, so convert the file to a data url.
64 const char kDataUrlPrefix[] = "data:text/css;charset=utf-8;base64,";
65 style_sheet_url = GURL(kDataUrlPrefix + css_base64);
66 }
67 }
68 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE,
69 NewRunnableMethod(this, &UserStyleSheetWatcher::SetStyleSheet,
70 style_sheet_url));
71 }
72
73 void UserStyleSheetWatcher::SetStyleSheet(const GURL& url) {
74 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
75
76 has_loaded_ = true;
77 user_style_sheet_ = url;
78 NotificationService::current()->Notify(
79 NotificationType::USER_STYLE_SHEET_UPDATED,
80 Source<UserStyleSheetWatcher>(this),
81 NotificationService::NoDetails());
82 }
OLDNEW
« no previous file with comments | « chrome/browser/user_style_sheet_watcher.h ('k') | chrome/browser/user_style_sheet_watcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698