summaryrefslogtreecommitdiffstats
path: root/chrome/browser/views/download_tab_view.cc
blob: e4edec9e9eabe1d755d80a137781e60478da139d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/views/download_tab_view.h"

#include <time.h>

#include <algorithm>
#include <functional>

#include "base/file_util.h"
#include "base/string_util.h"
#include "base/task.h"
#include "base/time_format.h"
#include "base/timer.h"
#include "chrome/app/theme/theme_resources.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/user_metrics.h"
#include "chrome/common/gfx/chrome_canvas.h"
#include "chrome/common/gfx/chrome_font.h"
#include "chrome/common/l10n_util.h"
#include "chrome/common/resource_bundle.h"
#include "chrome/common/stl_util-inl.h"
#include "chrome/common/time_format.h"
#include "chrome/views/background.h"
#include "googleurl/src/gurl.h"
#include "generated_resources.h"

// Approximate spacing, in pixels, taken from initial UI mock up screens
static const int kVerticalPadding = 5;
static const int kHorizontalLinkPadding = 15;
static const int kHorizontalButtonPadding = 8;

// For vertical and horizontal element spacing
static const int kSpacer = 20;

// Horizontal space between the left edge of the entries and the
// left edge of the view.
static const int kLeftMargin = 38;

// x-position of the icon (massage this so it visually matches
// kDestinationSearchOffset in native_ui_contents.cc
static const int kDownloadIconOffset = 132;

// Padding between the progress icon and the title, url
static const int kInfoPadding = 16;

// Horizontal distance from the left window edge to the left icon edge
static const int kDateSize = 132;

// Maximum size of the text for the file name or URL
static const int kFilenameSize = 350;

// Maximum size of the progress text during download, which is taken
// out of kFilenameSize
static const int kProgressSize = 170;

// Status label color (grey)
static const SkColor kStatusColor = SkColorSetRGB(128, 128, 128);

// URL label color (green)
static const SkColor kUrlColor = SkColorSetRGB(0, 128, 0);

// Paused download indicator (red)
static const SkColor kPauseColor = SkColorSetRGB(128, 0, 0);

// Warning label color (blue)
static const SkColor kWarningColor = SkColorSetRGB(87, 108, 149);

// Selected item background color
static const SkColor kSelectedItemColor = SkColorSetRGB(215, 232, 255);

// State key used to identify search text.
static const wchar_t kSearchTextKey[] = L"st";

// The maximum number of characters we show in a file name when displaying the
// dangerous download message.
static const int kFileNameMaxLength = 20;

// Sorting functor for DownloadItem --------------------------------------------

// Sort DownloadItems into ascending order by their start time.
class DownloadItemSorter : public std::binary_function<DownloadItem*,
                                                       DownloadItem*,
                                                       bool> {
 public:
  bool operator()(const DownloadItem* lhs, const DownloadItem* rhs) {
    return lhs->start_time() < rhs->start_time();
  }
};


// DownloadItemTabView implementation ------------------------------------------
DownloadItemTabView::DownloadItemTabView()
    : model_(NULL),
      parent_(NULL),
      is_floating_view_renderer_(false) {
  // Create our element views using empty strings for now,
  // set them based on the model's state in Layout().
  since_ = new ChromeViews::Label(L"");
  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
  ChromeFont font = rb.GetFont(ResourceBundle::WebFont);
  since_->SetHorizontalAlignment(ChromeViews::Label::ALIGN_LEFT);
  since_->SetFont(font);
  AddChildView(since_);

  date_ = new ChromeViews::Label(L"");
  date_->SetColor(kStatusColor);
  date_->SetHorizontalAlignment(ChromeViews::Label::ALIGN_LEFT);
  date_->SetFont(font);
  AddChildView(date_);

  // file_name_ is enabled once the download has finished and we can open
  // it via ShellExecute.
  file_name_ = new ChromeViews::Link(L"");
  file_name_->SetHorizontalAlignment(ChromeViews::Label::ALIGN_LEFT);
  file_name_->SetController(this);
  file_name_->SetFont(font);
  AddChildView(file_name_);

  // dangerous_download_warning_ is enabled when a dangerous download has been
  // initiated.
  dangerous_download_warning_ = new ChromeViews::Label();
  dangerous_download_warning_ ->SetMultiLine(true);
  dangerous_download_warning_->SetColor(kWarningColor);
  dangerous_download_warning_->SetHorizontalAlignment(
      ChromeViews::Label::ALIGN_LEFT);
  dangerous_download_warning_->SetFont(font);
  AddChildView(dangerous_download_warning_);

  // The save and discard buttons are shown to prompt the user when a dangerous
  // download was started.
  save_button_ = new ChromeViews::NativeButton(
      l10n_util::GetString(IDS_SAVE_DOWNLOAD));
  save_button_->set_enforce_dlu_min_size(false);
  save_button_->SetListener(this);
  discard_button_ = new ChromeViews::NativeButton(
      l10n_util::GetString(IDS_DISCARD_DOWNLOAD));
  discard_button_->SetListener(this);
  discard_button_->set_enforce_dlu_min_size(false);
  AddChildView(save_button_);
  AddChildView(discard_button_);

  // Set our URL name
  download_url_ = new ChromeViews::Label(L"");
  download_url_->SetColor(kUrlColor);
  download_url_->SetHorizontalAlignment(ChromeViews::Label::ALIGN_LEFT);
  download_url_->SetFont(font);
  AddChildView(download_url_);

  // Set our time remaining
  time_remaining_ = new ChromeViews::Label(L"");
  time_remaining_->SetColor(kStatusColor);
  time_remaining_->SetHorizontalAlignment(ChromeViews::Label::ALIGN_LEFT);
  time_remaining_->SetFont(font);
  AddChildView(time_remaining_);

  // Set our download progress
  download_progress_ = new ChromeViews::Label(L"");
  download_progress_->SetColor(kStatusColor);
  download_progress_->SetHorizontalAlignment(ChromeViews::Label::ALIGN_LEFT);
  download_progress_->SetFont(font);
  AddChildView(download_progress_);

  // Set our 'Pause', 'Cancel' and 'Show in folder' links using
  // actual strings, since these are constant
  pause_ = new ChromeViews::Link(l10n_util::GetString(IDS_DOWNLOAD_LINK_PAUSE));
  pause_->SetController(this);
  pause_->SetFont(font);
  AddChildView(pause_);

  cancel_ = new ChromeViews::Link(
                  l10n_util::GetString(IDS_DOWNLOAD_LINK_CANCEL));
  cancel_->SetController(this);
  cancel_->SetFont(font);
  AddChildView(cancel_);

  show_ = new ChromeViews::Link(l10n_util::GetString(IDS_DOWNLOAD_LINK_SHOW));
  show_->SetController(this);
  show_->SetFont(font);
  AddChildView(show_);
}

