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