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

Side by Side Diff: chrome/browser/ui/webui/chrome_url_data_manager.cc

Issue 11881055: Simplify WebUI data sources. Currently WebUI data sources implement a URLDataSourceDelegate interfa… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: fix issue in about_ui exposed by cros tests Created 7 years, 11 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
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/ui/webui/chrome_url_data_manager.h" 5 #include "chrome/browser/ui/webui/chrome_url_data_manager.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/i18n/rtl.h"
11 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
12 #include "base/memory/ref_counted_memory.h" 11 #include "base/memory/ref_counted_memory.h"
13 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop.h" 13 #include "base/message_loop.h"
15 #include "base/string_util.h" 14 #include "base/string_util.h"
16 #include "base/synchronization/lock.h" 15 #include "base/synchronization/lock.h"
17 #include "base/values.h"
18 #include "chrome/browser/net/chrome_url_request_context.h" 16 #include "chrome/browser/net/chrome_url_request_context.h"
19 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/ui/webui/chrome_url_data_manager_factory.h" 18 #include "chrome/browser/ui/webui/chrome_url_data_manager_factory.h"
21 #include "chrome/browser/ui/webui/chrome_url_data_manager_backend.h" 19 #include "chrome/browser/ui/webui/chrome_url_data_manager_backend.h"
22 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
23 #include "content/public/browser/url_data_source_delegate.h" 21 #include "content/public/browser/url_data_source.h"
24 #include "grit/platform_locale_settings.h"
25 #include "ui/base/l10n/l10n_util.h"
26
27 #if defined (TOOLKIT_GTK)
28 #include "ui/base/resource/resource_bundle.h"
29 #include "ui/gfx/font.h"
30 #endif
31
32 #if defined(OS_WIN)
33 #include "base/win/windows_version.h"
34 #endif
35 22
36 using content::BrowserThread; 23 using content::BrowserThread;
37 24
38 static base::LazyInstance<base::Lock>::Leaky 25 static base::LazyInstance<base::Lock>::Leaky
39 g_delete_lock = LAZY_INSTANCE_INITIALIZER; 26 g_delete_lock = LAZY_INSTANCE_INITIALIZER;
40 27
41 // static 28 // static
42 ChromeURLDataManager::URLDataSources* ChromeURLDataManager::data_sources_ = 29 ChromeURLDataManager::URLDataSources* ChromeURLDataManager::data_sources_ =
43 NULL; 30 NULL;
44 31
45 // Invoked on the IO thread to do the actual adding of the DataSource. 32 // Invoked on the IO thread to do the actual adding of the DataSource.
46 static void AddDataSourceOnIOThread( 33 static void AddDataSourceOnIOThread(
47 const base::Callback<ChromeURLDataManagerBackend*(void)>& backend, 34 const base::Callback<ChromeURLDataManagerBackend*(void)>& backend,
48 scoped_refptr<URLDataSource> data_source) { 35 scoped_refptr<URLDataSourceImpl> data_source) {
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
50 backend.Run()->AddDataSource(data_source.get()); 37 backend.Run()->AddDataSource(data_source.get());
51 } 38 }
52 39
53 ChromeURLDataManager::ChromeURLDataManager( 40 ChromeURLDataManager::ChromeURLDataManager(
54 const base::Callback<ChromeURLDataManagerBackend*(void)>& backend) 41 const base::Callback<ChromeURLDataManagerBackend*(void)>& backend)
55 : backend_(backend) { 42 : backend_(backend) {
56 } 43 }
57 44
58 ChromeURLDataManager::~ChromeURLDataManager() { 45 ChromeURLDataManager::~ChromeURLDataManager() {
59 } 46 }
60 47
61 void ChromeURLDataManager::AddDataSource(URLDataSource* source) { 48 void ChromeURLDataManager::AddDataSource(URLDataSourceImpl* source) {
62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
63 BrowserThread::PostTask( 50 BrowserThread::PostTask(
64 BrowserThread::IO, FROM_HERE, 51 BrowserThread::IO, FROM_HERE,
65 base::Bind(&AddDataSourceOnIOThread, 52 base::Bind(&AddDataSourceOnIOThread,
66 backend_, make_scoped_refptr(source))); 53 backend_, make_scoped_refptr(source)));
67 } 54 }
68 55
69 // static 56 // static
70 void ChromeURLDataManager::DeleteDataSources() { 57 void ChromeURLDataManager::DeleteDataSources() {
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
72 URLDataSources sources; 59 URLDataSources sources;
73 { 60 {
74 base::AutoLock lock(g_delete_lock.Get()); 61 base::AutoLock lock(g_delete_lock.Get());
75 if (!data_sources_) 62 if (!data_sources_)
76 return; 63 return;
77 data_sources_->swap(sources); 64 data_sources_->swap(sources);
78 } 65 }
79 for (size_t i = 0; i < sources.size(); ++i) 66 for (size_t i = 0; i < sources.size(); ++i)
80 delete sources[i]; 67 delete sources[i];
81 } 68 }
82 69
83 // static 70 // static
84 void ChromeURLDataManager::DeleteDataSource(const URLDataSource* data_source) { 71 void ChromeURLDataManager::DeleteDataSource(
72 const URLDataSourceImpl* data_source) {
85 // Invoked when a DataSource is no longer referenced and needs to be deleted. 73 // Invoked when a DataSource is no longer referenced and needs to be deleted.
86 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { 74 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
87 // We're on the UI thread, delete right away. 75 // We're on the UI thread, delete right away.
88 delete data_source; 76 delete data_source;
89 return; 77 return;
90 } 78 }
91 79
92 // We're not on the UI thread, add the DataSource to the list of DataSources 80 // We're not on the UI thread, add the DataSource to the list of DataSources
93 // to delete. 81 // to delete.
94 bool schedule_delete = false; 82 bool schedule_delete = false;
95 { 83 {
96 base::AutoLock lock(g_delete_lock.Get()); 84 base::AutoLock lock(g_delete_lock.Get());
97 if (!data_sources_) 85 if (!data_sources_)
98 data_sources_ = new URLDataSources(); 86 data_sources_ = new URLDataSources();
99 schedule_delete = data_sources_->empty(); 87 schedule_delete = data_sources_->empty();
100 data_sources_->push_back(data_source); 88 data_sources_->push_back(data_source);
101 } 89 }
102 if (schedule_delete) { 90 if (schedule_delete) {
103 // Schedule a task to delete the DataSource back on the UI thread. 91 // Schedule a task to delete the DataSource back on the UI thread.
104 BrowserThread::PostTask( 92 BrowserThread::PostTask(
105 BrowserThread::UI, FROM_HERE, 93 BrowserThread::UI, FROM_HERE,
106 base::Bind(&ChromeURLDataManager::DeleteDataSources)); 94 base::Bind(&ChromeURLDataManager::DeleteDataSources));
107 } 95 }
108 } 96 }
109 97
110 // static 98 // static
111 void ChromeURLDataManager::AddDataSource( 99 void ChromeURLDataManager::AddDataSource(
112 Profile* profile, 100 Profile* profile,
113 content::URLDataSourceDelegate* delegate) { 101 content::URLDataSource* source) {
114 if (!delegate->url_data_source_) {
115 delegate->url_data_source_ = new URLDataSource(
116 delegate->GetSource(), delegate);
117 }
118 ChromeURLDataManagerFactory::GetForProfile(profile)->AddDataSource( 102 ChromeURLDataManagerFactory::GetForProfile(profile)->AddDataSource(
119 delegate->url_data_source_); 103 new URLDataSourceImpl(source->GetSource(), source));
104 }
105
106 // static
107 void ChromeURLDataManager::AddDataSourceImpl(Profile* profile,
108 URLDataSourceImpl* source) {
109 ChromeURLDataManagerFactory::GetForProfile(profile)->AddDataSource(source);
120 } 110 }
121 111
122 // static 112 // static
123 bool ChromeURLDataManager::IsScheduledForDeletion( 113 bool ChromeURLDataManager::IsScheduledForDeletion(
124 const URLDataSource* data_source) { 114 const URLDataSourceImpl* data_source) {
125 base::AutoLock lock(g_delete_lock.Get()); 115 base::AutoLock lock(g_delete_lock.Get());
126 if (!data_sources_) 116 if (!data_sources_)
127 return false; 117 return false;
128 return std::find(data_sources_->begin(), data_sources_->end(), data_source) != 118 return std::find(data_sources_->begin(), data_sources_->end(), data_source) !=
129 data_sources_->end(); 119 data_sources_->end();
130 } 120 }
131 121
132 URLDataSource::URLDataSource(const std::string& source_name, 122 URLDataSourceImpl::URLDataSourceImpl(const std::string& source_name,
133 content::URLDataSourceDelegate* delegate) 123 content::URLDataSource* source)
134 : source_name_(source_name), 124 : source_name_(source_name),
135 backend_(NULL), 125 backend_(NULL),
136 delegate_(delegate) { 126 source_(source) {
137 } 127 }
138 128
139 URLDataSource::~URLDataSource() { 129 URLDataSourceImpl::~URLDataSourceImpl() {
140 } 130 }
141 131
142 void URLDataSource::SendResponse( 132 void URLDataSourceImpl::SendResponse(
143 int request_id, 133 int request_id,
144 base::RefCountedMemory* bytes) { 134 base::RefCountedMemory* bytes) {
145 // Take a ref-pointer on entry so byte->Release() will always get called. 135 // Take a ref-pointer on entry so byte->Release() will always get called.
146 scoped_refptr<base::RefCountedMemory> bytes_ptr(bytes); 136 scoped_refptr<base::RefCountedMemory> bytes_ptr(bytes);
147 if (ChromeURLDataManager::IsScheduledForDeletion(this)) { 137 if (ChromeURLDataManager::IsScheduledForDeletion(this)) {
148 // We're scheduled for deletion. Servicing the request would result in 138 // We're scheduled for deletion. Servicing the request would result in
149 // this->AddRef being invoked, even though the ref count is 0 and 'this' is 139 // this->AddRef being invoked, even though the ref count is 0 and 'this' is
150 // about to be deleted. If the AddRef were allowed through, when 'this' is 140 // about to be deleted. If the AddRef were allowed through, when 'this' is
151 // released it would be deleted again. 141 // released it would be deleted again.
152 // 142 //
153 // This scenario occurs with DataSources that make history requests. Such 143 // This scenario occurs with DataSources that make history requests. Such
154 // DataSources do a history query in |StartDataRequest| and the request is 144 // DataSources do a history query in |StartDataRequest| and the request is
155 // live until the object is deleted (history requests don't up the ref 145 // live until the object is deleted (history requests don't up the ref
156 // count). This means it's entirely possible for the DataSource to invoke 146 // count). This means it's entirely possible for the DataSource to invoke
157 // |SendResponse| between the time when there are no more refs and the time 147 // |SendResponse| between the time when there are no more refs and the time
158 // when the object is deleted. 148 // when the object is deleted.
159 return; 149 return;
160 } 150 }
161 BrowserThread::PostTask( 151 BrowserThread::PostTask(
162 BrowserThread::IO, FROM_HERE, 152 BrowserThread::IO, FROM_HERE,
163 base::Bind(&URLDataSource::SendResponseOnIOThread, this, request_id, 153 base::Bind(&URLDataSourceImpl::SendResponseOnIOThread, this, request_id,
164 bytes_ptr)); 154 bytes_ptr));
165 } 155 }
166 156
167 // static 157 void URLDataSourceImpl::SendResponseOnIOThread(
168 void URLDataSource::SetFontAndTextDirection(
169 DictionaryValue* localized_strings) {
170 int web_font_family_id = IDS_WEB_FONT_FAMILY;
171 int web_font_size_id = IDS_WEB_FONT_SIZE;
172 #if defined(OS_WIN)
173 // Vary font settings for Windows XP.
174 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
175 web_font_family_id = IDS_WEB_FONT_FAMILY_XP;
176 web_font_size_id = IDS_WEB_FONT_SIZE_XP;
177 }
178 #endif
179
180 std::string font_family = l10n_util::GetStringUTF8(web_font_family_id);
181
182 #if defined(TOOLKIT_GTK)
183 // Use the system font on Linux/GTK. Keep the hard-coded font families as
184 // backup in case for some crazy reason this one isn't available.
185 font_family = ui::ResourceBundle::GetSharedInstance().GetFont(
186 ui::ResourceBundle::BaseFont).GetFontName() + ", " + font_family;
187 #endif
188
189 localized_strings->SetString("fontfamily", font_family);
190 localized_strings->SetString("fontsize",
191 l10n_util::GetStringUTF8(web_font_size_id));
192 localized_strings->SetString("textdirection",
193 base::i18n::IsRTL() ? "rtl" : "ltr");
194 }
195
196 void URLDataSource::SendResponseOnIOThread(
197 int request_id, 158 int request_id,
198 scoped_refptr<base::RefCountedMemory> bytes) { 159 scoped_refptr<base::RefCountedMemory> bytes) {
199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
200 if (backend_) 161 if (backend_)
201 backend_->DataAvailable(request_id, bytes); 162 backend_->DataAvailable(request_id, bytes);
202 } 163 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698