DownloadItemTabView::~DownloadItemTabView() {
}

void DownloadItemTabView::SetModel(DownloadItem* model,
                                   DownloadTabView* parent) {
  DCHECK(model && parent);
  model_ = model;
  parent_ = parent;
  parent_->LookupIcon(model_);
}

void DownloadItemTabView::GetPreferredSize(CSize* out) {
  CSize pause_size;
  pause_->GetPreferredSize(&pause_size);
  CSize cancel_size;
  cancel_->GetPreferredSize(&cancel_size);
  CSize show_size;
  show_->GetPreferredSize(&show_size);

  out->cx = download_util::kBigProgressIconSize +
            2 * kSpacer +
            kHorizontalLinkPadding +
            kFilenameSize +
            std::max(pause_size.cx + cancel_size.cx + kHorizontalLinkPadding,
                     show_size.cx);

  out->cy = download_util::kBigProgressIconSize;
}

// Each DownloadItemTabView has reasonably complex layout requirements
// that are based on the state of its model. To make the code much simpler
// to read, Layout() is split into state specific code which will result
// in some redundant code.
void DownloadItemTabView::Layout() {
  DCHECK(model_);
  switch (model_->state()) {
    case DownloadItem::COMPLETE:
      if (model_->safety_state() == DownloadItem::DANGEROUS)
        LayoutPromptDangerousDownload();
      else
        LayoutComplete();
      break;
    case DownloadItem::CANCELLED:
      LayoutCancelled();
      break;
    case DownloadItem::IN_PROGRESS:
      if (model_->safety_state() == DownloadItem::DANGEROUS)
        LayoutPromptDangerousDownload();
      else
        LayoutInProgress();
      break;
    case DownloadItem::REMOVING:
      break;
    default:
      NOTREACHED();
  }
}

// Only display the date if the download is the last that occurred
// on a given day.
void DownloadItemTabView::LayoutDate() {
  if (!parent_->ShouldDrawDateForDownload(model_)) {
    since_->SetVisible(false);
    date_->SetVisible(false);
    return;
  }

  CSize since_size;

  since_->SetText(TimeFormat::RelativeDate(model_->start_time(), NULL));
  since_->GetPreferredSize(&since_size);
  since_->SetBounds(kLeftMargin, download_util::kBigProgressIconOffset,
                    kDateSize, since_size.cy);
  since_->SetVisible(true);

  CSize date_size;
  date_->SetText(base::TimeFormatShortDate(model_->start_time()));
  date_->GetPreferredSize(&date_size);
  date_->SetBounds(kLeftMargin, since_size.cy + kVerticalPadding +
                      download_util::kBigProgressIconOffset,
                   kDateSize, date_size.cy);
  date_->SetVisible(true);
}

// DownloadItem::COMPLETE state layout
void DownloadItemTabView::LayoutComplete() {
  // Hide unused UI elements
  pause_->SetVisible(false);
  pause_->SetEnabled(false);
  cancel_->SetVisible(false);
  cancel_->SetEnabled(false);
  time_remaining_->SetVisible(false);
  download_progress_->SetVisible(false);
  dangerous_download_warning_->SetVisible(false);
  save_button_->SetVisible(false);
  save_button_->SetEnabled(false);
  discard_button_->SetVisible(false);
  discard_button_->SetEnabled(false);

  LayoutDate();
  int dx = kDownloadIconOffset - download_util::kBigProgressIconOffset +
           download_util::kBigProgressIconSize + kInfoPadding;

  // File name and URL
  CSize file_name_size;
  file_name_->SetText(model_->file_name());
  file_name_->GetPreferredSize(&file_name_size);
  file_name_->SetBounds(dx, download_util::kBigProgressIconOffset,
                        std::min(kFilenameSize,
                                 static_cast<int>(file_name_size.cx)),
                        file_name_size.cy);
  file_name_->SetVisible(true);
  file_name_->SetEnabled(true);

  GURL url(model_->url());
  download_url_->SetURL(url);
  CSize url_size;
  download_url_->GetPreferredSize(&url_size);
  download_url_->SetBounds(dx,
                           file_name_size.cy + kVerticalPadding +
                              download_util::kBigProgressIconOffset,
                           std::min(kFilenameSize,
                                    static_cast<int>(width() - dx)),
                           url_size.cy);
  download_url_->SetVisible(true);
  dx += kFilenameSize + kSpacer;

  // Action button (text is constant and set in constructor)
  CSize show_size;
  show_->GetPreferredSize(&show_size);
  show_->SetBounds(dx, ((file_name_size.cy + url_size.cy) / 2) +
                      download_util::kBigProgressIconOffset,
                   show_size.cx, show_size.cy);
  show_->SetVisible(true);
  show_->SetEnabled(true);
}

