Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: chrome/browser/ui/webui/nacl_ui.cc

Issue 15243002: Fix style issues in NaClDomHandlerProxy. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 source->AddLocalizedString("naclLongTitle", IDS_NACL_TITLE_MESSAGE); 56 source->AddLocalizedString("naclLongTitle", IDS_NACL_TITLE_MESSAGE);
57 source->SetJsonPath("strings.js"); 57 source->SetJsonPath("strings.js");
58 source->AddResourcePath("about_nacl.css", IDR_ABOUT_NACL_CSS); 58 source->AddResourcePath("about_nacl.css", IDR_ABOUT_NACL_CSS);
59 source->AddResourcePath("about_nacl.js", IDR_ABOUT_NACL_JS); 59 source->AddResourcePath("about_nacl.js", IDR_ABOUT_NACL_JS);
60 source->SetDefaultResource(IDR_ABOUT_NACL_HTML); 60 source->SetDefaultResource(IDR_ABOUT_NACL_HTML);
61 return source; 61 return source;
62 } 62 }
63 63
64 //////////////////////////////////////////////////////////////////////////////// 64 ////////////////////////////////////////////////////////////////////////////////
65 // 65 //
66 // NaClDOMHandler 66 // NaClDomHandler
67 // 67 //
68 //////////////////////////////////////////////////////////////////////////////// 68 ////////////////////////////////////////////////////////////////////////////////
69 69
70 class NaClDOMHandler; 70 class NaClDomHandler;
71 71
72 // This class performs a check that the PNaCl path which was returned by 72 // This class performs a check that the PNaCl path which was returned by
73 // PathService is valid. One class instance is created per NaClDOMHandler 73 // PathService is valid. One class instance is created per NaClDomHandler
74 // and it is destroyed after the check is completed. 74 // and it is destroyed after the check is completed.
75 class NaClDOMHandlerProxy : public 75 class NaClDomHandlerProxy
76 base::RefCountedThreadSafe<NaClDOMHandlerProxy> { 76 : public base::RefCountedThreadSafe<NaClDomHandlerProxy> {
77 public: 77 public:
78 explicit NaClDOMHandlerProxy(NaClDOMHandler* handler); 78 explicit NaClDomHandlerProxy(NaClDomHandler* handler);
79 79
80 // A helper to check if PNaCl path exists. 80 // A helper to check if PNaCl path exists.
81 void ValidatePnaclPath(); 81 void ValidatePnaclPath();
82 82
83 void set_handler(NaClDOMHandler* handler) { 83 void set_handler(NaClDomHandler* handler) { handler_ = handler; }
84 handler_ = handler;
85 }
86 84
87 private: 85 private:
86 friend class base::RefCountedThreadSafe<NaClDomHandlerProxy>;
87 virtual ~NaClDomHandlerProxy() {}
88
88 // A helper callback that receives the result of checking if PNaCl path 89 // A helper callback that receives the result of checking if PNaCl path
89 // exists. 90 // exists.
90 void ValidatePnaclPathCallback(bool is_valid); 91 void ValidatePnaclPathCallback(bool is_valid);
91 92
92 virtual ~NaClDOMHandlerProxy() {} 93 // The handler that requested checking PNaCl file path.
93 friend class base::RefCountedThreadSafe<NaClDOMHandlerProxy>; 94 NaClDomHandler* handler_; // weak
94 95
95 // The handler that requested checking PNaCl file path. 96 DISALLOW_COPY_AND_ASSIGN(NaClDomHandlerProxy);
96 NaClDOMHandler* handler_;
97
98 DISALLOW_COPY_AND_ASSIGN(NaClDOMHandlerProxy);
99 }; 97 };
100 98
101 // The handler for JavaScript messages for the about:flags page. 99 // The handler for JavaScript messages for the about:flags page.
102 class NaClDOMHandler : public WebUIMessageHandler { 100 class NaClDomHandler : public WebUIMessageHandler {
103 public: 101 public:
104 NaClDOMHandler(); 102 NaClDomHandler();
105 virtual ~NaClDOMHandler(); 103 virtual ~NaClDomHandler();
106 104
107 // WebUIMessageHandler implementation. 105 // WebUIMessageHandler implementation.
108 virtual void RegisterMessages() OVERRIDE; 106 virtual void RegisterMessages() OVERRIDE;
109 107
110 // Callback for the "requestNaClInfo" message. 108 // Callback for the "requestNaClInfo" message.
111 void HandleRequestNaClInfo(const ListValue* args); 109 void HandleRequestNaClInfo(const ListValue* args);
112 110
113 // Callback for the NaCl plugin information. 111 // Callback for the NaCl plugin information.
114 void OnGotPlugins(const std::vector<webkit::WebPluginInfo>& plugins); 112 void OnGotPlugins(const std::vector<webkit::WebPluginInfo>& plugins);
115 113
116 // A helper callback that receives the result of checking if PNaCl path 114 // A helper callback that receives the result of checking if PNaCl path
117 // exists. is_valid is true if the PNaCl path that was returned by 115 // exists. |is_valid| is true if the PNaCl path that was returned by
118 // PathService is valid, and false otherwise. 116 // PathService is valid, and false otherwise.
119 void DidValidatePnaclPath(bool is_valid); 117 void DidValidatePnaclPath(bool is_valid);
120 118
121 private: 119 private:
122 // Called when enough information is gathered to return data back to the page. 120 // Called when enough information is gathered to return data back to the page.
123 void MaybeRespondToPage(); 121 void MaybeRespondToPage();
124 122
125 // Helper for MaybeRespondToPage -- called after enough information 123 // Helper for MaybeRespondToPage -- called after enough information
126 // is gathered. 124 // is gathered.
127 void PopulatePageInformation(DictionaryValue* naclInfo); 125 void PopulatePageInformation(DictionaryValue* naclInfo);
128 126
129 // Factory for the creating refs in callbacks. 127 // Factory for the creating refs in callbacks.
130 base::WeakPtrFactory<NaClDOMHandler> weak_ptr_factory_; 128 base::WeakPtrFactory<NaClDomHandler> weak_ptr_factory_;
131 129
132 // Whether the page has requested data. 130 // Whether the page has requested data.
133 bool page_has_requested_data_; 131 bool page_has_requested_data_;
134 132
135 // Whether the plugin information is ready. 133 // Whether the plugin information is ready.
136 bool has_plugin_info_; 134 bool has_plugin_info_;
137 135
138 // Whether PNaCl path was validated. PathService can return a path 136 // Whether PNaCl path was validated. PathService can return a path
139 // that does not exists, so it needs to be validated. 137 // that does not exists, so it needs to be validated.
140 bool pnacl_path_validated_; 138 bool pnacl_path_validated_;
141 bool pnacl_path_exists_; 139 bool pnacl_path_exists_;
142 140
143 // A proxy for handling cross threads messages. 141 // A proxy for handling cross threads messages.
144 scoped_refptr<NaClDOMHandlerProxy> proxy_; 142 scoped_refptr<NaClDomHandlerProxy> proxy_;
145 143
146 DISALLOW_COPY_AND_ASSIGN(NaClDOMHandler); 144 DISALLOW_COPY_AND_ASSIGN(NaClDomHandler);
147 }; 145 };
148 146
149 NaClDOMHandlerProxy::NaClDOMHandlerProxy(NaClDOMHandler* handler) 147 NaClDomHandlerProxy::NaClDomHandlerProxy(NaClDomHandler* handler)
150 : handler_(handler) { 148 : handler_(handler) {
151 } 149 }
152 150
153 void NaClDOMHandlerProxy::ValidatePnaclPath() { 151 void NaClDomHandlerProxy::ValidatePnaclPath() {
154 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { 152 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
155 BrowserThread::PostTask( 153 BrowserThread::PostTask(
156 BrowserThread::FILE, FROM_HERE, 154 BrowserThread::FILE, FROM_HERE,
157 base::Bind(&NaClDOMHandlerProxy::ValidatePnaclPath, this)); 155 base::Bind(&NaClDomHandlerProxy::ValidatePnaclPath, this));
158 return; 156 return;
159 } 157 }
160 158
161 bool is_valid = true;
162 base::FilePath pnacl_path; 159 base::FilePath pnacl_path;
163 bool got_path = PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_path); 160 bool got_path = PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_path);
164 // The PathService may return an empty string if PNaCl is not yet installed. 161 // The PathService may return an empty string if PNaCl is not yet installed.
165 // However, do not trust that the path returned by the PathService exists. 162 // However, do not trust that the path returned by the PathService exists.
166 // Check for existence here. 163 // Check for existence here.
167 if (!got_path || pnacl_path.empty() || !file_util::PathExists(pnacl_path)) { 164 ValidatePnaclPathCallback(
168 is_valid = false; 165 got_path && !pnacl_path.empty() && file_util::PathExists(pnacl_path));
169 }
170 ValidatePnaclPathCallback(is_valid);
171 } 166 }
172 167
173 void NaClDOMHandlerProxy::ValidatePnaclPathCallback(bool is_valid) { 168 void NaClDomHandlerProxy::ValidatePnaclPathCallback(bool is_valid) {
174 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { 169 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
175 BrowserThread::PostTask( 170 BrowserThread::PostTask(
176 BrowserThread::UI, FROM_HERE, 171 BrowserThread::UI, FROM_HERE,
177 base::Bind(&NaClDOMHandlerProxy::ValidatePnaclPathCallback, 172 base::Bind(&NaClDomHandlerProxy::ValidatePnaclPathCallback,
178 this, is_valid)); 173 this, is_valid));
179 return; 174 return;
180 } 175 }
181 176
182 // Check that handler_ is still valid, it could be set to NULL if navigation 177 // Check that handler_ is still valid, it could be set to NULL if navigation
183 // happened while checking that the PNaCl file exists. 178 // happened while checking that the PNaCl file exists.
184 if (handler_) 179 if (handler_)
185 handler_->DidValidatePnaclPath(is_valid); 180 handler_->DidValidatePnaclPath(is_valid);
186 } 181 }
187 182
188 NaClDOMHandler::NaClDOMHandler() 183 NaClDomHandler::NaClDomHandler()
189 : weak_ptr_factory_(this), 184 : weak_ptr_factory_(this),
190 page_has_requested_data_(false), 185 page_has_requested_data_(false),
191 has_plugin_info_(false), 186 has_plugin_info_(false),
192 pnacl_path_validated_(false), 187 pnacl_path_validated_(false),
193 pnacl_path_exists_(false), 188 pnacl_path_exists_(false),
194 proxy_(new NaClDOMHandlerProxy(this)) { 189 proxy_(new NaClDomHandlerProxy(this)) {
195 PluginService::GetInstance()->GetPlugins(base::Bind( 190 PluginService::GetInstance()->GetPlugins(base::Bind(
196 &NaClDOMHandler::OnGotPlugins, weak_ptr_factory_.GetWeakPtr())); 191 &NaClDomHandler::OnGotPlugins, weak_ptr_factory_.GetWeakPtr()));
197 } 192 }
198 193
199 NaClDOMHandler::~NaClDOMHandler() { 194 NaClDomHandler::~NaClDomHandler() {
200 if (proxy_) 195 if (proxy_)
201 proxy_->set_handler(NULL); 196 proxy_->set_handler(NULL);
202 } 197 }
203 198
204 void NaClDOMHandler::RegisterMessages() { 199 void NaClDomHandler::RegisterMessages() {
205 web_ui()->RegisterMessageCallback( 200 web_ui()->RegisterMessageCallback(
206 "requestNaClInfo", 201 "requestNaClInfo",
207 base::Bind(&NaClDOMHandler::HandleRequestNaClInfo, 202 base::Bind(&NaClDomHandler::HandleRequestNaClInfo,
208 base::Unretained(this))); 203 base::Unretained(this)));
209 } 204 }
210 205
211 // Helper functions for collecting a list of key-value pairs that will 206 // Helper functions for collecting a list of key-value pairs that will
212 // be displayed. 207 // be displayed.
213 void AddPair(ListValue* list, const string16& key, const string16& value) { 208 void AddPair(ListValue* list, const string16& key, const string16& value) {
214 DictionaryValue* results = new DictionaryValue(); 209 DictionaryValue* results = new DictionaryValue();
215 results->SetString("key", key); 210 results->SetString("key", key);
216 results->SetString("value", value); 211 results->SetString("value", value);
217 list->Append(results); 212 list->Append(results);
218 } 213 }
219 214
220 // Generate an empty data-pair which acts as a line break. 215 // Generate an empty data-pair which acts as a line break.
221 void AddLineBreak(ListValue* list) { 216 void AddLineBreak(ListValue* list) {
222 AddPair(list, ASCIIToUTF16(""), ASCIIToUTF16("")); 217 AddPair(list, ASCIIToUTF16(""), ASCIIToUTF16(""));
223 } 218 }
224 219
225 // Check whether a commandline switch is turned on or off. 220 // Check whether a commandline switch is turned on or off.
226 void ListFlagStatus(ListValue* list, const std::string& flag_label, 221 void ListFlagStatus(ListValue* list, const std::string& flag_label,
227 const std::string& flag_name) { 222 const std::string& flag_name) {
228 if (CommandLine::ForCurrentProcess()->HasSwitch(flag_name)) 223 if (CommandLine::ForCurrentProcess()->HasSwitch(flag_name))
229 AddPair(list, ASCIIToUTF16(flag_label), ASCIIToUTF16("On")); 224 AddPair(list, ASCIIToUTF16(flag_label), ASCIIToUTF16("On"));
230 else 225 else
231 AddPair(list, ASCIIToUTF16(flag_label), ASCIIToUTF16("Off")); 226 AddPair(list, ASCIIToUTF16(flag_label), ASCIIToUTF16("Off"));
232 } 227 }
233 228
234 void NaClDOMHandler::HandleRequestNaClInfo(const ListValue* args) { 229 void NaClDomHandler::HandleRequestNaClInfo(const ListValue* args) {
235 page_has_requested_data_ = true; 230 page_has_requested_data_ = true;
236 MaybeRespondToPage(); 231 MaybeRespondToPage();
237 } 232 }
238 233
239 void NaClDOMHandler::OnGotPlugins( 234 void NaClDomHandler::OnGotPlugins(
240 const std::vector<webkit::WebPluginInfo>& plugins) { 235 const std::vector<webkit::WebPluginInfo>& plugins) {
241 has_plugin_info_ = true; 236 has_plugin_info_ = true;
242 MaybeRespondToPage(); 237 MaybeRespondToPage();
243 } 238 }
244 239
245 void NaClDOMHandler::PopulatePageInformation(DictionaryValue* naclInfo) { 240 void NaClDomHandler::PopulatePageInformation(DictionaryValue* naclInfo) {
246 DCHECK(pnacl_path_validated_); 241 DCHECK(pnacl_path_validated_);
247 // Store Key-Value pairs of about-information. 242 // Store Key-Value pairs of about-information.
248 scoped_ptr<ListValue> list(new ListValue()); 243 scoped_ptr<ListValue> list(new ListValue());
249 244
250 // Obtain the Chrome version info. 245 // Obtain the Chrome version info.
251 chrome::VersionInfo version_info; 246 chrome::VersionInfo version_info;
252 AddPair(list.get(), 247 AddPair(list.get(),
253 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME), 248 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
254 ASCIIToUTF16(version_info.Version() + " (" + 249 ASCIIToUTF16(version_info.Version() + " (" +
255 chrome::VersionInfo::GetVersionStringModifier() + ")")); 250 chrome::VersionInfo::GetVersionStringModifier() + ")"));
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 AddPair(list.get(), 329 AddPair(list.get(),
335 ASCIIToUTF16("PNaCl translator version"), 330 ASCIIToUTF16("PNaCl translator version"),
336 pnacl_path.BaseName().LossyDisplayName()); 331 pnacl_path.BaseName().LossyDisplayName());
337 } 332 }
338 333
339 ListFlagStatus(list.get(), "Flag '--enable-pnacl'", switches::kEnablePnacl); 334 ListFlagStatus(list.get(), "Flag '--enable-pnacl'", switches::kEnablePnacl);
340 // naclInfo will take ownership of list, and clean it up on destruction. 335 // naclInfo will take ownership of list, and clean it up on destruction.
341 naclInfo->Set("naclInfo", list.release()); 336 naclInfo->Set("naclInfo", list.release());
342 } 337 }
343 338
344 void NaClDOMHandler::DidValidatePnaclPath(bool is_valid) { 339 void NaClDomHandler::DidValidatePnaclPath(bool is_valid) {
345 pnacl_path_validated_ = true; 340 pnacl_path_validated_ = true;
346 pnacl_path_exists_ = is_valid; 341 pnacl_path_exists_ = is_valid;
347 MaybeRespondToPage(); 342 MaybeRespondToPage();
348 } 343 }
349 344
350 void NaClDOMHandler::MaybeRespondToPage() { 345 void NaClDomHandler::MaybeRespondToPage() {
351 // Don't reply until everything is ready. The page will show a 'loading' 346 // Don't reply until everything is ready. The page will show a 'loading'
352 // message until then. 347 // message until then.
353 if (!page_has_requested_data_ || !has_plugin_info_) 348 if (!page_has_requested_data_ || !has_plugin_info_)
354 return; 349 return;
355 350
356 if (!pnacl_path_validated_) { 351 if (!pnacl_path_validated_) {
357 DCHECK(proxy_); 352 DCHECK(proxy_);
358 proxy_->ValidatePnaclPath(); 353 proxy_->ValidatePnaclPath();
359 return; 354 return;
360 } 355 }
361 356
362 DictionaryValue naclInfo; 357 DictionaryValue naclInfo;
363 PopulatePageInformation(&naclInfo); 358 PopulatePageInformation(&naclInfo);
364 web_ui()->CallJavascriptFunction("nacl.returnNaClInfo", naclInfo); 359 web_ui()->CallJavascriptFunction("nacl.returnNaClInfo", naclInfo);
365 } 360 }
366 361
367 } // namespace 362 } // namespace
368 363
369 /////////////////////////////////////////////////////////////////////////////// 364 ///////////////////////////////////////////////////////////////////////////////
370 // 365 //
371 // NaClUI 366 // NaClUI
372 // 367 //
373 /////////////////////////////////////////////////////////////////////////////// 368 ///////////////////////////////////////////////////////////////////////////////
374 369
375 NaClUI::NaClUI(content::WebUI* web_ui) : WebUIController(web_ui) { 370 NaClUI::NaClUI(content::WebUI* web_ui) : WebUIController(web_ui) {
376 content::RecordAction(UserMetricsAction("ViewAboutNaCl")); 371 content::RecordAction(UserMetricsAction("ViewAboutNaCl"));
377 372
378 web_ui->AddMessageHandler(new NaClDOMHandler()); 373 web_ui->AddMessageHandler(new NaClDomHandler());
379 374
380 // Set up the about:nacl source. 375 // Set up the about:nacl source.
381 Profile* profile = Profile::FromWebUI(web_ui); 376 Profile* profile = Profile::FromWebUI(web_ui);
382 content::WebUIDataSource::Add(profile, CreateNaClUIHTMLSource()); 377 content::WebUIDataSource::Add(profile, CreateNaClUIHTMLSource());
383 } 378 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698