OLD | NEW |
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_ = |
| 43 NULL; |
42 | 44 |
43 // Invoked on the IO thread to do the actual adding of the DataSource. | 45 // Invoked on the IO thread to do the actual adding of the DataSource. |
44 static void AddDataSourceOnIOThread( | 46 static void AddDataSourceOnIOThread( |
45 const base::Callback<ChromeURLDataManagerBackend*(void)>& backend, | 47 const base::Callback<ChromeURLDataManagerBackend*(void)>& backend, |
46 scoped_refptr<ChromeURLDataManager::DataSource> data_source) { | 48 scoped_refptr<URLDataSource> data_source) { |
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
48 backend.Run()->AddDataSource(data_source.get()); | 50 backend.Run()->AddDataSource(data_source.get()); |
49 } | 51 } |
50 | 52 |
51 ChromeURLDataManager::ChromeURLDataManager( | 53 ChromeURLDataManager::ChromeURLDataManager( |
52 const base::Callback<ChromeURLDataManagerBackend*(void)>& backend) | 54 const base::Callback<ChromeURLDataManagerBackend*(void)>& backend) |
53 : backend_(backend) { | 55 : backend_(backend) { |
54 } | 56 } |
55 | 57 |
56 ChromeURLDataManager::~ChromeURLDataManager() { | 58 ChromeURLDataManager::~ChromeURLDataManager() { |
57 } | 59 } |
58 | 60 |
59 void ChromeURLDataManager::AddDataSource(DataSource* source) { | 61 void ChromeURLDataManager::AddDataSource(URLDataSource* source) { |
60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
61 BrowserThread::PostTask( | 63 BrowserThread::PostTask( |
62 BrowserThread::IO, FROM_HERE, | 64 BrowserThread::IO, FROM_HERE, |
63 base::Bind(&AddDataSourceOnIOThread, | 65 base::Bind(&AddDataSourceOnIOThread, |
64 backend_, make_scoped_refptr(source))); | 66 backend_, make_scoped_refptr(source))); |
65 } | 67 } |
66 | 68 |
67 // static | 69 // static |
68 void ChromeURLDataManager::DeleteDataSources() { | 70 void ChromeURLDataManager::DeleteDataSources() { |
69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
70 DataSources sources; | 72 URLDataSources sources; |
71 { | 73 { |
72 base::AutoLock lock(g_delete_lock.Get()); | 74 base::AutoLock lock(g_delete_lock.Get()); |
73 if (!data_sources_) | 75 if (!data_sources_) |
74 return; | 76 return; |
75 data_sources_->swap(sources); | 77 data_sources_->swap(sources); |
76 } | 78 } |
77 for (size_t i = 0; i < sources.size(); ++i) | 79 for (size_t i = 0; i < sources.size(); ++i) |
78 delete sources[i]; | 80 delete sources[i]; |
79 } | 81 } |
80 | 82 |
81 // static | 83 // static |
82 void ChromeURLDataManager::DeleteDataSource(const DataSource* data_source) { | 84 void ChromeURLDataManager::DeleteDataSource(const URLDataSource* data_source) { |
83 // Invoked when a DataSource is no longer referenced and needs to be deleted. | 85 // Invoked when a DataSource is no longer referenced and needs to be deleted. |
84 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 86 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
85 // We're on the UI thread, delete right away. | 87 // We're on the UI thread, delete right away. |
86 delete data_source; | 88 delete data_source; |
87 return; | 89 return; |
88 } | 90 } |
89 | 91 |
90 // We're not on the UI thread, add the DataSource to the list of DataSources | 92 // We're not on the UI thread, add the DataSource to the list of DataSources |
91 // to delete. | 93 // to delete. |
92 bool schedule_delete = false; | 94 bool schedule_delete = false; |
93 { | 95 { |
94 base::AutoLock lock(g_delete_lock.Get()); | 96 base::AutoLock lock(g_delete_lock.Get()); |
95 if (!data_sources_) | 97 if (!data_sources_) |
96 data_sources_ = new DataSources(); | 98 data_sources_ = new URLDataSources(); |
97 schedule_delete = data_sources_->empty(); | 99 schedule_delete = data_sources_->empty(); |
98 data_sources_->push_back(data_source); | 100 data_sources_->push_back(data_source); |
99 } | 101 } |
100 if (schedule_delete) { | 102 if (schedule_delete) { |
101 // Schedule a task to delete the DataSource back on the UI thread. | 103 // Schedule a task to delete the DataSource back on the UI thread. |
102 BrowserThread::PostTask( | 104 BrowserThread::PostTask( |
103 BrowserThread::UI, FROM_HERE, | 105 BrowserThread::UI, FROM_HERE, |
104 base::Bind(&ChromeURLDataManager::DeleteDataSources)); | 106 base::Bind(&ChromeURLDataManager::DeleteDataSources)); |
105 } | 107 } |
106 } | 108 } |
107 | 109 |
108 // static | 110 // static |
109 void ChromeURLDataManager::AddDataSource(Profile* profile, DataSource* source) { | 111 void ChromeURLDataManager::AddDataSource( |
110 ChromeURLDataManagerFactory::GetForProfile(profile)->AddDataSource(source); | 112 Profile* profile, |
| 113 content::URLDataSourceDelegate* delegate) { |
| 114 if (!delegate->url_data_source_) { |
| 115 delegate->url_data_source_ = new URLDataSource( |
| 116 delegate->GetSource(), delegate); |
| 117 } |
| 118 ChromeURLDataManagerFactory::GetForProfile(profile)->AddDataSource( |
| 119 delegate->url_data_source_); |
111 } | 120 } |
112 | 121 |
113 // static | 122 // static |
114 bool ChromeURLDataManager::IsScheduledForDeletion( | 123 bool ChromeURLDataManager::IsScheduledForDeletion( |
115 const DataSource* data_source) { | 124 const URLDataSource* data_source) { |
116 base::AutoLock lock(g_delete_lock.Get()); | 125 base::AutoLock lock(g_delete_lock.Get()); |
117 if (!data_sources_) | 126 if (!data_sources_) |
118 return false; | 127 return false; |
119 return std::find(data_sources_->begin(), data_sources_->end(), data_source) != | 128 return std::find(data_sources_->begin(), data_sources_->end(), data_source) != |
120 data_sources_->end(); | 129 data_sources_->end(); |
121 } | 130 } |
122 | 131 |
123 ChromeURLDataManager::DataSource::DataSource(const std::string& source_name, | 132 URLDataSource::URLDataSource(const std::string& source_name, |
124 MessageLoop* message_loop) | 133 content::URLDataSourceDelegate* delegate) |
125 : source_name_(source_name), | 134 : source_name_(source_name), |
126 message_loop_(message_loop), | 135 backend_(NULL), |
127 backend_(NULL) { | 136 delegate_(delegate) { |
128 } | 137 } |
129 | 138 |
130 ChromeURLDataManager::DataSource::~DataSource() { | 139 URLDataSource::~URLDataSource() { |
131 } | 140 } |
132 | 141 |
133 void ChromeURLDataManager::DataSource::SendResponse( | 142 void URLDataSource::SendResponse( |
134 int request_id, | 143 int request_id, |
135 base::RefCountedMemory* bytes) { | 144 base::RefCountedMemory* bytes) { |
136 // Take a ref-pointer on entry so byte->Release() will always get called. | 145 // Take a ref-pointer on entry so byte->Release() will always get called. |
137 scoped_refptr<base::RefCountedMemory> bytes_ptr(bytes); | 146 scoped_refptr<base::RefCountedMemory> bytes_ptr(bytes); |
138 if (IsScheduledForDeletion(this)) { | 147 if (ChromeURLDataManager::IsScheduledForDeletion(this)) { |
139 // We're scheduled for deletion. Servicing the request would result in | 148 // 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 | 149 // 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 | 150 // about to be deleted. If the AddRef were allowed through, when 'this' is |
142 // released it would be deleted again. | 151 // released it would be deleted again. |
143 // | 152 // |
144 // This scenario occurs with DataSources that make history requests. Such | 153 // This scenario occurs with DataSources that make history requests. Such |
145 // DataSources do a history query in |StartDataRequest| and the request is | 154 // 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 | 155 // 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 | 156 // 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 | 157 // |SendResponse| between the time when there are no more refs and the time |
149 // when the object is deleted. | 158 // when the object is deleted. |
150 return; | 159 return; |
151 } | 160 } |
152 BrowserThread::PostTask( | 161 BrowserThread::PostTask( |
153 BrowserThread::IO, FROM_HERE, | 162 BrowserThread::IO, FROM_HERE, |
154 base::Bind(&DataSource::SendResponseOnIOThread, this, request_id, | 163 base::Bind(&URLDataSource::SendResponseOnIOThread, this, request_id, |
155 bytes_ptr)); | 164 bytes_ptr)); |
156 } | 165 } |
157 | 166 |
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 | 167 // static |
170 void ChromeURLDataManager::DataSource::SetFontAndTextDirection( | 168 void URLDataSource::SetFontAndTextDirection( |
171 DictionaryValue* localized_strings) { | 169 DictionaryValue* localized_strings) { |
172 int web_font_family_id = IDS_WEB_FONT_FAMILY; | 170 int web_font_family_id = IDS_WEB_FONT_FAMILY; |
173 int web_font_size_id = IDS_WEB_FONT_SIZE; | 171 int web_font_size_id = IDS_WEB_FONT_SIZE; |
174 #if defined(OS_WIN) | 172 #if defined(OS_WIN) |
175 // Vary font settings for Windows XP. | 173 // Vary font settings for Windows XP. |
176 if (base::win::GetVersion() < base::win::VERSION_VISTA) { | 174 if (base::win::GetVersion() < base::win::VERSION_VISTA) { |
177 web_font_family_id = IDS_WEB_FONT_FAMILY_XP; | 175 web_font_family_id = IDS_WEB_FONT_FAMILY_XP; |
178 web_font_size_id = IDS_WEB_FONT_SIZE_XP; | 176 web_font_size_id = IDS_WEB_FONT_SIZE_XP; |
179 } | 177 } |
180 #endif | 178 #endif |
181 | 179 |
182 std::string font_family = l10n_util::GetStringUTF8(web_font_family_id); | 180 std::string font_family = l10n_util::GetStringUTF8(web_font_family_id); |
183 | 181 |
184 #if defined(TOOLKIT_GTK) | 182 #if defined(TOOLKIT_GTK) |
185 // Use the system font on Linux/GTK. Keep the hard-coded font families as | 183 // 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. | 184 // backup in case for some crazy reason this one isn't available. |
187 font_family = ui::ResourceBundle::GetSharedInstance().GetFont( | 185 font_family = ui::ResourceBundle::GetSharedInstance().GetFont( |
188 ui::ResourceBundle::BaseFont).GetFontName() + ", " + font_family; | 186 ui::ResourceBundle::BaseFont).GetFontName() + ", " + font_family; |
189 #endif | 187 #endif |
190 | 188 |
191 localized_strings->SetString("fontfamily", font_family); | 189 localized_strings->SetString("fontfamily", font_family); |
192 localized_strings->SetString("fontsize", | 190 localized_strings->SetString("fontsize", |
193 l10n_util::GetStringUTF8(web_font_size_id)); | 191 l10n_util::GetStringUTF8(web_font_size_id)); |
194 localized_strings->SetString("textdirection", | 192 localized_strings->SetString("textdirection", |
195 base::i18n::IsRTL() ? "rtl" : "ltr"); | 193 base::i18n::IsRTL() ? "rtl" : "ltr"); |
196 } | 194 } |
197 | 195 |
198 void ChromeURLDataManager::DataSource::SendResponseOnIOThread( | 196 void URLDataSource::SendResponseOnIOThread( |
199 int request_id, | 197 int request_id, |
200 scoped_refptr<base::RefCountedMemory> bytes) { | 198 scoped_refptr<base::RefCountedMemory> bytes) { |
201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
202 if (backend_) | 200 if (backend_) |
203 backend_->DataAvailable(request_id, bytes); | 201 backend_->DataAvailable(request_id, bytes); |
204 } | 202 } |
OLD | NEW |