// DownloadItem::CANCELLED state layout
void DownloadItemTabView::LayoutCancelled() {
  // Hide unused UI elements
  show_->SetVisible(false);
  show_->SetEnabled(false);
  pause_->SetVisible(false);
  pause_->SetEnabled(false);
  cancel_->SetVisible(false);
  cancel_->SetEnabled(false);
  dangerous_download_warning_->SetVisible(false);
  save_button_->SetVisible(false);
  save_button_->SetEnabled(false);
  discard_button_->SetVisible(false);
  discard_button_->SetEnabled(false);

  LayoutDate();
  int dx = kDownloadIconOffset - download_util::kBigProgressIconOffset +
      download_util::kBigProgressIconSize + kInfoPadding;

  // File name and URL, truncated to show cancelled status
  CSize file_name_size;
  file_name_->SetText(model_->file_name());
  file_name_->GetPreferredSize(&file_name_size);
  file_name_->SetBounds(dx, download_util::kBigProgressIconOffset,
                        kFilenameSize - kProgressSize - kSpacer,
                        file_name_size.cy);
  file_name_->SetVisible(true);
  file_name_->SetEnabled(false);

  GURL url(model_->url());
  download_url_->SetURL(url);
  CSize url_size;
  download_url_->GetPreferredSize(&url_size);
  download_url_->SetBounds(dx,
                           file_name_size.cy + kVerticalPadding +
                              download_util::kBigProgressIconOffset,
                           std::min(kFilenameSize - kProgressSize - kSpacer,
                                    static_cast<int>(width() - dx)),
                           url_size.cy);
  download_url_->SetVisible(true);

  dx += kFilenameSize - kProgressSize;

  // Display cancelled status
  CSize cancel_size;
  time_remaining_->SetColor(kStatusColor);
  time_remaining_->SetText(l10n_util::GetString(IDS_DOWNLOAD_TAB_CANCELLED));
  time_remaining_->GetPreferredSize(&cancel_size);
  time_remaining_->SetBounds(dx, download_util::kBigProgressIconOffset,
                             kProgressSize, cancel_size.cy);
  time_remaining_->SetVisible(true);

  // Display received size, we may not know the total size if the server didn't
  // provide a content-length.
  int64 total = model_->total_bytes();
  int64 size = model_->received_bytes();
  DataUnits amount_units = GetByteDisplayUnits(size);
  std::wstring received_size = FormatBytes(size, amount_units, true);
  std::wstring amount = received_size;

  // We don't know which string we'll end up using for constructing the final
  // progress string so we need to adjust both strings for the locale
  // direction.
  std::wstring amount_localized;
  if (l10n_util::AdjustStringForLocaleDirection(amount, &amount_localized)) {
    amount.assign(amount_localized);
    received_size.assign(amount_localized);
  }

  if (total > 0) {
    amount_units = GetByteDisplayUnits(total);
    std::wstring total_text = FormatBytes(total, amount_units, true);
    std::wstring total_text_localized;
    if (l10n_util::AdjustStringForLocaleDirection(total_text,
                                                  &total_text_localized))
      total_text.assign(total_text_localized);

    // Note that there is no need to adjust the new amount string for the
    // locale direction as ChromeViews::Label does that for us.
    amount = l10n_util::GetStringF(IDS_DOWNLOAD_TAB_PROGRESS_SIZE,
                                   received_size,
                                   total_text);
  }

  CSize byte_size;
  download_progress_->SetText(amount);
  download_progress_->GetPreferredSize(&byte_size);
  download_progress_->SetBounds(dx,
                                file_name_size.cy + kVerticalPadding +
                                download_util::kBigProgressIconOffset,
                                kProgressSize,
                                byte_size.cy);
  download_progress_->SetVisible(true);
}

// DownloadItem::IN_PROGRESS state layout
void DownloadItemTabView::LayoutInProgress() {
  // Hide unused UI elements
  show_->SetVisible(false);
  show_->SetEnabled(false);
  dangerous_download_warning_->SetVisible(false);
  save_button_->SetVisible(false);
  save_button_->SetEnabled(false);
  discard_button_->SetVisible(false);
  discard_button_->SetEnabled(false);

  LayoutDate();
  int dx = kDownloadIconOffset - download_util::kBigProgressIconOffset +
            download_util::kBigProgressIconSize +
           kInfoPadding;

  // File name and URL, truncated to show progress status
  CSize file_name_size;
  file_name_->SetText(model_->GetFileName());
  file_name_->GetPreferredSize(&file_name_size);
  file_name_->SetBounds(dx, download_util::kBigProgressIconOffset,
                        kFilenameSize - kProgressSize - kSpacer,
                        file_name_size.cy);
  file_name_->SetVisible(true);
  file_name_->SetEnabled(false);

  GURL url(model_->url());
  download_url_->SetURL(url);
  CSize url_size;
  download_url_->GetPreferredSize(&url_size);
  download_url_->SetBounds(dx, file_name_size.cy + kVerticalPadding +
                           download_util::kBigProgressIconOffset,
                           std::min(kFilenameSize - kProgressSize - kSpacer,
                                    static_cast<int>(width() - dx)),
                           url_size.cy);
  download_url_->SetVisible(true);

  dx += kFilenameSize - kProgressSize;

  // Set the time remaining and progress display strings. This can
  // be complicated by not having received the total download size
  // In that case, we can't calculate time remaining so we just
  // display speed and received size.

  // Size
  int64 total = model_->total_bytes();
  int64 size = model_->received_bytes();
  DataUnits amount_units = GetByteDisplayUnits(size);
  std::wstring received_size = FormatBytes(size, amount_units, true);
  std::wstring amount = received_size;

  // Adjust both strings for the locale direction since we don't yet know which
  // string we'll end up using for constructing the final progress string.
  std::wstring amount_localized;
  if (l10n_util::AdjustStringForLocaleDirection(amount, &amount_localized)) {
    amount.assign(amount_localized);
    received_size.assign(amount_localized);
  }

  if (total > 0) {
    amount_units = GetByteDisplayUnits(total);
    std::wstring total_text = FormatBytes(total, amount_units, true);
    std::wstring total_text_localized;
    if (l10n_util::AdjustStringForLocaleDirection(total_text,
                                                  &total_text_localized))
      total_text.assign(total_text_localized);

    amount = l10n_util::GetStringF(IDS_DOWNLOAD_TAB_PROGRESS_SIZE,
                                   received_size,
                                   total_text);

    // We adjust the 'amount' string in case we use it as part of the progress
    // text.
    if (l10n_util::AdjustStringForLocaleDirection(amount, &amount_localized))
      amount.assign(amount_localized);
  }

  // Speed
  int64 speed = model_->CurrentSpeed();
  std::wstring progress = amount;
  if (!model_->is_paused() && speed > 0) {
    amount_units = GetByteDisplayUnits(speed);
    std::wstring speed_text = FormatSpeed(speed, amount_units, true);
    std::wstring speed_text_localized;
    if (l10n_util::AdjustStringForLocaleDirection(speed_text,
                                                  &speed_text_localized))
      speed_text.assign(speed_text_localized);

    progress = l10n_util::GetStringF(IDS_DOWNLOAD_TAB_PROGRESS_SPEED,
                                     speed_text,
                                     amount);

    // For some reason, the appearance of the dash character ('-') in a string
    // causes Windows to ignore the 'LRE'/'RLE'/'PDF' Unicode formatting
    // characters within the string and this causes the string to be displayed
    // incorrectly on RTL UIs. Therefore, we add the Unicode right-to-left
    // override character (U+202E) if the locale is RTL in order to fix this
    // problem.
    if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT)
      progress.insert(0, L"\x202E");
  }

  // Time remaining
  int y_pos = file_name_size.cy + kVerticalPadding +
              download_util::kBigProgressIconOffset;
  CSize time_size;
  time_remaining_->SetColor(kStatusColor);
  if (model_->is_paused()) {
    time_remaining_->SetColor(kPauseColor);
    time_remaining_->SetText(
        l10n_util::GetString(IDS_DOWNLOAD_PROGRESS_PAUSED));
    time_remaining_->GetPreferredSize(&time_size);
    time_remaining_->SetBounds(dx, download_util::kBigProgressIconOffset,
                               kProgressSize, time_size.cy);
    time_remaining_->SetVisible(true);
  } else if (total > 0)  {
    TimeDelta remaining;
    if (model_->TimeRemaining(&remaining))
      time_remaining_->SetText(TimeFormat::TimeRemaining(remaining));
    time_remaining_->GetPreferredSize(&time_size);
    time_remaining_->SetBounds(dx, download_util::kBigProgressIconOffset,
                               kProgressSize, time_size.cy);
    time_remaining_->SetVisible(true);
  } else {
    time_remaining_->SetText(L"");
    y_pos = ((file_name_size.cy + url_size.cy) / 2) +
            download_util::kBigProgressIconOffset;
  }

  CSize byte_size;
  download_progress_->SetText(progress);
  download_progress_->GetPreferredSize(&byte_size);
  download_progress_->SetBounds(dx, y_pos,
                                kProgressSize, byte_size.cy);
  download_progress_->SetVisible(true);

  dx += kProgressSize + kSpacer;
  y_pos = ((file_name_size.cy + url_size.cy) / 2) +
          download_util::kBigProgressIconOffset;

  // Pause (or Resume) / Cancel buttons.
  if (model_->is_paused())
    pause_->SetText(l10n_util::GetString(IDS_DOWNLOAD_LINK_RESUME));
  else
    pause_->SetText(l10n_util::GetString(IDS_DOWNLOAD_LINK_PAUSE));

  CSize pause_size;
  pause_->SetVisible(true);
  pause_->SetEnabled(true);
  pause_->GetPreferredSize(&pause_size);
  pause_->SetBounds(dx, y_pos, pause_size.cx, pause_size.cy);

  dx += pause_size.cx + kHorizontalLinkPadding;

  CSize cancel_size;
  cancel_->GetPreferredSize(&cancel_size);
  cancel_->SetBounds(dx, y_pos, cancel_size.cx, cancel_size.cy);
  cancel_->SetVisible(true);
  cancel_->SetEnabled(true);
}

