Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/dom_ui/imageburner_ui.h" | |
| 6 | |
| 7 #include "base/i18n/rtl.h" | |
| 8 #include "app/l10n_util.h" | |
| 9 #include "app/resource_bundle.h" | |
| 10 #include "base/message_loop.h" | |
| 11 #include "base/path_service.h" | |
| 12 #include "base/singleton.h" | |
| 13 #include "base/string_util.h" | |
| 14 #include "base/task.h" | |
| 15 #include "base/utf_string_conversions.h" | |
| 16 #include "base/values.h" | |
| 17 | |
| 18 #include "chrome/browser/chrome_thread.h" | |
| 19 #include "chrome/browser/download/download_types.h" | |
| 20 #include "chrome/browser/download/download_util.h" | |
| 21 #include "chrome/browser/profile.h" | |
| 22 #include "chrome/common/chrome_paths.h" | |
| 23 #include "chrome/common/jstemplate_builder.h" | |
| 24 #include "chrome/common/url_constants.h" | |
| 25 #include "grit/browser_resources.h" | |
| 26 #include "grit/generated_resources.h" | |
| 27 #include "grit/locale_settings.h" | |
| 28 | |
| 29 | |
| 30 //////////////////////////////////////////////////////////////////////////////// | |
| 31 // | |
| 32 // ImageBurnUIHTMLSource | |
| 33 // | |
| 34 //////////////////////////////////////////////////////////////////////////////// | |
| 35 | |
| 36 ImageBurnUIHTMLSource::ImageBurnUIHTMLSource() | |
| 37 : DataSource(chrome::kChromeUIImageBurnerHost, MessageLoop::current()) { | |
| 38 } | |
| 39 | |
| 40 void ImageBurnUIHTMLSource::StartDataRequest(const std::string& path, | |
| 41 bool is_off_the_record, | |
| 42 int request_id) { | |
| 43 DictionaryValue localized_strings; | |
| 44 localized_strings.SetString("burnConfirmText1", | |
| 45 l10n_util::GetStringUTF16(IDS_IMAGEBURN_CONFIM_BURN1)); | |
| 46 localized_strings.SetString("burnConfirmText2", | |
| 47 l10n_util::GetStringUTF16(IDS_IMAGEBURN_CONFIM_BURN2)); | |
|
tony
2011/08/04 17:18:45
This is the wrong way to localize a sentence with
| |
| 48 localized_strings.SetString("burnUnsuccessfulMessage", | |
| 49 l10n_util::GetStringUTF16(IDS_IMAGEBURN_BURN_UNSUCCESSFUL)); | |
| 50 localized_strings.SetString("burnSuccessfulMessage", | |
| 51 l10n_util::GetStringUTF16(IDS_IMAGEBURN_BURN_SUCCESSFUL)); | |
| 52 localized_strings.SetString("downloadAbortedMessage", | |
| 53 l10n_util::GetStringUTF16(IDS_IMAGEBURN_DOWNLOAD_UNSUCCESSFUL)); | |
| 54 localized_strings.SetString("title", | |
| 55 l10n_util::GetStringUTF16(IDS_IMAGEBURN_TITLE)); | |
| 56 localized_strings.SetString("listTitle", | |
| 57 l10n_util::GetStringUTF16(IDS_IMAGEBURN_ROOT_LIST_TITLE)); | |
| 58 localized_strings.SetString("downloadStatusStart", | |
| 59 l10n_util::GetStringUTF16(IDS_IMAGEBURN_DOWNLOAD_STARTING_STATUS)); | |
| 60 localized_strings.SetString("downloadStatusInProgress", | |
| 61 l10n_util::GetStringUTF16(IDS_IMAGEBURN_DOWNLOAD_IN_PROGRESS_STATUS)); | |
| 62 localized_strings.SetString("downloadStatusComplete", | |
| 63 l10n_util::GetStringUTF16(IDS_IMAGEBURN_DOWNLOAD_COMPLETE_STATUS)); | |
| 64 localized_strings.SetString("downloadStatusCanceled", | |
| 65 l10n_util::GetStringUTF16(IDS_IMAGEBURN_DOWNLOAD_CANCELED_STATUS)); | |
| 66 localized_strings.SetString("burnStatusStart", | |
| 67 l10n_util::GetStringUTF16(IDS_IMAGEBURN_BURN_STARTING_STATUS)); | |
| 68 localized_strings.SetString("burnStatusInProgress", | |
| 69 l10n_util::GetStringUTF16(IDS_IMAGEBURN_BURN_IN_PROGRESS_STATUS)); | |
| 70 localized_strings.SetString("burnStatusComplete", | |
| 71 l10n_util::GetStringUTF16(IDS_IMAGEBURN_BURN_COMPLETE_STATUS)); | |
| 72 localized_strings.SetString("burnStatusCanceled", | |
| 73 l10n_util::GetStringUTF16(IDS_IMAGEBURN_BURN_CANCELED_STATUS)); | |
| 74 | |
| 75 SetFontAndTextDirection(&localized_strings); | |
| 76 | |
| 77 static const base::StringPiece imageburn_html( | |
| 78 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 79 IDR_IMAGEBURNER_HTML)); | |
| 80 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( | |
| 81 imageburn_html, &localized_strings); | |
| 82 | |
| 83 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); | |
| 84 html_bytes->data.resize(full_html.size()); | |
| 85 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); | |
| 86 | |
| 87 SendResponse(request_id, html_bytes); | |
| 88 } | |
| 89 | |
| 90 //////////////////////////////////////////////////////////////////////////////// | |
| 91 // | |
| 92 // ImageBurnHandler | |
| 93 // | |
| 94 //////////////////////////////////////////////////////////////////////////////// | |
| 95 | |
| 96 ImageBurnHandler::ImageBurnHandler(TabContents* contents) | |
| 97 :tab_contents_(contents), | |
| 98 download_manager_(NULL), | |
| 99 download_item_observer_added_(false), | |
| 100 active_download_item_(NULL), | |
| 101 burn_resource_manager_(NULL) { | |
| 102 chromeos::MountLibrary* mount_lib = | |
| 103 chromeos::CrosLibrary::Get()->GetMountLibrary(); | |
| 104 mount_lib->AddObserver(this); | |
| 105 chromeos::BurnLibrary* burn_lib = | |
| 106 chromeos::CrosLibrary::Get()->GetBurnLibrary(); | |
| 107 burn_lib->AddObserver(this); | |
| 108 local_image_file_path_.clear(); | |
| 109 burn_resource_manager_ = Singleton<ImageBurnResourceManager>::get(); | |
| 110 } | |
| 111 | |
| 112 ImageBurnHandler::~ImageBurnHandler() { | |
| 113 chromeos::MountLibrary* mount_lib = | |
| 114 chromeos::CrosLibrary::Get()->GetMountLibrary(); | |
| 115 mount_lib->RemoveObserver(this); | |
| 116 chromeos::BurnLibrary* burn_lib = | |
| 117 chromeos::CrosLibrary::Get()->GetBurnLibrary(); | |
| 118 burn_lib->RemoveObserver(this); | |
| 119 if (active_download_item_) { | |
| 120 active_download_item_->RemoveObserver(this); | |
| 121 } | |
| 122 if (download_manager_) | |
| 123 download_manager_->RemoveObserver(this); | |
| 124 } | |
| 125 | |
| 126 DOMMessageHandler* ImageBurnHandler::Attach(DOMUI* dom_ui) { | |
| 127 return DOMMessageHandler::Attach(dom_ui); | |
| 128 } | |
| 129 | |
| 130 void ImageBurnHandler::RegisterMessages() { | |
| 131 dom_ui_->RegisterMessageCallback("getRoots", | |
| 132 NewCallback(this, &ImageBurnHandler::HandleGetRoots)); | |
| 133 dom_ui_->RegisterMessageCallback("downloadImage", | |
| 134 NewCallback(this, &ImageBurnHandler::HandleDownloadImage)); | |
| 135 dom_ui_->RegisterMessageCallback("burnImage", | |
| 136 NewCallback(this, &ImageBurnHandler::HandleBurnImage)); | |
| 137 dom_ui_->RegisterMessageCallback("cancelBurnImage", | |
| 138 NewCallback(this, &ImageBurnHandler::HandleCancelBurnImage)); | |
| 139 } | |
| 140 | |
| 141 void ImageBurnHandler::MountChanged(chromeos::MountLibrary* obj, | |
| 142 chromeos::MountEventType evt, | |
| 143 const std::string& path) { | |
| 144 if ((evt == chromeos::DISK_REMOVED || | |
| 145 evt == chromeos::DISK_CHANGED || | |
| 146 evt == chromeos::DEVICE_REMOVED)) { | |
| 147 dom_ui_->CallJavascriptFunction(L"rootsChanged"); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 void ImageBurnHandler::ProgressUpdated(chromeos::BurnLibrary* object, | |
| 152 chromeos::BurnEventType evt, | |
| 153 const ImageBurnStatus& status) { | |
| 154 UpdateBurnProgress(status.amount_burnt, status.total_size, | |
| 155 status.target_path, evt); | |
| 156 } | |
| 157 | |
| 158 void ImageBurnHandler::OnDownloadUpdated(DownloadItem* download) { | |
| 159 if (download->state() != DownloadItem::CANCELLED | |
| 160 && download->state() != DownloadItem::COMPLETE) { | |
| 161 scoped_ptr<DictionaryValue> result_value( | |
| 162 download_util::CreateDownloadItemValue(download, 0)); | |
| 163 dom_ui_->CallJavascriptFunction(L"downloadUpdated", *result_value); | |
| 164 } | |
| 165 if (download->state() == DownloadItem::CANCELLED) | |
| 166 DownloadCompleted(false); | |
| 167 } | |
| 168 | |
| 169 void ImageBurnHandler::OnDownloadFileCompleted(DownloadItem* download) { | |
| 170 DCHECK(download->state() == DownloadItem::COMPLETE); | |
| 171 local_image_file_path_ = download->full_path(); | |
| 172 DownloadCompleted(true); | |
| 173 } | |
| 174 | |
| 175 void ImageBurnHandler::OnDownloadOpened(DownloadItem* download) { | |
| 176 if (download->safety_state() == DownloadItem::DANGEROUS) | |
| 177 download->DangerousDownloadValidated(); | |
| 178 } | |
| 179 | |
| 180 void ImageBurnHandler::ModelChanged() { | |
| 181 std::vector<DownloadItem*> downloads; | |
| 182 download_manager_->GetTemporaryDownloads( | |
| 183 burn_resource_manager_->GetLocalImageDirPath(), &downloads); | |
| 184 if (download_item_observer_added_) | |
| 185 return; | |
| 186 std::vector<DownloadItem*>::const_iterator it = downloads.begin(); | |
| 187 for (; it != downloads.end(); ++it) { | |
| 188 if ((*it)->url() == *image_download_url_) { | |
| 189 download_item_observer_added_ = true; | |
| 190 (*it)->AddObserver(this); | |
| 191 active_download_item_ = *it; | |
| 192 break; | |
| 193 } | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 void ImageBurnHandler::HandleGetRoots(const ListValue* args) { | |
| 198 ListValue results_value; | |
| 199 DictionaryValue info_value; | |
| 200 chromeos::MountLibrary* mount_lib = | |
| 201 chromeos::CrosLibrary::Get()->GetMountLibrary(); | |
| 202 const chromeos::MountLibrary::DiskVector& disks = mount_lib->disks(); | |
| 203 if (!burn_resource_manager_->CheckBurnInProgress()) { | |
| 204 for (size_t i = 0; i < disks.size(); ++i) { | |
| 205 if (!disks[i].mount_path.empty()) { | |
| 206 DictionaryValue* page_value = new DictionaryValue(); | |
| 207 FilePath disk_path = FilePath(disks[i].system_path).DirName(); | |
| 208 std::string title = "/dev/" + disk_path.BaseName().value(); | |
| 209 page_value->SetString(kPropertyTitle, title); | |
| 210 page_value->SetString(kPropertyPath, title); | |
| 211 page_value->SetBoolean(kPropertyDirectory, true); | |
| 212 results_value.Append(page_value); | |
| 213 } | |
| 214 } | |
| 215 } | |
| 216 info_value.SetString("functionCall", "getRoots"); | |
| 217 info_value.SetString(kPropertyPath, ""); | |
| 218 dom_ui_->CallJavascriptFunction(L"browseFileResult", | |
| 219 info_value, results_value); | |
| 220 } | |
| 221 | |
| 222 void ImageBurnHandler::HandleDownloadImage(const ListValue* args) { | |
| 223 ExtractTargetedDeviceSystemPath(args); | |
| 224 CreateLocalImagePath(); | |
| 225 CreateImageUrl(); | |
| 226 } | |
| 227 | |
| 228 void ImageBurnHandler::HandleBurnImage(const ListValue* args) { | |
| 229 ImageBurnTaskProxy* task = new ImageBurnTaskProxy(AsWeakPtr()); | |
| 230 task->AddRef(); | |
| 231 ChromeThread::PostTask( | |
| 232 ChromeThread::UI, FROM_HERE, | |
| 233 NewRunnableMethod(task, &ImageBurnTaskProxy::BurnImage)); | |
| 234 } | |
| 235 | |
| 236 void ImageBurnHandler::HandleCancelBurnImage(const ListValue* args) { | |
| 237 image_target_.clear(); | |
| 238 } | |
| 239 | |
| 240 void ImageBurnHandler::DownloadCompleted(bool is_successful) { | |
| 241 burn_resource_manager_-> ReportDownloadFinished(is_successful); | |
| 242 if (active_download_item_) { | |
| 243 active_download_item_->RemoveObserver(this); | |
| 244 active_download_item_ = NULL; | |
| 245 } | |
| 246 download_item_observer_added_ = false; | |
| 247 | |
| 248 DictionaryValue signal_value; | |
| 249 if (is_successful) { | |
| 250 signal_value.SetString("state", "COMPLETE"); | |
| 251 dom_ui_->CallJavascriptFunction(L"downloadUpdated", signal_value); | |
| 252 dom_ui_->CallJavascriptFunction(L"promtUserDownloadFinished"); | |
| 253 } else { | |
| 254 signal_value.SetString("state", "CANCELLED"); | |
| 255 dom_ui_->CallJavascriptFunction(L"downloadUpdated", signal_value); | |
| 256 dom_ui_->CallJavascriptFunction(L"alertUserDownloadAborted"); | |
| 257 } | |
| 258 } | |
| 259 | |
| 260 void ImageBurnHandler::BurnImage() { | |
| 261 chromeos::BurnLibrary* burn_lib = | |
| 262 chromeos::CrosLibrary::Get()->GetBurnLibrary(); | |
| 263 if (burn_lib->DoBurn(local_image_file_path_, image_target_)) { | |
| 264 DictionaryValue signal_value; | |
| 265 signal_value.SetString("state", "IN_PROGRESS"); | |
| 266 signal_value.SetInteger("amount_burnt", 0); | |
| 267 signal_value.SetString("progress_status_text", ""); | |
| 268 dom_ui_->CallJavascriptFunction(L"burnUpdated", signal_value); | |
| 269 } | |
| 270 } | |
| 271 | |
| 272 void ImageBurnHandler::FinalizeBurn(bool successful) { | |
| 273 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | |
| 274 if (successful) | |
| 275 dom_ui_->CallJavascriptFunction(L"burnSuccessful"); | |
| 276 else | |
| 277 dom_ui_->CallJavascriptFunction(L"burnUnsuccessful"); | |
| 278 } | |
| 279 | |
| 280 void ImageBurnHandler::UpdateBurnProgress(int64 total_burnt, | |
| 281 int64 image_size, | |
| 282 const std::string& path, | |
| 283 chromeos::BurnEventType event) { | |
| 284 DictionaryValue progress_value; | |
| 285 progress_value.SetString("progress_status_text", | |
| 286 WideToUTF16Hack(GetBurnProgressText(total_burnt, image_size))); | |
| 287 if (event == chromeos::BURN_UPDATED) | |
| 288 progress_value.SetString("state", "IN_PROGRESS"); | |
| 289 else if (event == chromeos::BURN_CANCELED) | |
| 290 progress_value.SetString("state", "CANCELLED"); | |
| 291 else if (event == chromeos::BURN_COMPLETE) | |
| 292 progress_value.SetString("state", "COMPLETE"); | |
| 293 progress_value.SetInteger("received", total_burnt); | |
| 294 progress_value.SetInteger("total", image_size); | |
| 295 progress_value.SetString("path", path); | |
| 296 | |
| 297 dom_ui_->CallJavascriptFunction(L"burnProgressUpdated", progress_value); | |
| 298 } | |
| 299 | |
| 300 std::wstring ImageBurnHandler::GetBurnProgressText(int64 total_burnt, | |
| 301 int64 image_size) { | |
| 302 DataUnits amount_units = GetByteDisplayUnits(total_burnt); | |
| 303 std::wstring burnt_size = | |
| 304 UTF16ToWideHack(FormatBytes(total_burnt, amount_units, true)); | |
| 305 | |
| 306 std::wstring burnt_size_localized; | |
| 307 if (base::i18n::AdjustStringForLocaleDirection(burnt_size, | |
| 308 &burnt_size_localized)) { | |
| 309 burnt_size.assign(burnt_size_localized); | |
| 310 } | |
| 311 | |
| 312 if (image_size) { | |
| 313 amount_units = GetByteDisplayUnits(image_size); | |
| 314 std::wstring total_text = | |
| 315 UTF16ToWideHack(FormatBytes(image_size, amount_units, true)); | |
| 316 std::wstring total_text_localized; | |
| 317 if (base::i18n::AdjustStringForLocaleDirection(total_text, | |
| 318 &total_text_localized)) | |
| 319 total_text.assign(total_text_localized); | |
| 320 | |
| 321 return l10n_util::GetStringF(IDS_IMAGEBURN_BURN_PROGRESS, | |
| 322 burnt_size, | |
| 323 total_text); | |
| 324 } else { | |
| 325 return l10n_util::GetStringF(IDS_IMAGEBURN_BURN_PROGRESS_SIZE_UNKNOWN, | |
| 326 burnt_size); | |
| 327 } | |
| 328 } | |
| 329 | |
| 330 void ImageBurnHandler::CreateImageUrl() { | |
| 331 ImageBurnTaskProxy* task = new ImageBurnTaskProxy(AsWeakPtr()); | |
| 332 task->AddRef(); | |
| 333 task->CreateImageUrl(tab_contents_, this); | |
| 334 } | |
| 335 | |
| 336 void ImageBurnHandler::CreateImageUrlCallback(GURL* image_url) { | |
| 337 if (!image_url) { | |
| 338 DownloadCompleted(false); | |
| 339 return; | |
| 340 } | |
| 341 image_download_url_ = image_url; | |
| 342 | |
| 343 download_manager_ = tab_contents_->profile()->GetDownloadManager(); | |
| 344 download_manager_->AddObserver(this); | |
| 345 | |
| 346 ImageBurnTaskProxy* task = new ImageBurnTaskProxy(AsWeakPtr()); | |
| 347 task->AddRef(); | |
| 348 | |
| 349 if (!task->ReportDownloadInitialized()) { | |
| 350 DownloadSaveInfo save_info; | |
| 351 save_info.file_path = local_image_file_path_; | |
| 352 net::FileStream* file_stream = | |
| 353 burn_resource_manager_->CreateFileStream(&local_image_file_path_); | |
| 354 if (!file_stream) { | |
| 355 DownloadCompleted(false); | |
| 356 return; | |
| 357 } | |
| 358 save_info.file_stream = linked_ptr<net::FileStream>(file_stream); | |
| 359 | |
| 360 download_manager_->DownloadUrlToFile(*image_download_url_, | |
| 361 tab_contents_->GetURL(), | |
| 362 tab_contents_->encoding(), | |
| 363 save_info, | |
| 364 tab_contents_); | |
| 365 } else if (task->CheckDownloadFinished()) { | |
| 366 DownloadCompleted(true); | |
| 367 } | |
| 368 } | |
| 369 | |
| 370 void ImageBurnHandler::ExtractTargetedDeviceSystemPath( | |
| 371 const ListValue* list_value) { | |
| 372 Value* list_member; | |
| 373 std::string image_dest; | |
| 374 if (list_value->Get(0, &list_member) && | |
| 375 list_member->GetType() == Value::TYPE_STRING) { | |
| 376 const StringValue* string_value = | |
| 377 static_cast<const StringValue*>(list_member); | |
| 378 string_value->GetAsString(&image_dest); | |
| 379 } else { | |
| 380 LOG(ERROR) << "Unable to get path string"; | |
| 381 return; | |
| 382 } | |
| 383 image_target_ = FilePath(image_dest); | |
| 384 } | |
| 385 | |
| 386 void ImageBurnHandler::CreateLocalImagePath() { | |
| 387 local_image_file_path_ = | |
| 388 burn_resource_manager_->GetLocalImageDirPath().Append(kImageFileName); | |
| 389 } | |
| 390 | |
| 391 //////////////////////////////////////////////////////////////////////////////// | |
| 392 // | |
| 393 // ImageBurnTaskProxy | |
| 394 // | |
| 395 //////////////////////////////////////////////////////////////////////////////// | |
| 396 ImageBurnTaskProxy::ImageBurnTaskProxy( | |
| 397 const base::WeakPtr<ImageBurnHandler>& handler) | |
| 398 : handler_(handler) { | |
| 399 resource_manager_ = Singleton<ImageBurnResourceManager>::get(); | |
| 400 } | |
| 401 | |
| 402 bool ImageBurnTaskProxy::ReportDownloadInitialized() { | |
| 403 bool initialized = resource_manager_-> CheckImageDownloadStarted(); | |
| 404 if (!initialized) | |
| 405 resource_manager_-> ReportImageDownloadStarted(); | |
| 406 return initialized; | |
| 407 } | |
| 408 | |
| 409 bool ImageBurnTaskProxy::CheckDownloadFinished() { | |
| 410 return resource_manager_->CheckDownloadFinished(); | |
| 411 } | |
| 412 | |
| 413 void ImageBurnTaskProxy:: BurnImage() { | |
| 414 if (!resource_manager_->CheckBurnInProgress() && handler_) { | |
| 415 resource_manager_->SetBurnInProgress(true); | |
| 416 handler_->BurnImage(); | |
| 417 } | |
| 418 } | |
| 419 | |
| 420 void ImageBurnTaskProxy::FinalizeBurn(bool success) { | |
| 421 if (handler_) { | |
| 422 handler_->FinalizeBurn(success); | |
| 423 resource_manager_->SetBurnInProgress(false); | |
| 424 } | |
| 425 } | |
| 426 | |
| 427 void ImageBurnTaskProxy::CreateImageUrl(TabContents* tab_contents, | |
| 428 ImageBurnHandler* downloader) { | |
| 429 resource_manager_->CreateImageUrl(tab_contents, downloader); | |
| 430 } | |
| 431 | |
| 432 | |
| 433 //////////////////////////////////////////////////////////////////////////////// | |
| 434 // | |
| 435 // ImageBurnResourceManager | |
| 436 // | |
| 437 //////////////////////////////////////////////////////////////////////////////// | |
| 438 | |
| 439 ImageBurnResourceManager::ImageBurnResourceManager() | |
| 440 : image_download_started_(false), | |
| 441 image_download_finished_(false), | |
| 442 burn_in_progress_(false), | |
| 443 download_manager_(NULL), | |
| 444 download_item_observer_added_(false), | |
| 445 active_download_item_(NULL), | |
| 446 image_url_(NULL), | |
| 447 image_fetcher_url_(kImageBaseURL + kImageFetcherName), | |
| 448 image_url_fetching_requested_(false), | |
| 449 image_url_fetched_(false) { | |
| 450 local_image_dir_file_path_.clear(); | |
| 451 image_fecher_local_path_ = GetLocalImageDirPath().Append(kImageFetcherName); | |
| 452 } | |
| 453 | |
| 454 ImageBurnResourceManager::~ImageBurnResourceManager() { | |
| 455 if (!local_image_dir_file_path_.empty()) { | |
| 456 file_util::Delete(local_image_dir_file_path_, true); | |
| 457 } | |
| 458 if (active_download_item_) { | |
| 459 active_download_item_->RemoveObserver(this); | |
| 460 } | |
| 461 if (download_manager_) | |
| 462 download_manager_->RemoveObserver(this); | |
| 463 } | |
| 464 | |
| 465 void ImageBurnResourceManager::OnDownloadUpdated(DownloadItem* download) { | |
| 466 if (download->state() == DownloadItem::CANCELLED) { | |
| 467 image_url_.reset(); | |
| 468 ImageUrlFetched(false); | |
| 469 } | |
| 470 } | |
| 471 | |
| 472 void ImageBurnResourceManager::OnDownloadFileCompleted(DownloadItem* download) { | |
| 473 DCHECK(download->state() == DownloadItem::COMPLETE); | |
| 474 std::string image_url; | |
| 475 if (file_util::ReadFileToString(image_fecher_local_path_, &image_url)) { | |
| 476 image_url_.reset(new GURL(kImageBaseURL + image_url)); | |
| 477 ImageUrlFetched(true); | |
| 478 } else { | |
| 479 image_url_.reset(); | |
| 480 ImageUrlFetched(false); | |
| 481 } | |
| 482 } | |
| 483 | |
| 484 void ImageBurnResourceManager::OnDownloadOpened(DownloadItem* download) { } | |
| 485 | |
| 486 void ImageBurnResourceManager::ModelChanged() { | |
| 487 std::vector<DownloadItem*> downloads; | |
| 488 download_manager_->GetTemporaryDownloads(GetLocalImageDirPath(), &downloads); | |
| 489 | |
| 490 if (download_item_observer_added_) | |
| 491 return; | |
| 492 std::vector<DownloadItem*>::const_iterator it = downloads.begin(); | |
| 493 for (; it != downloads.end(); ++it) { | |
| 494 if ((*it)->url() == image_fetcher_url_) { | |
| 495 download_item_observer_added_ = true; | |
| 496 (*it)->AddObserver(this); | |
| 497 active_download_item_ = *it; | |
| 498 } | |
| 499 } | |
| 500 } | |
| 501 | |
| 502 FilePath ImageBurnResourceManager::GetLocalImageDirPath() { | |
| 503 if (local_image_dir_file_path_.empty()) { | |
| 504 CHECK(PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, | |
| 505 &local_image_dir_file_path_)); | |
| 506 local_image_dir_file_path_ = | |
| 507 local_image_dir_file_path_.Append(kTempImageFolderName); | |
| 508 file_util::CreateDirectory(local_image_dir_file_path_); | |
| 509 } | |
| 510 return local_image_dir_file_path_; | |
| 511 } | |
| 512 | |
| 513 bool ImageBurnResourceManager::CheckImageDownloadStarted() { | |
| 514 return image_download_started_; | |
| 515 } | |
| 516 | |
| 517 void ImageBurnResourceManager::ReportImageDownloadStarted() { | |
| 518 image_download_started_ = true; | |
| 519 } | |
| 520 | |
| 521 bool ImageBurnResourceManager::CheckDownloadFinished() { | |
| 522 return image_download_finished_; | |
| 523 } | |
| 524 | |
| 525 bool ImageBurnResourceManager::CheckBurnInProgress() { | |
| 526 return burn_in_progress_; | |
| 527 } | |
| 528 | |
| 529 void ImageBurnResourceManager::SetBurnInProgress(bool value) { | |
| 530 burn_in_progress_ = value; | |
| 531 } | |
| 532 | |
| 533 void ImageBurnResourceManager::ReportDownloadFinished(bool success) { | |
| 534 if (!image_download_started_) | |
| 535 return; | |
| 536 if (!success) | |
| 537 image_download_started_ = false; | |
| 538 image_download_finished_ = success; | |
| 539 } | |
| 540 | |
| 541 void ImageBurnResourceManager::CreateImageUrl(TabContents* tab_contents, | |
| 542 ImageBurnHandler* downloader) { | |
| 543 if (image_url_fetched_) { | |
| 544 downloader->CreateImageUrlCallback(image_url_.get()); | |
| 545 return; | |
| 546 } | |
| 547 downloaders_.push_back(downloader); | |
| 548 | |
| 549 if (image_url_fetching_requested_) { | |
| 550 return; | |
| 551 } | |
| 552 image_url_fetching_requested_ = true; | |
| 553 | |
| 554 download_manager_ = tab_contents->profile()->GetDownloadManager(); | |
| 555 download_manager_->AddObserver(this); | |
| 556 | |
| 557 DownloadSaveInfo save_info; | |
| 558 save_info.file_path = image_fecher_local_path_; | |
| 559 net::FileStream* file_stream = CreateFileStream(&image_fecher_local_path_); | |
| 560 if (!file_stream) { | |
| 561 ImageUrlFetched(false); | |
| 562 return; | |
| 563 } | |
| 564 save_info.file_stream = linked_ptr<net::FileStream>(file_stream); | |
| 565 download_manager_->DownloadUrlToFile(image_fetcher_url_, | |
| 566 tab_contents->GetURL(), | |
| 567 tab_contents->encoding(), | |
| 568 save_info, | |
| 569 tab_contents); | |
| 570 } | |
| 571 | |
| 572 void ImageBurnResourceManager::ImageUrlFetched(bool success) { | |
| 573 if (active_download_item_) { | |
| 574 active_download_item_->RemoveObserver(this); | |
| 575 active_download_item_ = NULL; | |
| 576 } | |
| 577 download_item_observer_added_ = false; | |
| 578 if (download_manager_) | |
| 579 download_manager_->RemoveObserver(this); | |
| 580 if (!success) | |
| 581 image_url_fetching_requested_ = false; | |
| 582 image_url_fetched_ = success; | |
| 583 for (size_t i = 0; i < downloaders_.size(); ++i) | |
| 584 downloaders_[i]->CreateImageUrlCallback(image_url_.get()); | |
| 585 downloaders_.clear(); | |
| 586 } | |
| 587 | |
| 588 net::FileStream* ImageBurnResourceManager::CreateFileStream( | |
| 589 FilePath* file_path) { | |
| 590 DCHECK(file_path && !file_path->empty()); | |
| 591 scoped_ptr<net::FileStream> file_stream(new net::FileStream); | |
| 592 if (file_stream->Open(*file_path, base::PLATFORM_FILE_CREATE_ALWAYS | | |
| 593 base::PLATFORM_FILE_WRITE)) { | |
| 594 return NULL; | |
| 595 } | |
| 596 return file_stream.release(); | |
| 597 } | |
| 598 | |
| 599 //////////////////////////////////////////////////////////////////////////////// | |
| 600 // | |
| 601 // ImageBurnUI | |
| 602 // | |
| 603 //////////////////////////////////////////////////////////////////////////////// | |
| 604 ImageBurnUI::ImageBurnUI(TabContents* contents) : DOMUI(contents) { | |
| 605 ImageBurnHandler* handler = new ImageBurnHandler(contents); | |
| 606 AddMessageHandler((handler)->Attach(this)); | |
| 607 ImageBurnUIHTMLSource* html_source = new ImageBurnUIHTMLSource(); | |
| 608 ChromeThread::PostTask( | |
| 609 ChromeThread::IO, FROM_HERE, | |
| 610 NewRunnableMethod( | |
| 611 Singleton<ChromeURLDataManager>::get(), | |
| 612 &ChromeURLDataManager::AddDataSource, | |
| 613 make_scoped_refptr(html_source))); | |
| 614 } | |
| OLD | NEW |