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