void DownloadItemTabView::LayoutPromptDangerousDownload() {
  // Hide unused UI elements
  show_->SetVisible(false);
  show_->SetEnabled(false);
  file_name_->SetVisible(false);
  file_name_->SetEnabled(false);
  pause_->SetVisible(false);
  pause_->SetEnabled(false);
  cancel_->SetVisible(false);
  cancel_->SetEnabled(false);
  time_remaining_->SetVisible(false);
  download_progress_->SetVisible(false);

  LayoutDate();
  int dx = kDownloadIconOffset - download_util::kBigProgressIconOffset +
           download_util::kBigProgressIconSize +
           kInfoPadding;

  // Warning message and URL.
  CSize warning_size;
  std::wstring file_name;
  ElideString(model_->original_name(), kFileNameMaxLength, &file_name);
  dangerous_download_warning_->SetText(
      l10n_util::GetStringF(IDS_PROMPT_DANGEROUS_DOWNLOAD, file_name));
  dangerous_download_warning_->GetPreferredSize(&warning_size);
  dangerous_download_warning_->SetBounds(dx, 0,
                                         kFilenameSize, warning_size.cy);
  dangerous_download_warning_->SetVisible(true);

  GURL url(model_->url());
  download_url_->SetURL(url);
  CSize url_size;
  download_url_->GetPreferredSize(&url_size);
  download_url_->SetBounds(dx, height() - url_size.cy,
                           std::min(kFilenameSize - kSpacer,
                                    static_cast<int>(width() - dx)),
                           url_size.cy);
  download_url_->SetVisible(true);

  dx += kFilenameSize + kSpacer;

  // Save/Discard buttons.
  CSize button_size;
  save_button_->GetPreferredSize(&button_size);
  save_button_->SetBounds(dx, (height() - button_size.cy) / 2,
                          button_size.cx, button_size.cy);
  save_button_->SetVisible(true);
  save_button_->SetEnabled(true);

  dx += button_size.cx + kHorizontalButtonPadding;

  discard_button_->GetPreferredSize(&button_size);
  discard_button_->SetBounds(dx, (height() - button_size.cy) / 2,
                             button_size.cx, button_size.cy);
  discard_button_->SetVisible(true);
  discard_button_->SetEnabled(true);
}

void DownloadItemTabView::Paint(ChromeCanvas* canvas) {
  PaintBackground(canvas);

  if (model_->state() == DownloadItem::IN_PROGRESS  &&
      model_->safety_state() != DownloadItem::DANGEROUS) {
    download_util::PaintDownloadProgress(canvas,
                                         this,
                                         kDownloadIconOffset -
                                         download_util::kBigProgressIconOffset,
                                         0,
                                         parent_->start_angle(),
                                         model_->PercentComplete(),
                                         download_util::BIG);
  }

  // Most of the UI elements in the DownloadItemTabView are represented as
  // child Views and therefore they get mirrored automatically in
  // right-to-left UIs. The download item icon is not contained within a child
  // View so we need to mirror it manually if the locale is RTL.
  SkBitmap* icon = parent_->LookupIcon(model_);
  if (icon) {
    gfx::Rect icon_bounds(kDownloadIconOffset,
                          download_util::kBigProgressIconOffset,
                          icon->width(), icon->height());
    icon_bounds.set_x(MirroredLeftPointForRect(icon_bounds));
    canvas->DrawBitmapInt(*icon, icon_bounds.x(), icon_bounds.y());
  }
}

