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" | |
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_) { | 102 URLDataSourceImpl* source_impl = |
115 delegate->url_data_source_ = new URLDataSource( | 103 new URLDataSourceImpl(source->GetSource(), source); |
116 delegate->GetSource(), delegate); | |
117 } | |
118 ChromeURLDataManagerFactory::GetForProfile(profile)->AddDataSource( | 104 ChromeURLDataManagerFactory::GetForProfile(profile)->AddDataSource( |
Evan Stade
2013/01/15 20:49:34
combine these lines?
jam
2013/01/15 21:09:08
that doesn't fit
Evan Stade
2013/01/15 21:33:43
sorry, what I meant was to inline source_impl
jam
2013/01/15 21:39:47
Done.
| |
119 delegate->url_data_source_); | 105 source_impl); |
106 } | |
107 | |
108 // static | |
109 void ChromeURLDataManager::AddDataSourceImpl(Profile* profile, | |
110 URLDataSourceImpl* source) { | |
111 ChromeURLDataManagerFactory::GetForProfile(profile)->AddDataSource(source); | |
120 } | 112 } |
121 | 113 |
122 // static | 114 // static |
123 bool ChromeURLDataManager::IsScheduledForDeletion( | 115 bool ChromeURLDataManager::IsScheduledForDeletion( |
124 const URLDataSource* data_source) { | 116 const URLDataSourceImpl* data_source) { |
125 base::AutoLock lock(g_delete_lock.Get()); | 117 base::AutoLock lock(g_delete_lock.Get()); |
126 if (!data_sources_) | 118 if (!data_sources_) |
127 return false; | 119 return false; |
128 return std::find(data_sources_->begin(), data_sources_->end(), data_source) != | 120 return std::find(data_sources_->begin(), data_sources_->end(), data_source) != |
129 data_sources_->end(); | 121 data_sources_->end(); |
130 } | 122 } |
131 | 123 |
132 URLDataSource::URLDataSource(const std::string& source_name, | 124 URLDataSourceImpl::URLDataSourceImpl(const std::string& source_name, |
133 content::URLDataSourceDelegate* delegate) | 125 content::URLDataSource* source) |
134 : source_name_(source_name), | 126 : source_name_(source_name), |
135 backend_(NULL), | 127 backend_(NULL), |
136 delegate_(delegate) { | 128 source_(source) { |
137 } | 129 } |
138 | 130 |
139 URLDataSource::~URLDataSource() { | 131 URLDataSourceImpl::~URLDataSourceImpl() { |
140 } | 132 } |
141 | 133 |
142 void URLDataSource::SendResponse( | 134 void URLDataSourceImpl::SendResponse( |
143 int request_id, | 135 int request_id, |
144 base::RefCountedMemory* bytes) { | 136 base::RefCountedMemory* bytes) { |
145 // Take a ref-pointer on entry so byte->Release() will always get called. | 137 // Take a ref-pointer on entry so byte->Release() will always get called. |
146 scoped_refptr<base::RefCountedMemory> bytes_ptr(bytes); | 138 scoped_refptr<base::RefCountedMemory> bytes_ptr(bytes); |
147 if (ChromeURLDataManager::IsScheduledForDeletion(this)) { | 139 if (ChromeURLDataManager::IsScheduledForDeletion(this)) { |
148 // We're scheduled for deletion. Servicing the request would result in | 140 // 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 | 141 // 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 | 142 // about to be deleted. If the AddRef were allowed through, when 'this' is |
151 // released it would be deleted again. | 143 // released it would be deleted again. |
152 // | 144 // |
153 // This scenario occurs with DataSources that make history requests. Such | 145 // This scenario occurs with DataSources that make history requests. Such |
154 // DataSources do a history query in |StartDataRequest| and the request is | 146 // 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 | 147 // 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 | 148 // 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 | 149 // |SendResponse| between the time when there are no more refs and the time |
158 // when the object is deleted. | 150 // when the object is deleted. |
159 return; | 151 return; |
160 } | 152 } |
161 BrowserThread::PostTask( | 153 BrowserThread::PostTask( |
162 BrowserThread::IO, FROM_HERE, | 154 BrowserThread::IO, FROM_HERE, |
163 base::Bind(&URLDataSource::SendResponseOnIOThread, this, request_id, | 155 base::Bind(&URLDataSourceImpl::SendResponseOnIOThread, this, request_id, |
164 bytes_ptr)); | 156 bytes_ptr)); |
165 } | 157 } |
166 | 158 |
167 // static | 159 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, | 160 int request_id, |
198 scoped_refptr<base::RefCountedMemory> bytes) { | 161 scoped_refptr<base::RefCountedMemory> bytes) { |
199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
200 if (backend_) | 163 if (backend_) |
201 backend_->DataAvailable(request_id, bytes); | 164 backend_->DataAvailable(request_id, bytes); |
202 } | 165 } |
OLD | NEW |