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/nacl_ui.h" | 5 #include "chrome/browser/ui/webui/nacl_ui.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 22 matching lines...) Expand all Loading... | |
33 #include "grit/generated_resources.h" | 33 #include "grit/generated_resources.h" |
34 #include "grit/theme_resources.h" | 34 #include "grit/theme_resources.h" |
35 #include "ui/base/l10n/l10n_util.h" | 35 #include "ui/base/l10n/l10n_util.h" |
36 #include "ui/base/resource/resource_bundle.h" | 36 #include "ui/base/resource/resource_bundle.h" |
37 #include "webkit/plugins/webplugininfo.h" | 37 #include "webkit/plugins/webplugininfo.h" |
38 | 38 |
39 #if defined(OS_WIN) | 39 #if defined(OS_WIN) |
40 #include "base/win/windows_version.h" | 40 #include "base/win/windows_version.h" |
41 #endif | 41 #endif |
42 | 42 |
43 using content::BrowserThread; | |
43 using content::PluginService; | 44 using content::PluginService; |
44 using content::UserMetricsAction; | 45 using content::UserMetricsAction; |
45 using content::WebUIMessageHandler; | 46 using content::WebUIMessageHandler; |
46 | 47 |
47 namespace { | 48 namespace { |
48 | 49 |
49 content::WebUIDataSource* CreateNaClUIHTMLSource() { | 50 content::WebUIDataSource* CreateNaClUIHTMLSource() { |
50 content::WebUIDataSource* source = | 51 content::WebUIDataSource* source = |
51 content::WebUIDataSource::Create(chrome::kChromeUINaClHost); | 52 content::WebUIDataSource::Create(chrome::kChromeUINaClHost); |
52 | 53 |
53 source->SetUseJsonJSFormatV2(); | 54 source->SetUseJsonJSFormatV2(); |
54 source->AddLocalizedString("loadingMessage", IDS_NACL_LOADING_MESSAGE); | 55 source->AddLocalizedString("loadingMessage", IDS_NACL_LOADING_MESSAGE); |
55 source->AddLocalizedString("naclLongTitle", IDS_NACL_TITLE_MESSAGE); | 56 source->AddLocalizedString("naclLongTitle", IDS_NACL_TITLE_MESSAGE); |
56 source->SetJsonPath("strings.js"); | 57 source->SetJsonPath("strings.js"); |
57 source->AddResourcePath("about_nacl.css", IDR_ABOUT_NACL_CSS); | 58 source->AddResourcePath("about_nacl.css", IDR_ABOUT_NACL_CSS); |
58 source->AddResourcePath("about_nacl.js", IDR_ABOUT_NACL_JS); | 59 source->AddResourcePath("about_nacl.js", IDR_ABOUT_NACL_JS); |
59 source->SetDefaultResource(IDR_ABOUT_NACL_HTML); | 60 source->SetDefaultResource(IDR_ABOUT_NACL_HTML); |
60 return source; | 61 return source; |
61 } | 62 } |
62 | 63 |
63 //////////////////////////////////////////////////////////////////////////////// | 64 //////////////////////////////////////////////////////////////////////////////// |
64 // | 65 // |
65 // NaClDOMHandler | 66 // NaClDOMHandler |
66 // | 67 // |
67 //////////////////////////////////////////////////////////////////////////////// | 68 //////////////////////////////////////////////////////////////////////////////// |
68 | 69 |
70 class NaClDOMHandler; | |
71 | |
72 class NaClDOMHandlerProxy : public | |
73 base::RefCountedThreadSafe<NaClDOMHandlerProxy> { | |
74 public: | |
75 explicit NaClDOMHandlerProxy(NaClDOMHandler* handler); | |
76 // A helper to check if PNaCl path exists. | |
77 // The check is done on the FILE thread. | |
78 void ValidatePnaclPath(); | |
79 | |
80 void set_handler(NaClDOMHandler* handler){ | |
jvoung (off chromium)
2013/05/15 21:43:25
space between ) and {
| |
81 handler_ = handler; | |
82 } | |
83 | |
84 private: | |
85 // A helper callback that receives the result of checking if PNaCl path | |
86 // exists. It switches back to the UI thread and saves the result. | |
87 void ValidatePnaclPathCallback(bool is_valid); | |
88 | |
89 NaClDOMHandler* handler_; | |
90 | |
91 DISALLOW_COPY_AND_ASSIGN(NaClDOMHandlerProxy); | |
92 }; | |
93 | |
69 // The handler for JavaScript messages for the about:flags page. | 94 // The handler for JavaScript messages for the about:flags page. |
70 class NaClDOMHandler : public WebUIMessageHandler { | 95 class NaClDOMHandler : public WebUIMessageHandler { |
71 public: | 96 public: |
72 NaClDOMHandler(); | 97 NaClDOMHandler(); |
73 virtual ~NaClDOMHandler(); | 98 virtual ~NaClDOMHandler(); |
74 | 99 |
75 // WebUIMessageHandler implementation. | 100 // WebUIMessageHandler implementation. |
76 virtual void RegisterMessages() OVERRIDE; | 101 virtual void RegisterMessages() OVERRIDE; |
77 | 102 |
78 // Callback for the "requestNaClInfo" message. | 103 // Callback for the "requestNaClInfo" message. |
79 void HandleRequestNaClInfo(const ListValue* args); | 104 void HandleRequestNaClInfo(const ListValue* args); |
80 | 105 |
81 // Callback for the NaCl plugin information. | 106 // Callback for the NaCl plugin information. |
82 void OnGotPlugins(const std::vector<webkit::WebPluginInfo>& plugins); | 107 void OnGotPlugins(const std::vector<webkit::WebPluginInfo>& plugins); |
83 | 108 |
109 // A helper callback that receives the result of checking if PNaCl path | |
110 // exists. It switches back to the UI thread and saves the result. | |
jvoung (off chromium)
2013/05/15 21:43:25
Update comment: this one doesn't try to switch ba
| |
111 void DidValidatePnaclPath(bool is_valid); | |
112 | |
84 private: | 113 private: |
85 // Called when enough information is gathered to return data back to the page. | 114 // Called when enough information is gathered to return data back to the page. |
86 void MaybeRespondToPage(); | 115 void MaybeRespondToPage(); |
87 | 116 |
88 // Helper for MaybeRespondToPage -- called after enough information | 117 // Helper for MaybeRespondToPage -- called after enough information |
89 // is gathered. | 118 // is gathered. |
90 void PopulatePageInformation(DictionaryValue* naclInfo); | 119 void PopulatePageInformation(DictionaryValue* naclInfo); |
91 | 120 |
92 // Factory for the creating refs in callbacks. | 121 // Factory for the creating refs in callbacks. |
93 base::WeakPtrFactory<NaClDOMHandler> weak_ptr_factory_; | 122 base::WeakPtrFactory<NaClDOMHandler> weak_ptr_factory_; |
94 | 123 |
95 // Whether the page has requested data. | 124 // Whether the page has requested data. |
96 bool page_has_requested_data_; | 125 bool page_has_requested_data_; |
97 | 126 |
98 // Whether the plugin information is ready. | 127 // Whether the plugin information is ready. |
99 bool has_plugin_info_; | 128 bool has_plugin_info_; |
100 | 129 |
130 // Whether PNaCl path was validated. PathService can return a path | |
131 // that does not exists, so it needs to be validated. | |
132 bool pnacl_path_validated_; | |
133 bool pnacl_path_exists_; | |
134 | |
135 // A proxy for handling cross threads messages. | |
136 scoped_refptr<NaClDOMHandlerProxy> proxy_; | |
137 | |
101 DISALLOW_COPY_AND_ASSIGN(NaClDOMHandler); | 138 DISALLOW_COPY_AND_ASSIGN(NaClDOMHandler); |
102 }; | 139 }; |
103 | 140 |
141 NaClDOMHandlerProxy::NaClDOMHandlerProxy(NaClDOMHandler* handler) | |
142 : handler_(handler) { | |
143 } | |
144 | |
145 void NaClDOMHandlerProxy::ValidatePnaclPath() { | |
146 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | |
147 BrowserThread::PostTask( | |
148 BrowserThread::FILE, FROM_HERE, | |
149 base::Bind(&NaClDOMHandlerProxy::ValidatePnaclPath, this)); | |
150 return; | |
151 } | |
152 | |
153 bool is_valid = true; | |
154 base::FilePath pnacl_path; | |
155 bool got_path = PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_path); | |
156 // The PathService may return an empty string if PNaCl is not yet installed. | |
157 // However, do not trust that the path returned by the PathService exists. | |
158 // Check for existence here. | |
159 if (!got_path || pnacl_path.empty() || !file_util::PathExists(pnacl_path)) { | |
160 is_valid = false; | |
161 } | |
162 ValidatePnaclPathCallback(is_valid); | |
163 } | |
164 | |
165 void NaClDOMHandlerProxy::ValidatePnaclPathCallback(bool is_valid) { | |
166 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
167 BrowserThread::PostTask( | |
168 BrowserThread::UI, FROM_HERE, | |
169 base::Bind(&NaClDOMHandlerProxy::ValidatePnaclPathCallback, | |
170 this, is_valid)); | |
171 return; | |
172 } | |
173 | |
174 handler_->DidValidatePnaclPath(is_valid); | |
175 } | |
176 | |
104 NaClDOMHandler::NaClDOMHandler() | 177 NaClDOMHandler::NaClDOMHandler() |
105 : weak_ptr_factory_(this), | 178 : weak_ptr_factory_(this), |
106 page_has_requested_data_(false), | 179 page_has_requested_data_(false), |
107 has_plugin_info_(false) { | 180 has_plugin_info_(false), |
181 pnacl_path_validated_(false), | |
182 pnacl_path_exists_(false), | |
183 proxy_(new NaClDOMHandlerProxy(this)) { | |
108 PluginService::GetInstance()->GetPlugins(base::Bind( | 184 PluginService::GetInstance()->GetPlugins(base::Bind( |
109 &NaClDOMHandler::OnGotPlugins, weak_ptr_factory_.GetWeakPtr())); | 185 &NaClDOMHandler::OnGotPlugins, weak_ptr_factory_.GetWeakPtr())); |
110 } | 186 } |
111 | 187 |
112 NaClDOMHandler::~NaClDOMHandler() { | 188 NaClDOMHandler::~NaClDOMHandler() { |
189 if (proxy_) { | |
190 proxy_->set_handler(NULL); | |
191 } | |
113 } | 192 } |
114 | 193 |
115 void NaClDOMHandler::RegisterMessages() { | 194 void NaClDOMHandler::RegisterMessages() { |
116 web_ui()->RegisterMessageCallback( | 195 web_ui()->RegisterMessageCallback( |
117 "requestNaClInfo", | 196 "requestNaClInfo", |
118 base::Bind(&NaClDOMHandler::HandleRequestNaClInfo, | 197 base::Bind(&NaClDOMHandler::HandleRequestNaClInfo, |
119 base::Unretained(this))); | 198 base::Unretained(this))); |
120 } | 199 } |
121 | 200 |
122 // Helper functions for collecting a list of key-value pairs that will | 201 // Helper functions for collecting a list of key-value pairs that will |
(...skipping 24 matching lines...) Expand all Loading... | |
147 MaybeRespondToPage(); | 226 MaybeRespondToPage(); |
148 } | 227 } |
149 | 228 |
150 void NaClDOMHandler::OnGotPlugins( | 229 void NaClDOMHandler::OnGotPlugins( |
151 const std::vector<webkit::WebPluginInfo>& plugins) { | 230 const std::vector<webkit::WebPluginInfo>& plugins) { |
152 has_plugin_info_ = true; | 231 has_plugin_info_ = true; |
153 MaybeRespondToPage(); | 232 MaybeRespondToPage(); |
154 } | 233 } |
155 | 234 |
156 void NaClDOMHandler::PopulatePageInformation(DictionaryValue* naclInfo) { | 235 void NaClDOMHandler::PopulatePageInformation(DictionaryValue* naclInfo) { |
236 DCHECK(pnacl_path_validated_); | |
157 // Store Key-Value pairs of about-information. | 237 // Store Key-Value pairs of about-information. |
158 scoped_ptr<ListValue> list(new ListValue()); | 238 scoped_ptr<ListValue> list(new ListValue()); |
159 | 239 |
160 // Obtain the Chrome version info. | 240 // Obtain the Chrome version info. |
161 chrome::VersionInfo version_info; | 241 chrome::VersionInfo version_info; |
162 AddPair(list.get(), | 242 AddPair(list.get(), |
163 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME), | 243 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME), |
164 ASCIIToUTF16(version_info.Version() + " (" + | 244 ASCIIToUTF16(version_info.Version() + " (" + |
165 chrome::VersionInfo::GetVersionStringModifier() + ")")); | 245 chrome::VersionInfo::GetVersionStringModifier() + ")")); |
166 | 246 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 } | 306 } |
227 | 307 |
228 // Check that commandline flags are enabled. | 308 // Check that commandline flags are enabled. |
229 ListFlagStatus(list.get(), "Flag '--enable-nacl'", switches::kEnableNaCl); | 309 ListFlagStatus(list.get(), "Flag '--enable-nacl'", switches::kEnableNaCl); |
230 | 310 |
231 AddLineBreak(list.get()); | 311 AddLineBreak(list.get()); |
232 | 312 |
233 // Obtain the version of the PNaCl translator. | 313 // Obtain the version of the PNaCl translator. |
234 base::FilePath pnacl_path; | 314 base::FilePath pnacl_path; |
235 bool got_path = PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_path); | 315 bool got_path = PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_path); |
236 // The PathService may return an empty string if PNaCl is not yet installed. | 316 if (!got_path || pnacl_path.empty() || !pnacl_path_exists_) { |
237 // However, do not trust that the path returned by the PathService exists. | |
238 // Check for existence here. | |
239 if (!got_path || pnacl_path.empty() || !file_util::PathExists(pnacl_path)) { | |
240 AddPair(list.get(), | 317 AddPair(list.get(), |
241 ASCIIToUTF16("PNaCl translator"), | 318 ASCIIToUTF16("PNaCl translator"), |
242 ASCIIToUTF16("Not installed")); | 319 ASCIIToUTF16("Not installed")); |
243 } else { | 320 } else { |
244 AddPair(list.get(), | 321 AddPair(list.get(), |
245 ASCIIToUTF16("PNaCl translator path"), | 322 ASCIIToUTF16("PNaCl translator path"), |
246 pnacl_path.LossyDisplayName()); | 323 pnacl_path.LossyDisplayName()); |
247 AddPair(list.get(), | 324 AddPair(list.get(), |
248 ASCIIToUTF16("PNaCl translator version"), | 325 ASCIIToUTF16("PNaCl translator version"), |
249 pnacl_path.BaseName().LossyDisplayName()); | 326 pnacl_path.BaseName().LossyDisplayName()); |
250 } | 327 } |
251 | 328 |
252 ListFlagStatus(list.get(), "Flag '--enable-pnacl'", switches::kEnablePnacl); | 329 ListFlagStatus(list.get(), "Flag '--enable-pnacl'", switches::kEnablePnacl); |
253 // naclInfo will take ownership of list, and clean it up on destruction. | 330 // naclInfo will take ownership of list, and clean it up on destruction. |
254 naclInfo->Set("naclInfo", list.release()); | 331 naclInfo->Set("naclInfo", list.release()); |
255 } | 332 } |
256 | 333 |
334 void NaClDOMHandler::DidValidatePnaclPath(bool is_valid) { | |
335 pnacl_path_validated_ = true; | |
336 pnacl_path_exists_ = is_valid; | |
337 MaybeRespondToPage(); | |
338 } | |
339 | |
257 void NaClDOMHandler::MaybeRespondToPage() { | 340 void NaClDOMHandler::MaybeRespondToPage() { |
258 // Don't reply until everything is ready. The page will show a 'loading' | 341 // Don't reply until everything is ready. The page will show a 'loading' |
259 // message until then. | 342 // message until then. |
260 if (!page_has_requested_data_ || !has_plugin_info_) | 343 if (!page_has_requested_data_ || !has_plugin_info_) |
261 return; | 344 return; |
262 | 345 |
346 if (!pnacl_path_validated_) { | |
347 DCHECK(proxy_); | |
348 proxy_->ValidatePnaclPath(); | |
349 return; | |
350 } | |
351 | |
263 DictionaryValue naclInfo; | 352 DictionaryValue naclInfo; |
264 PopulatePageInformation(&naclInfo); | 353 PopulatePageInformation(&naclInfo); |
265 web_ui()->CallJavascriptFunction("nacl.returnNaClInfo", naclInfo); | 354 web_ui()->CallJavascriptFunction("nacl.returnNaClInfo", naclInfo); |
266 } | 355 } |
267 | 356 |
268 } // namespace | 357 } // namespace |
269 | 358 |
270 /////////////////////////////////////////////////////////////////////////////// | 359 /////////////////////////////////////////////////////////////////////////////// |
271 // | 360 // |
272 // NaClUI | 361 // NaClUI |
273 // | 362 // |
274 /////////////////////////////////////////////////////////////////////////////// | 363 /////////////////////////////////////////////////////////////////////////////// |
275 | 364 |
276 NaClUI::NaClUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 365 NaClUI::NaClUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
277 content::RecordAction(UserMetricsAction("ViewAboutNaCl")); | 366 content::RecordAction(UserMetricsAction("ViewAboutNaCl")); |
278 | 367 |
279 web_ui->AddMessageHandler(new NaClDOMHandler()); | 368 web_ui->AddMessageHandler(new NaClDOMHandler()); |
280 | 369 |
281 // Set up the about:nacl source. | 370 // Set up the about:nacl source. |
282 Profile* profile = Profile::FromWebUI(web_ui); | 371 Profile* profile = Profile::FromWebUI(web_ui); |
283 content::WebUIDataSource::Add(profile, CreateNaClUIHTMLSource()); | 372 content::WebUIDataSource::Add(profile, CreateNaClUIHTMLSource()); |
284 } | 373 } |
OLD | NEW |