void DownloadItemTabView::PaintBackground(ChromeCanvas* canvas) {
  if (parent_->ItemIsSelected(model_)) {
    // Before we paint the border and the focus rect, we need to mirror the
    // highlighted area if the View is using a right-to-left UI layout. We need
    // to explicitly mirror the position because the highlighted area is
    // directly painted on the canvas (as opposed to being represented as a
    // child View like the rest of the UI elements in DownloadItemTabView).
    gfx::Rect highlighted_bounds(kDownloadIconOffset -
                                    download_util::kBigProgressIconOffset,
                                 0,
                                 download_util::kBigProgressIconSize +
                                    kInfoPadding + kFilenameSize,
                                 download_util::kBigProgressIconSize);
    highlighted_bounds.set_x(MirroredLeftPointForRect(highlighted_bounds));

    canvas->FillRectInt(kSelectedItemColor,
                        highlighted_bounds.x(),
                        highlighted_bounds.y(),
                        highlighted_bounds.width(),
                        highlighted_bounds.height());

    canvas->DrawFocusRect(highlighted_bounds.x(),
                          highlighted_bounds.y(),
                          highlighted_bounds.width(),
                          highlighted_bounds.height());
  }
}

void DownloadItemTabView::DidChangeBounds(const CRect& previous,
                                          const CRect& current) {
  Layout();
}

bool DownloadItemTabView::OnMousePressed(const ChromeViews::MouseEvent& event) {
  gfx::Point point(event.location());

  // If the click is in the highlight region, then highlight this download.
  // Otherwise, remove the highlighting from any download.
  gfx::Rect select_rect(
      kDownloadIconOffset - download_util::kBigProgressIconOffset,
      0,
      kDownloadIconOffset - download_util::kBigProgressIconOffset +
          download_util::kBigProgressIconSize + kInfoPadding + kFilenameSize,
      download_util::kBigProgressIconSize);

  // The position of the highlighted region does not take into account the
  // View's UI layout so we have to manually mirror the position if the View is
  // using a right-to-left UI layout.
  gfx::Rect mirrored_rect(select_rect);
  select_rect.set_x(MirroredLeftPointForRect(mirrored_rect));
  if (select_rect.Contains(point)) {
    parent_->ItemBecameSelected(model_);

    // Don't show the right-click menu if we are prompting the user for a
    // dangerous download.
    if (event.IsRightMouseButton() &&
        model_->safety_state() != DownloadItem::DANGEROUS) {
      ChromeViews::View::ConvertPointToScreen(this, &point);

      download_util::DownloadDestinationContextMenu menu(
          model_, GetViewContainer()->GetHWND(), point.ToPOINT());
    }
  } else {
    parent_->ItemBecameSelected(NULL);
  }

  return true;
}

// Handle drag (file copy) operations.
bool DownloadItemTabView::OnMouseDragged(const ChromeViews::MouseEvent& event) {
  if (model_->state() != DownloadItem::COMPLETE ||
      model_->safety_state() == DownloadItem::DANGEROUS)
    return false;

  CPoint point(event.x(), event.y());

  // In order to make sure drag and drop works as expected when the UI is
  // mirrored, we can either flip the mouse X coordinate or flip the X position
  // of the drag rectangle. Flipping the mouse X coordinate is easier.
  point.x = MirroredXCoordinateInsideView(point.x);
  CRect drag_rect(kDownloadIconOffset - download_util::kBigProgressIconOffset,
                  0,
                  kDownloadIconOffset - download_util::kBigProgressIconOffset +
                      download_util::kBigProgressIconSize + kInfoPadding +
                      kFilenameSize,
                  download_util::kBigProgressIconSize);

  if (drag_rect.PtInRect(point)) {
    SkBitmap* icon = parent_->LookupIcon(model_);
    if (icon)
      download_util::DragDownload(model_, icon);
  }

  return true;
}

void DownloadItemTabView::LinkActivated(ChromeViews::Link* source,
                                        int event_flags) {
  // There are several links in our view that could have been clicked:
  if (source == file_name_) {
    ChromeViews::ViewContainer* container = this->GetViewContainer();
    HWND parent_window = container ? container->GetHWND() : NULL;
    model_->manager()->OpenDownloadInShell(model_, parent_window);
  } else if (source == pause_) {
    model_->TogglePause();
  } else if (source == cancel_) {
    model_->Cancel(true /* update history service */);
  } else if (source == show_) {
    model_->manager()->ShowDownloadInShell(model_);
  } else {
    NOTREACHED();
  }

  parent_->ItemBecameSelected(model_);
}

void DownloadItemTabView::ButtonPressed(ChromeViews::NativeButton* sender) {
  if (sender == save_button_) {
    parent_->model()->DangerousDownloadValidated(model_);
    // Relayout and repaint to display the right mode (complete or in progress).
    Layout();
    SchedulePaint();
  } else if (sender == discard_button_) {
    model_->Remove(true);
  } else  {
    NOTREACHED();
  }
}

// DownloadTabView implementation ----------------------------------------------

DownloadTabView::DownloadTabView(DownloadManager* model)
    : model_(model),
      start_angle_(download_util::kStartAngleDegrees),
      scroll_helper_(kSpacer, download_util::kBigProgressIconSize + kSpacer),
      selected_index_(-1) {
  DCHECK(model_);
}

DownloadTabView::~DownloadTabView() {
  StopDownloadProgress();
  model_->RemoveObserver(this);

  // DownloadManager owns the contents.
  downloads_.clear();
  ClearDownloadInProgress();
  ClearDangerousDownloads();

  icon_consumer_.CancelAllRequests();
}

void DownloadTabView::Initialize() {
  model_->AddObserver(this);
}

// Start progress animation timers when we get our first (in-progress) download.
void DownloadTabView::StartDownloadProgress() {
  if (progress_timer_.IsRunning())
    return;
  progress_timer_.Start(
      TimeDelta::FromMilliseconds(download_util::kProgressRateMs), this,
      &DownloadTabView::UpdateDownloadProgress);
}

