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

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

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

Powered by Google App Engine
This is Rietveld 408576698