OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/chromeos/app_mode/kiosk_app_data.h" | 5 #include "chrome/browser/chromeos/app_mode/kiosk_app_data.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 | 239 |
240 base::WeakPtr<KioskAppData> client_; | 240 base::WeakPtr<KioskAppData> client_; |
241 | 241 |
242 DISALLOW_COPY_AND_ASSIGN(WebstoreDataParser); | 242 DISALLOW_COPY_AND_ASSIGN(WebstoreDataParser); |
243 }; | 243 }; |
244 | 244 |
245 //////////////////////////////////////////////////////////////////////////////// | 245 //////////////////////////////////////////////////////////////////////////////// |
246 // KioskAppData | 246 // KioskAppData |
247 | 247 |
248 KioskAppData::KioskAppData(KioskAppDataDelegate* delegate, | 248 KioskAppData::KioskAppData(KioskAppDataDelegate* delegate, |
249 const std::string& app_id) | 249 const std::string& app_id, |
| 250 const std::string& user_id) |
250 : delegate_(delegate), | 251 : delegate_(delegate), |
251 status_(STATUS_INIT), | 252 status_(STATUS_INIT), |
252 id_(app_id) { | 253 app_id_(app_id), |
| 254 user_id_(user_id) { |
253 } | 255 } |
254 | 256 |
255 KioskAppData::~KioskAppData() {} | 257 KioskAppData::~KioskAppData() {} |
256 | 258 |
257 void KioskAppData::Load() { | 259 void KioskAppData::Load() { |
258 SetStatus(STATUS_LOADING); | 260 SetStatus(STATUS_LOADING); |
259 | 261 |
260 if (LoadFromCache()) | 262 if (LoadFromCache()) |
261 return; | 263 return; |
262 | 264 |
263 StartFetch(); | 265 StartFetch(); |
264 } | 266 } |
265 | 267 |
266 void KioskAppData::ClearCache() { | 268 void KioskAppData::ClearCache() { |
267 PrefService* local_state = g_browser_process->local_state(); | 269 PrefService* local_state = g_browser_process->local_state(); |
268 | 270 |
269 DictionaryPrefUpdate dict_update(local_state, | 271 DictionaryPrefUpdate dict_update(local_state, |
270 KioskAppManager::kKioskDictionaryName); | 272 KioskAppManager::kKioskDictionaryName); |
271 | 273 |
272 std::string app_key = std::string(KioskAppManager::kKeyApps) + '.' + id_; | 274 std::string app_key = std::string(KioskAppManager::kKeyApps) + '.' + app_id_; |
273 dict_update->Remove(app_key, NULL); | 275 dict_update->Remove(app_key, NULL); |
274 | 276 |
275 if (!icon_path_.empty()) { | 277 if (!icon_path_.empty()) { |
276 BrowserThread::PostBlockingPoolTask( | 278 BrowserThread::PostBlockingPoolTask( |
277 FROM_HERE, | 279 FROM_HERE, |
278 base::Bind(base::IgnoreResult(&file_util::Delete), icon_path_, false)); | 280 base::Bind(base::IgnoreResult(&file_util::Delete), icon_path_, false)); |
279 } | 281 } |
280 } | 282 } |
281 | 283 |
282 bool KioskAppData::IsLoading() const { | 284 bool KioskAppData::IsLoading() const { |
283 return status_ == STATUS_LOADING; | 285 return status_ == STATUS_LOADING; |
284 } | 286 } |
285 | 287 |
286 void KioskAppData::SetStatus(Status status) { | 288 void KioskAppData::SetStatus(Status status) { |
287 if (status_ == status) | 289 if (status_ == status) |
288 return; | 290 return; |
289 | 291 |
290 status_ = status; | 292 status_ = status; |
291 | 293 |
292 if (!delegate_) | 294 if (!delegate_) |
293 return; | 295 return; |
294 | 296 |
295 switch (status_) { | 297 switch (status_) { |
296 case STATUS_INIT: | 298 case STATUS_INIT: |
297 break; | 299 break; |
298 case STATUS_LOADING: | 300 case STATUS_LOADING: |
299 case STATUS_LOADED: | 301 case STATUS_LOADED: |
300 delegate_->OnKioskAppDataChanged(id_); | 302 delegate_->OnKioskAppDataChanged(app_id_); |
301 break; | 303 break; |
302 case STATUS_ERROR: | 304 case STATUS_ERROR: |
303 delegate_->OnKioskAppDataLoadFailure(id_); | 305 delegate_->OnKioskAppDataLoadFailure(app_id_); |
304 break; | 306 break; |
305 }; | 307 }; |
306 } | 308 } |
307 | 309 |
308 net::URLRequestContextGetter* KioskAppData::GetRequestContextGetter() { | 310 net::URLRequestContextGetter* KioskAppData::GetRequestContextGetter() { |
309 return g_browser_process->system_request_context(); | 311 return g_browser_process->system_request_context(); |
310 } | 312 } |
311 | 313 |
312 bool KioskAppData::LoadFromCache() { | 314 bool KioskAppData::LoadFromCache() { |
313 std::string app_key = std::string(KioskAppManager::kKeyApps) + '.' + id_; | 315 std::string app_key = std::string(KioskAppManager::kKeyApps) + '.' + app_id_; |
314 std::string name_key = app_key + '.' + kKeyName; | 316 std::string name_key = app_key + '.' + kKeyName; |
315 std::string icon_path_key = app_key + '.' + kKeyIcon; | 317 std::string icon_path_key = app_key + '.' + kKeyIcon; |
316 | 318 |
317 PrefService* local_state = g_browser_process->local_state(); | 319 PrefService* local_state = g_browser_process->local_state(); |
318 const base::DictionaryValue* dict = | 320 const base::DictionaryValue* dict = |
319 local_state->GetDictionary(KioskAppManager::kKioskDictionaryName); | 321 local_state->GetDictionary(KioskAppManager::kKioskDictionaryName); |
320 | 322 |
321 icon_path_.clear(); | 323 icon_path_.clear(); |
322 std::string icon_path_string; | 324 std::string icon_path_string; |
323 if (!dict->GetString(name_key, &name_) || | 325 if (!dict->GetString(name_key, &name_) || |
324 !dict->GetString(icon_path_key, &icon_path_string)) { | 326 !dict->GetString(icon_path_key, &icon_path_string)) { |
325 return false; | 327 return false; |
326 } | 328 } |
327 icon_path_ = base::FilePath(icon_path_string); | 329 icon_path_ = base::FilePath(icon_path_string); |
328 | 330 |
329 // IconLoader deletes itself when done. | 331 // IconLoader deletes itself when done. |
330 (new IconLoader(AsWeakPtr(), icon_path_))->Start(); | 332 (new IconLoader(AsWeakPtr(), icon_path_))->Start(); |
331 return true; | 333 return true; |
332 } | 334 } |
333 | 335 |
334 void KioskAppData::SetCache(const std::string& name, | 336 void KioskAppData::SetCache(const std::string& name, |
335 const base::FilePath& icon_path) { | 337 const base::FilePath& icon_path) { |
336 std::string app_key = std::string(KioskAppManager::kKeyApps) + '.' + id_; | 338 std::string app_key = std::string(KioskAppManager::kKeyApps) + '.' + app_id_; |
337 std::string name_key = app_key + '.' + kKeyName; | 339 std::string name_key = app_key + '.' + kKeyName; |
338 std::string icon_path_key = app_key + '.' + kKeyIcon; | 340 std::string icon_path_key = app_key + '.' + kKeyIcon; |
339 | 341 |
340 PrefService* local_state = g_browser_process->local_state(); | 342 PrefService* local_state = g_browser_process->local_state(); |
341 DictionaryPrefUpdate dict_update(local_state, | 343 DictionaryPrefUpdate dict_update(local_state, |
342 KioskAppManager::kKioskDictionaryName); | 344 KioskAppManager::kKioskDictionaryName); |
343 dict_update->SetString(name_key, name); | 345 dict_update->SetString(name_key, name); |
344 dict_update->SetString(icon_path_key, icon_path.value()); | 346 dict_update->SetString(icon_path_key, icon_path.value()); |
345 icon_path_ = icon_path; | 347 icon_path_ = icon_path; |
346 } | 348 } |
(...skipping 19 matching lines...) Expand all Loading... |
366 std::vector<unsigned char> image_data; | 368 std::vector<unsigned char> image_data; |
367 CHECK(gfx::PNGCodec::EncodeBGRASkBitmap(icon, false, &image_data)); | 369 CHECK(gfx::PNGCodec::EncodeBGRASkBitmap(icon, false, &image_data)); |
368 raw_icon_ = new base::RefCountedString; | 370 raw_icon_ = new base::RefCountedString; |
369 raw_icon_->data().assign(image_data.begin(), image_data.end()); | 371 raw_icon_->data().assign(image_data.begin(), image_data.end()); |
370 | 372 |
371 base::FilePath cache_dir; | 373 base::FilePath cache_dir; |
372 if (delegate_) | 374 if (delegate_) |
373 delegate_->GetKioskAppIconCacheDir(&cache_dir); | 375 delegate_->GetKioskAppIconCacheDir(&cache_dir); |
374 | 376 |
375 base::FilePath icon_path = | 377 base::FilePath icon_path = |
376 cache_dir.AppendASCII(id_).AddExtension(kIconFileExtension); | 378 cache_dir.AppendASCII(app_id_).AddExtension(kIconFileExtension); |
377 BrowserThread::GetBlockingPool()->PostTask( | 379 BrowserThread::GetBlockingPool()->PostTask( |
378 FROM_HERE, | 380 FROM_HERE, |
379 base::Bind(&SaveIconToLocalOnBlockingPool, icon_path, raw_icon_)); | 381 base::Bind(&SaveIconToLocalOnBlockingPool, icon_path, raw_icon_)); |
380 | 382 |
381 SetCache(name_, icon_path); | 383 SetCache(name_, icon_path); |
382 SetStatus(STATUS_LOADED); | 384 SetStatus(STATUS_LOADED); |
383 } | 385 } |
384 | 386 |
385 void KioskAppData::OnWebstoreParseFailure() { | 387 void KioskAppData::OnWebstoreParseFailure() { |
386 SetStatus(STATUS_ERROR); | 388 SetStatus(STATUS_ERROR); |
387 } | 389 } |
388 | 390 |
389 void KioskAppData::StartFetch() { | 391 void KioskAppData::StartFetch() { |
390 webstore_fetcher_.reset(new extensions::WebstoreDataFetcher( | 392 webstore_fetcher_.reset(new extensions::WebstoreDataFetcher( |
391 this, | 393 this, |
392 GetRequestContextGetter(), | 394 GetRequestContextGetter(), |
393 GURL(), | 395 GURL(), |
394 id_)); | 396 app_id_)); |
395 webstore_fetcher_->Start(); | 397 webstore_fetcher_->Start(); |
396 } | 398 } |
397 | 399 |
398 void KioskAppData::OnWebstoreRequestFailure() { | 400 void KioskAppData::OnWebstoreRequestFailure() { |
399 SetStatus(STATUS_ERROR); | 401 SetStatus(STATUS_ERROR); |
400 } | 402 } |
401 | 403 |
402 void KioskAppData::OnWebstoreResponseParseSuccess( | 404 void KioskAppData::OnWebstoreResponseParseSuccess( |
403 base::DictionaryValue* webstore_data) { | 405 base::DictionaryValue* webstore_data) { |
404 // Takes ownership of |webstore_data|. | 406 // Takes ownership of |webstore_data|. |
(...skipping 18 matching lines...) Expand all Loading... |
423 return; | 425 return; |
424 } | 426 } |
425 GURL icon_url = GURL(extension_urls::GetWebstoreLaunchURL()).Resolve( | 427 GURL icon_url = GURL(extension_urls::GetWebstoreLaunchURL()).Resolve( |
426 icon_url_string); | 428 icon_url_string); |
427 if (!icon_url.is_valid()) { | 429 if (!icon_url.is_valid()) { |
428 OnWebstoreResponseParseFailure(kInvalidWebstoreResponseError); | 430 OnWebstoreResponseParseFailure(kInvalidWebstoreResponseError); |
429 return; | 431 return; |
430 } | 432 } |
431 | 433 |
432 // WebstoreDataParser deletes itself when done. | 434 // WebstoreDataParser deletes itself when done. |
433 (new WebstoreDataParser(AsWeakPtr()))->Start(id_, | 435 (new WebstoreDataParser(AsWeakPtr()))->Start(app_id_, |
434 manifest, | 436 manifest, |
435 icon_url, | 437 icon_url, |
436 GetRequestContextGetter()); | 438 GetRequestContextGetter()); |
437 } | 439 } |
438 | 440 |
439 void KioskAppData::OnWebstoreResponseParseFailure(const std::string& error) { | 441 void KioskAppData::OnWebstoreResponseParseFailure(const std::string& error) { |
440 webstore_fetcher_.reset(); | 442 webstore_fetcher_.reset(); |
441 SetStatus(STATUS_ERROR); | 443 SetStatus(STATUS_ERROR); |
442 } | 444 } |
443 | 445 |
444 } // namespace chromeos | 446 } // namespace chromeos |
OLD | NEW |