// Stop progress animation when there are no more in-progress downloads.
void DownloadTabView::StopDownloadProgress() {
  progress_timer_.Stop();
}

// Update our animations.
void DownloadTabView::UpdateDownloadProgress() {
  start_angle_ = (start_angle_ + download_util::kUnknownIncrementDegrees) %
                 download_util::kMaxDegrees;
  SchedulePaint();
}

void DownloadTabView::DidChangeBounds(const CRect& previous,
                                      const CRect& current) {
  Layout();
}

void DownloadTabView::Layout() {
  CRect r;
  DetachAllFloatingViews();
  // Dangerous downloads items use NativeButtons, so they need to be attached
  // as NativeControls are not supported yet in floating views.
  gfx::Rect visible_bounds = GetVisibleBounds();
  int row_start = (visible_bounds.y() - kSpacer) /
                  (download_util::kBigProgressIconSize + kSpacer);
  int row_stop = (visible_bounds.y() - kSpacer + visible_bounds.height()) /
                 (download_util::kBigProgressIconSize + kSpacer);
  row_stop = std::min(row_stop, static_cast<int>(downloads_.size()) - 1);
  for (int i = row_start; i <= row_stop; ++i) {
    // The DownloadManager stores downloads earliest first, but this view
    // displays latest first, so adjust the index:
    int index = static_cast<int>(downloads_.size()) - 1 - i;
    if (downloads_[index]->safety_state() == DownloadItem::DANGEROUS)
      ValidateFloatingViewForID(index);
  }
  View* v = GetParent();
  if (v) {
    v->GetLocalBounds(&r, true);
    int h = static_cast<int>(downloads_.size()) *
            (download_util::kBigProgressIconSize + kSpacer) + kSpacer;
    SetBounds(x(), y(), v->width(), h);
  }
}

// Paint our scrolled region
void DownloadTabView::Paint(ChromeCanvas* canvas) {
  ChromeViews::View::Paint(canvas);

  if (download_util::kBigIconSize == 0 || downloads_.size() == 0)
    return;

  SkRect clip;
  if (canvas->getClipBounds(&clip)) {
    int row_start = (SkScalarRound(clip.fTop) - kSpacer) /
                    (download_util::kBigProgressIconSize + kSpacer);
    int row_stop = SkScalarRound(clip.fBottom) /
                   (download_util::kBigProgressIconSize + kSpacer);
    SkRect download_rect;
    for (int i = row_start; i <= row_stop; ++i) {
      int y = i * (download_util::kBigProgressIconSize + kSpacer) + kSpacer;
      if (HasFloatingViewForPoint(0, y))
        continue;
      download_rect.set(SkIntToScalar(0),
                        SkIntToScalar(y),
                        SkIntToScalar(width()),
                        SkIntToScalar(y + download_util::kBigProgressIconSize));
      if (SkRect::Intersects(clip, download_rect)) {
        // The DownloadManager stores downloads earliest first, but this
        // view displays latest first, so adjust the index:
        int index = static_cast<int>(downloads_.size()) - 1 - i;
        download_renderer_.SetModel(downloads_[index], this);
        PaintFloatingView(canvas, &download_renderer_,
                          0, y,
                          width(), download_util::kBigProgressIconSize);
      }
    }
  }
}

// Draw the DownloadItemTabView for the current position.
bool DownloadTabView::GetFloatingViewIDForPoint(int x, int y, int* id) {
  if (y < kSpacer ||
      y > (kSpacer + download_util::kBigProgressIconSize) *
          static_cast<int>(downloads_.size()))
    return false;

  // Are we hovering over a download or the spacer? If we're over the
  // download, create a floating view for it.
  if ((y - kSpacer) % (download_util::kBigProgressIconSize + kSpacer) <
      download_util::kBigProgressIconSize) {
    int row = y / (download_util::kBigProgressIconSize + kSpacer);
    *id = static_cast<int>(downloads_.size()) - 1 - row;
    return true;
  }
  return false;
}

ChromeViews::View* DownloadTabView::CreateFloatingViewForIndex(int index) {
  if (index >= static_cast<int>(downloads_.size())) {
    // It's possible that the downloads have been cleared via the "Clear
    // Browsing Data" command, so this index is gone.
    return NULL;
  }

  DownloadItemTabView* dl = new DownloadItemTabView();
  // We attach the view before layout as the Save/Discard buttons are native
  // and need to be in the tree hierarchy to compute their preferred size
  // correctly.
  AttachFloatingView(dl, index);
  dl->SetModel(downloads_[index], this);
  int row = static_cast<int>(downloads_.size()) - 1 - index;
  int y_pos = row * (download_util::kBigProgressIconSize + kSpacer) + kSpacer;
  dl->SetBounds(0, y_pos, width(), download_util::kBigProgressIconSize);
  dl->Layout();
  return dl;
}

bool DownloadTabView::EnumerateFloatingViews(
      ChromeViews::View::FloatingViewPosition position,
      int starting_id, int* id) {
  DCHECK(id);
  return View::EnumerateFloatingViewsForInterval(
      0, static_cast<int>(downloads_.size()), false, position, starting_id, id);
}

ChromeViews::View* DownloadTabView::ValidateFloatingViewForID(int id) {
  return CreateFloatingViewForIndex(id);
}

void DownloadTabView::OnDownloadUpdated(DownloadItem* download) {
  switch (download->state()) {
    case DownloadItem::COMPLETE:
    case DownloadItem::CANCELLED: {
      base::hash_set<DownloadItem*>::iterator d = in_progress_.find(download);
      if (d != in_progress_.end()) {
        // If this is a dangerous download not yet validated by the user, we
        // still need to be notified when the validation happens.
        if (download->safety_state() != DownloadItem::DANGEROUS)
          (*d)->RemoveObserver(this);
        in_progress_.erase(d);
      }
      if (in_progress_.empty())
        StopDownloadProgress();
      LoadIcon(download);
      break;
    }
    case DownloadItem::IN_PROGRESS: {
      // If all IN_PROGRESS downloads are paused, don't waste CPU issuing any
      // further progress updates until at least one download is active again.
      if (download->is_paused()) {
        bool continue_update = false;
        base::hash_set<DownloadItem*>::iterator it = in_progress_.begin();
        for (; it != in_progress_.end(); ++it) {
          if (!(*it)->is_paused()) {
            continue_update = true;
            break;
          }
        }
        if (!continue_update)
          StopDownloadProgress();
      } else {
        StartDownloadProgress();
      }
      break;
    }
    case DownloadItem::REMOVING:
      // Handled below.
      break;
    default:
      NOTREACHED();
      break;
  }

  OrderedDownloads::iterator it = find(downloads_.begin(),
                                       downloads_.end(),
                                       download);
  if (it == downloads_.end())
    return;

  const int index = static_cast<int>(it - downloads_.begin());
  DownloadItemTabView* view =
      static_cast<DownloadItemTabView*>(RetrieveFloatingViewForID(index));
  if (view) {
    if (download->state() != DownloadItem::REMOVING) {
      view->Layout();
      SchedulePaintForViewAtIndex(index);
    } else if (selected_index_ == index) {
      selected_index_ = -1;
    }
  }
}

// A download has started or been deleted. Query our DownloadManager for the
// current set of downloads, which will call us back in SetDownloads once it
// has retrieved them.
void DownloadTabView::ModelChanged() {
  downloads_.clear();
  ClearDownloadInProgress();
  ClearDangerousDownloads();
  DetachAllFloatingViews();

  // Issue the query.
  model_->GetDownloads(this, search_text_);
}

void DownloadTabView::SetDownloads(std::vector<DownloadItem*>& downloads) {
  // Stop progress timers.
  StopDownloadProgress();

  // Clear out old state and remove self as observer for each download.
  downloads_.clear();
  ClearDownloadInProgress();
  ClearDangerousDownloads();

  // Swap new downloads in.
  downloads_.swap(downloads);
  sort(downloads_.begin(), downloads_.end(), DownloadItemSorter());

  // Scan for any in progress downloads and add ourself to them as an observer.
  for (OrderedDownloads::iterator it = downloads_.begin();
       it != downloads_.end(); ++it) {
    DownloadItem* download = *it;
    if (download->state() == DownloadItem::IN_PROGRESS) {
      download->AddObserver(this);
      in_progress_.insert(download);
    } else if (download->safety_state() == DownloadItem::DANGEROUS) {
      // We need to be notified when the user validates the dangerous download.
      download->AddObserver(this);
      dangerous_downloads_.insert(download);
    }
  }

  // Start any progress timers if required.
  if (!in_progress_.empty())
    StartDownloadProgress();

  // Update the UI.
  selected_index_ = -1;
  GetParent()->GetParent()->Layout();
  SchedulePaint();
}


// If we have the icon in our cache, then return it. If not, look it up via the
// IconManager. Ignore in progress requests (duplicates).
SkBitmap* DownloadTabView::LookupIcon(DownloadItem* download) {
  IconManager* im = g_browser_process->icon_manager();
  // Fast look up.
  SkBitmap* icon = im->LookupIcon(download->full_path(), IconLoader::NORMAL);

  // Expensive look up.
  if (!icon)
    LoadIcon(download);

  return icon;
}

// Bypass the caches and perform the Icon extraction directly. This is useful in
// the case where the download has completed and we want to re-check the file
// to see if it has an embedded icon (which we couldn't do at download start).
void DownloadTabView::LoadIcon(DownloadItem* download) {
  IconManager* im = g_browser_process->icon_manager();
  IconManager::Handle h =
      im->LoadIcon(download->full_path(), IconLoader::NORMAL,
                   &icon_consumer_,
                   NewCallback(this, &DownloadTabView::OnExtractIconComplete));
  icon_consumer_.SetClientData(im, h, download);
}

void DownloadTabView::ClearDownloadInProgress() {
  for (base::hash_set<DownloadItem*>::iterator it = in_progress_.begin();
       it != in_progress_.end(); ++it)
    (*it)->RemoveObserver(this);
  in_progress_.clear();
}

void DownloadTabView::ClearDangerousDownloads() {
  base::hash_set<DownloadItem*>::const_iterator it;
  for (it = dangerous_downloads_.begin();
       it != dangerous_downloads_.end(); ++it)
    (*it)->RemoveObserver(this);
  dangerous_downloads_.clear();
}

// Check to see if the download is the latest download on a given day.
// We use this to determine when to draw the date next to a particular
// download view: if the DownloadItem is the latest download on a given
// day, the date gets drawn.
bool DownloadTabView::ShouldDrawDateForDownload(DownloadItem* download) {
  DCHECK(download);
  OrderedDownloads::iterator it = find(downloads_.begin(),
                                       downloads_.end(),
                                       download);
  DCHECK(it != downloads_.end());
  const int index = static_cast<int>(it - downloads_.begin());

  // If download is the last or only download, it draws the date.
  if (downloads_.size() - 1 == index)
    return true;

  const DownloadItem* next = downloads_[index + 1];

  Time next_midnight = next->start_time().LocalMidnight();
  Time curr_midnight = download->start_time().LocalMidnight();
  if (next_midnight == curr_midnight) {
    // 'next' happened today: let it draw the date so we don't have to.
    return false;
  }
  return true;
}

int DownloadTabView::GetPageScrollIncrement(
    ChromeViews::ScrollView* scroll_view, bool is_horizontal,
    bool is_positive) {
  return scroll_helper_.GetPageScrollIncrement(scroll_view, is_horizontal,
                                               is_positive);
}

int DownloadTabView::GetLineScrollIncrement(
    ChromeViews::ScrollView* scroll_view, bool is_horizontal,
    bool is_positive) {
  return scroll_helper_.GetLineScrollIncrement(scroll_view, is_horizontal,
                                               is_positive);
}

void DownloadTabView::ItemBecameSelected(const DownloadItem* download) {
  int index = -1;
  if (download != NULL) {
    OrderedDownloads::const_iterator it = find(downloads_.begin(),
                                               downloads_.end(),
                                               download);
    DCHECK(it != downloads_.end());
    index = static_cast<int>(it - downloads_.begin());
    if (index == selected_index_)
      return;  // Avoid unnecessary paint.
  }

  if (selected_index_ >= 0)
    SchedulePaintForViewAtIndex(selected_index_);
  if (index >= 0)
    SchedulePaintForViewAtIndex(index);
  selected_index_ = index;
}

bool DownloadTabView::ItemIsSelected(DownloadItem* download) {
  OrderedDownloads::iterator it = find(downloads_.begin(),
                                       downloads_.end(),
                                       download);
  if (it != downloads_.end())
    return selected_index_ == static_cast<int>(it - downloads_.begin());
  return false;
}

void DownloadTabView::SchedulePaintForViewAtIndex(int index) {
  int y = GetYPositionForIndex(index);
  SchedulePaint(0, y, width(), download_util::kBigProgressIconSize);
}

int DownloadTabView::GetYPositionForIndex(int index) {
  int row = static_cast<int>(downloads_.size()) - 1 - index;
  return row * (download_util::kBigProgressIconSize + kSpacer) + kSpacer;
}

void DownloadTabView::SetSearchText(const std::wstring& search_text) {
  search_text_ = search_text;
  model_->GetDownloads(this, search_text_);
}

// The 'icon_bitmap' is ignored here, since it is cached by the IconManager.
// When the paint message runs, we'll use the fast IconManager lookup API to
// retrieve it.
void DownloadTabView::OnExtractIconComplete(IconManager::Handle handle,
                                            SkBitmap* icon_bitmap) {
  IconManager* im = g_browser_process->icon_manager();
  DownloadItem* download = icon_consumer_.GetClientData(im, handle);
  OrderedDownloads::iterator it = find(downloads_.begin(),
                                       downloads_.end(),
                                       download);
  if (it != downloads_.end()) {
    const int index = static_cast<int>(it - downloads_.begin());
    SchedulePaintForViewAtIndex(index);
  }
}

// DownloadTabUIFactory ------------------------------------------------------

class DownloadTabUIFactory : public NativeUIFactory {
 public:
  DownloadTabUIFactory() {}
  virtual ~DownloadTabUIFactory() {}

  virtual NativeUI* CreateNativeUIForURL(const GURL& url,
                                         NativeUIContents* contents) {
    return new DownloadTabUI(contents);
  }

 private:
  DISALLOW_EVIL_CONSTRUCTORS(DownloadTabUIFactory);
};

// DownloadTabUI -------------------------------------------------------------

DownloadTabUI::DownloadTabUI(NativeUIContents* contents)
#pragma warning(suppress: 4355)  // Okay to pass "this" here.
    : searchable_container_(this),
      download_tab_view_(NULL),
      contents_(contents) {
  DownloadManager* dlm = contents_->profile()->GetDownloadManager();
  download_tab_view_ = new DownloadTabView(dlm);
  searchable_container_.SetContents(download_tab_view_);
  download_tab_view_->Initialize();

  NotificationService* ns = NotificationService::current();
  ns->AddObserver(this, NOTIFY_DOWNLOAD_START,
                  NotificationService::AllSources());
  ns->AddObserver(this, NOTIFY_DOWNLOAD_STOP,
                  NotificationService::AllSources());

  // Spin the throbber if there are active downloads, since we may have been
  // created after the NOTIFY_DOWNLOAD_START was sent. If the download manager
  // has not been created, don't bother since it will negatively impact start
  // up time with history requests.
  Profile* profile = contents_->profile();
  if (profile &&
      profile->HasCreatedDownloadManager() &&
      profile->GetDownloadManager()->in_progress_count() > 0)
    contents_->SetIsLoading(true, NULL);
}

DownloadTabUI::~DownloadTabUI() {
  NotificationService* ns = NotificationService::current();
  ns->RemoveObserver(this, NOTIFY_DOWNLOAD_START,
                     NotificationService::AllSources());
  ns->RemoveObserver(this, NOTIFY_DOWNLOAD_STOP,
                     NotificationService::AllSources());
}

const std::wstring DownloadTabUI::GetTitle() const {
  return l10n_util::GetString(IDS_DOWNLOAD_TITLE);
}

const int DownloadTabUI::GetFavIconID() const {
  return IDR_DOWNLOADS_FAVICON;
}

const int DownloadTabUI::GetSectionIconID() const {
  return IDR_DOWNLOADS_SECTION;
}

const std::wstring DownloadTabUI::GetSearchButtonText() const {
  return l10n_util::GetString(IDS_DOWNLOAD_SEARCH_BUTTON);
}

ChromeViews::View* DownloadTabUI::GetView() {
  return &searchable_container_;
}

void DownloadTabUI::WillBecomeVisible(NativeUIContents* parent) {
  UserMetrics::RecordAction(L"Destination_Downloads", parent->profile());
}

void DownloadTabUI::WillBecomeInvisible(NativeUIContents* parent) {
}

void DownloadTabUI::Navigate(const PageState& state) {
  std::wstring search_text;
  state.GetProperty(kSearchTextKey, &search_text);
  download_tab_view_->SetSearchText(search_text);
  searchable_container_.GetSearchField()->SetText(search_text);
}

bool DownloadTabUI::SetInitialFocus() {
  searchable_container_.GetSearchField()->RequestFocus();
  return true;
}

// static
GURL DownloadTabUI::GetURL() {
  std::string spec(NativeUIContents::GetScheme());
  spec.append("://downloads");
  return GURL(spec);
}

// static
NativeUIFactory* DownloadTabUI::GetNativeUIFactory() {
  return new DownloadTabUIFactory();
}

void DownloadTabUI::DoSearch(const std::wstring& new_text) {
  download_tab_view_->SetSearchText(new_text);
  PageState* page_state = contents_->page_state().Copy();
  page_state->SetProperty(kSearchTextKey, new_text);
  contents_->SetPageState(page_state);
}

void DownloadTabUI::Observe(NotificationType type,
                            const NotificationSource& source,
                            const NotificationDetails& details) {
  switch (type) {
    case NOTIFY_DOWNLOAD_START:
    case NOTIFY_DOWNLOAD_STOP:
      DCHECK(profile()->HasCreatedDownloadManager());
      contents_->SetIsLoading(
          profile()->GetDownloadManager()->in_progress_count() > 0,
          NULL);
      break;
    default:
      break;
  }
}