summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/views/web_intent_picker_views.cc
blob: 345808cd4496ac6a6060b198acd2528c5b255ecb (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
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
// Copyright (c) 2012 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 <algorithm>
#include <vector>

#include "base/command_line.h"
#include "base/memory/scoped_vector.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/tab_contents/tab_util.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/intents/web_intent_inline_disposition_delegate.h"
#include "chrome/browser/ui/intents/web_intent_picker.h"
#include "chrome/browser/ui/intents/web_intent_picker_delegate.h"
#include "chrome/browser/ui/intents/web_intent_picker_model.h"
#include "chrome/browser/ui/intents/web_intent_picker_model_observer.h"
#include "chrome/browser/ui/tab_contents/tab_contents.h"
#include "chrome/browser/ui/views/constrained_window_views.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/location_bar/location_icon_view.h"
#include "chrome/browser/ui/views/toolbar_view.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension_constants.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "grit/google_chrome_strings.h"
#include "grit/shared_resources.h"
#include "grit/theme_resources.h"
#include "grit/ui_resources.h"
#include "ipc/ipc_message.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/text/text_elider.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/button/text_button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/link.h"
#include "ui/views/controls/link_listener.h"
#include "ui/views/controls/throbber.h"
#include "ui/views/controls/webview/webview.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/grid_layout.h"
#include "ui/views/layout/layout_constants.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/dialog_delegate.h"
#include "ui/views/window/non_client_view.h"

using content::WebContents;
using views::GridLayout;

namespace {

// The color used to dim disabled elements.
const SkColor kHalfOpacityWhite = SkColorSetARGB(128, 255, 255, 255);

// The color used to display a disabled link.
const SkColor kDisabledLinkColor = SkColorSetRGB(128, 128, 128);

// The time between successive throbber frames in milliseconds.
const int kThrobberFrameTimeMs = 50;

// Width of IntentView action button in pixels
const int kButtonWidth = 130;

// Minimum number of action buttons - fill up with suggestions as needed.
const int kMinRowCount = 4;

// Maximum number of action buttons - do not add suggestions to reach.
const int kMaxRowCount = 8;

// Enables or disables all child views of |view|.
void EnableChildViews(views::View* view, bool enabled) {
  for (int i = 0; i < view->child_count(); ++i) {
    views::View* child = view->child_at(i);
    child->SetEnabled(enabled);
  }
}

// Create a "close" button.
views::ImageButton* CreateCloseButton(views::ButtonListener* listener) {
  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
  views::ImageButton* close_button = new views::ImageButton(listener);
  close_button->SetImage(views::CustomButton::BS_NORMAL,
                         rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X));
  close_button->SetImage(views::CustomButton::BS_HOT,
                         rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X_HOVER));
  close_button->SetImage(views::CustomButton::BS_PUSHED,
                         rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X_HOVER));
  return close_button;
}
// SarsView -------------------------------------------------------------------

// A view that displays 5 stars: empty, full or half full, given a rating in
// the range [0,5].
class StarsView : public views::View {
 public:
  explicit StarsView(double rating);
  virtual ~StarsView();

 private:
  // The star rating to display, in the range [0,5].
  double rating_;

  DISALLOW_COPY_AND_ASSIGN(StarsView);
};

StarsView::StarsView(double rating)
    : rating_(rating) {
  const int kSpacing = 1;  // Spacing between stars in pixels.

  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
  SetLayoutManager(
      new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, kSpacing));

  for (int i = 0; i < 5; ++i) {
    views::ImageView* image = new views::ImageView();
    image->SetImage(rb.GetImageSkiaNamed(
        WebIntentPicker::GetNthStarImageIdFromCWSRating(rating, i)));
    AddChildView(image);
  }

  // TODO(binji): Add tooltip with text rating
  // "Average Rating: X.XX stars (YYYYY)"
  // Y = "1: Hated it, 2: Disliked it, 3: It was okay, 4: Liked it,
  //      5: Loved it"
  // Choose Y based on rounded X.
}

StarsView::~StarsView() {
}

// ThrobberNativeTextButton ----------------------------------------------------

// A native text button that can display a throbber in place of its icon. Much
// of the logic of this class is copied from ui/views/controls/throbber.h.
class ThrobberNativeTextButton : public views::NativeTextButton {
 public:
  ThrobberNativeTextButton(views::ButtonListener* listener,
                           const string16& text);
  virtual ~ThrobberNativeTextButton();

  // Start or stop the throbber.
  void StartThrobber();
  void StopThrobber();

  // Set the throbber bitmap to use. IDR_THROBBER is used by default.
  void SetFrames(const gfx::ImageSkia* frames);

  // Provide a preferred size, accomodating buttons wider than their text.
  virtual gfx::Size GetPreferredSize() OVERRIDE;

  // Set the width desired for this button.
  void set_preferred_width(int width) { preferred_width_ = width; }

 protected:
  virtual const gfx::ImageSkia& GetImageToPaint() const OVERRIDE;

 private:
  // The timer callback to schedule painting this view.
  void Run();

  // Image that contains the throbber frames.
  const gfx::ImageSkia* frames_;

  // The currently displayed frame, given to GetImageToPaint.
  mutable gfx::ImageSkia this_frame_;

  // How long one frame is displayed.
  base::TimeDelta frame_time_;

  // Used to schedule Run calls.
  base::RepeatingTimer<ThrobberNativeTextButton> timer_;

  // How many frames we have.
  int frame_count_;

  // Time when StartThrobber was called.
  base::TimeTicks start_time_;

  // Whether the throbber is shown an animating.
  bool running_;

  // The width this button should assume.
  int preferred_width_;

  DISALLOW_COPY_AND_ASSIGN(ThrobberNativeTextButton);
};

ThrobberNativeTextButton::ThrobberNativeTextButton(
    views::ButtonListener* listener, const string16& text)
    : NativeTextButton(listener, text),
      frame_time_(base::TimeDelta::FromMilliseconds(kThrobberFrameTimeMs)),
      frame_count_(0),
      running_(false),
      preferred_width_(0) {
  SetFrames(ui::ResourceBundle::GetSharedInstance().GetImageNamed(
      IDR_THROBBER).ToImageSkia());
}

ThrobberNativeTextButton::~ThrobberNativeTextButton() {
  StopThrobber();
}

void ThrobberNativeTextButton::StartThrobber() {
  if (running_)
    return;

  start_time_ = base::TimeTicks::Now();
  timer_.Start(FROM_HERE, frame_time_, this, &ThrobberNativeTextButton::Run);
  running_ = true;

  SchedulePaint();
}

void ThrobberNativeTextButton::StopThrobber() {
  if (!running_)
    return;

  timer_.Stop();
  running_ = false;
}

void ThrobberNativeTextButton::SetFrames(const gfx::ImageSkia* frames) {
  frames_ = frames;
  DCHECK(frames_->width() > 0 && frames_->height() > 0);
  DCHECK(frames_->width() % frames_->height() == 0);
  frame_count_ = frames_->width() / frames_->height();
  PreferredSizeChanged();
}

gfx::Size ThrobberNativeTextButton::GetPreferredSize() {
  gfx::Size size = NativeTextButton::GetPreferredSize();
  if (preferred_width_)
    size.set_width(preferred_width_);
  return size;
}

const gfx::ImageSkia& ThrobberNativeTextButton::GetImageToPaint() const {
  if (!running_)
    return NativeTextButton::GetImageToPaint();

  const base::TimeDelta elapsed_time = base::TimeTicks::Now() - start_time_;
  const int current_frame =
      static_cast<int>(elapsed_time / frame_time_) % frame_count_;
  const int image_size = frames_->height();
  const int image_offset = current_frame * image_size;

  gfx::Rect subset_rect(image_offset, 0, image_size, image_size);
  this_frame_ = gfx::ImageSkiaOperations::ExtractSubset(*frames_, subset_rect);
  return this_frame_;
}

void ThrobberNativeTextButton::Run() {
  DCHECK(running_);

  SchedulePaint();
}

// WaitingView ----------------------------------------------------------
class WaitingView : public views::View {
 public:
  WaitingView(views::ButtonListener* listener, bool use_close_button);

 private:
  DISALLOW_COPY_AND_ASSIGN(WaitingView);
};

WaitingView::WaitingView(views::ButtonListener* listener,
                         bool use_close_button) {
  views::GridLayout* layout = new views::GridLayout(this);
  layout->set_minimum_size(gfx::Size(WebIntentPicker::kWindowWidth, 0));
  layout->SetInsets(WebIntentPicker::kContentAreaBorder,
                    WebIntentPicker::kContentAreaBorder,
                    WebIntentPicker::kContentAreaBorder,
                    WebIntentPicker::kContentAreaBorder);
  SetLayoutManager(layout);

  views::ColumnSet* cs = layout->AddColumnSet(0);
  views::ColumnSet* header_cs = NULL;
  if (use_close_button) {
    header_cs = layout->AddColumnSet(1);
    header_cs->AddPaddingColumn(1, views::kUnrelatedControlHorizontalSpacing);
    header_cs->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0,
                         GridLayout::USE_PREF, 0, 0);  // Close Button.
  }
  cs->AddPaddingColumn(0, views::kPanelHorizIndentation);
  cs->AddColumn(GridLayout::CENTER, GridLayout::CENTER,
                1, GridLayout::USE_PREF, 0, 0);
  cs->AddPaddingColumn(0, views::kPanelHorizIndentation);

  // Create throbber.
  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
  const gfx::ImageSkia* frames =
      rb.GetImageNamed(IDR_SPEECH_INPUT_SPINNER).ToImageSkia();
  views::Throbber* throbber = new views::Throbber(kThrobberFrameTimeMs, true);
  throbber->SetFrames(frames);
  throbber->Start();

  // Create text.
  views::Label* label = new views::Label();
  label->SetHorizontalAlignment(views::Label::ALIGN_CENTER);
  label->SetFont(rb.GetFont(ui::ResourceBundle::MediumBoldFont));
  label->SetText(l10n_util::GetStringUTF16(IDS_INTENT_PICKER_WAIT_FOR_CWS));

  // Layout the view.
  if (use_close_button) {
    layout->StartRow(0, 1);
    layout->AddView(CreateCloseButton(listener));
  }

  layout->AddPaddingRow(0, views::kUnrelatedControlLargeVerticalSpacing);
  layout->StartRow(0, 0);
  layout->AddView(throbber);
  layout->AddPaddingRow(0, views::kUnrelatedControlLargeVerticalSpacing);
  layout->StartRow(0, 0);
  layout->AddView(label);
  layout->AddPaddingRow(0, views::kUnrelatedControlLargeVerticalSpacing);
}

// SuggestedExtensionsLayout ---------------------------------------------------

// TODO(groby): Extremely fragile code, relies on order and number of fields.
// Would probably be better off as GridLayout or similar. Also see review
// comments on http://codereview.chromium.org/10909183

// A LayoutManager used by a row of the IntentsView. It is similar
// to a BoxLayout, but it right aligns the rightmost view (which is the install
// button). It also uses the maximum height of all views in the row as a
// preferred height so it doesn't change when the install button is hidden.
class SuggestedExtensionsLayout : public views::LayoutManager {
 public:
  SuggestedExtensionsLayout();
  virtual ~SuggestedExtensionsLayout();

  // Implementation of views::LayoutManager.
  virtual void Layout(views::View* host) OVERRIDE;
  virtual gfx::Size GetPreferredSize(views::View* host) OVERRIDE;

 private:
  DISALLOW_COPY_AND_ASSIGN(SuggestedExtensionsLayout);
};

SuggestedExtensionsLayout::SuggestedExtensionsLayout() {
}

SuggestedExtensionsLayout::~SuggestedExtensionsLayout() {
}

void SuggestedExtensionsLayout::Layout(views::View* host) {
  gfx::Rect child_area(host->GetLocalBounds());
  child_area.Inset(host->GetInsets());
  int x = child_area.x();
  int y = child_area.y();

  for (int i = 0; i < host->child_count(); ++i) {
    views::View* child = host->child_at(i);
    if (!child->visible())
      continue;
    gfx::Size size(child->GetPreferredSize());
    gfx::Rect child_bounds(x, y, size.width(), child_area.height());
    if (i == host->child_count() - 1) {
      // Last child (the install button) should be right aligned.
      child_bounds.set_x(std::max(child_area.width() - size.width(), x));
    } else if (i == 1) {
      // Label is considered fixed width, to align ratings widget.
      DCHECK_LE(size.width(), WebIntentPicker::kTitleLinkMaxWidth);
      x += WebIntentPicker::kTitleLinkMaxWidth +
          views::kRelatedControlHorizontalSpacing;
    } else {
      x += size.width() + views::kRelatedControlHorizontalSpacing;
    }
    // Clamp child view bounds to |child_area|.
    child->SetBoundsRect(child_bounds.Intersect(child_area));
  }
}

gfx::Size SuggestedExtensionsLayout::GetPreferredSize(views::View* host) {
  int width = 0;
  int height = 0;
  for (int i = 0; i < host->child_count(); ++i) {
    views::View* child = host->child_at(i);
    gfx::Size size(child->GetPreferredSize());
    // The preferred height includes visible and invisible children. This
    // prevents jank when a child is hidden.
    height = std::max(height, size.height());
    if (!child->visible())
      continue;
    if (i != 0)
      width += views::kRelatedControlHorizontalSpacing;
  }
  gfx::Insets insets(host->GetInsets());
  return gfx::Size(width + insets.width(), height + insets.height());
}

// IntentRowView --------------------------------------------------

// A view for each row in the IntentsView. It displays information
// for both installed and suggested intent handlers.
class IntentRowView : public views::View,
                      public views::ButtonListener,
                      public views::LinkListener {
 public:
  enum ActionType {
    ACTION_UNKNOWN,
    ACTION_INSTALL,
    ACTION_INVOKE
  };

  class Delegate {
   public:
    // Called when the user clicks the "Add to Chrome" button.
    virtual void OnExtensionInstallClicked(const string16& extension_id) = 0;

    // Called when the user clicks the extension title link.
    virtual void OnExtensionLinkClicked(
        const string16& extension_id,
        WindowOpenDisposition disposition) = 0;

    // Called when the action button is clicked. |type| indicates the requested
    // kind of action, and |tag| identifies the service or extension to
    // operate on.
    virtual void OnActionButtonClicked(ActionType type, size_t tag) = 0;

   protected:
    virtual ~Delegate() {}
  };

  virtual ~IntentRowView();

  // Creates a new view for |service| or |extension| depending on which
  // value is not NULL.
  static IntentRowView* CreateHandlerRow(
      const WebIntentPickerModel::InstalledService* service,
      const WebIntentPickerModel::SuggestedExtension* extension,
      int tag,
      IntentRowView::Delegate* delegate,
      int preferred_width);

  // ButtonListener implementation.
  virtual void ButtonPressed(views::Button* sender,
                             const ui::Event& event) OVERRIDE;

  // LinkListener implementation.
  void LinkClicked(views::Link* source, int event_flags) OVERRIDE;

  // Start an animating throbber for this row, and hide the star rating and the
  // install button.
  void StartThrobber();

  void MarkBusy(const string16& extension_id);

  // Stop the throbber for this row, and show the star rating and the install
  // button.
  void StopThrobber();

 protected:
  virtual void OnEnabledChanged() OVERRIDE;

  virtual void PaintChildren(gfx::Canvas* canvas) OVERRIDE;

 private:
  IntentRowView(ActionType type, size_t tag);

  // Gets the proper message string associated with |type_|.
  string16 GetActionButtonMessage();

  // Identifier for the suggested extension displayed in this row.
  string16 extension_id_;

  // A delegate to respond to button presses and clicked links.
  Delegate* delegate_;

  // The icon of the extension.
  views::ImageView* icon_;

  // The title of the extension, which links to the CWS detailed description of
  // this extension.
  views::View* title_link_;

  // The star rating of this extension.
  StarsView* stars_;

  // A button to install the extension.
  ThrobberNativeTextButton* install_button_;

  // The type of action that is invoked from the row's button.
  ActionType type_;

  // A tag identifying the data associated with this row. For both installed
  // and suggested services, this is an index into the respective collections
  // on the WebIntentPickerModel.
  size_t tag_;

  DISALLOW_COPY_AND_ASSIGN(IntentRowView);
};

IntentRowView::IntentRowView(ActionType type, size_t tag)
    : delegate_(NULL),
      icon_(NULL),
      title_link_(NULL),
      stars_(NULL),
      install_button_(NULL),
      type_(type),
      tag_(tag) {}

IntentRowView* IntentRowView::CreateHandlerRow(
    const WebIntentPickerModel::InstalledService* service,
    const WebIntentPickerModel::SuggestedExtension* extension,
    int tag,
    IntentRowView::Delegate* delegate,
    int preferred_width) {

  // one or the other must be set...exclusively
  DCHECK((service != NULL || extension != NULL) &&
         (service == NULL || extension == NULL));


  const string16& title = (service != NULL)
      ? service->title : extension->title;

  // TODO(groby): Once links are properly sized (see SuggestionRowViewLayout
  // refactor notes), can simply SetElideBehavior(views::Label::ELIDE_AT_END).
  // Note: Verify that views links do not treat empty space at the end as
  // part of the link, if this change happens.
  string16 elided_title = ui::ElideText(title, gfx::Font(),
                                        WebIntentPicker::kTitleLinkMaxWidth,
                                        ui::ELIDE_AT_END);

  const gfx::ImageSkia* icon = NULL;
  StarsView* stars = NULL;
  views::Label* label = NULL;
  IntentRowView* view;
  if (service != NULL) {
    view = new IntentRowView(ACTION_INVOKE, tag);
    icon = service->favicon.ToImageSkia();
    label = new views::Label(elided_title);
  } else {
    view = new IntentRowView(ACTION_INSTALL, tag);
    view->extension_id_ = extension->id;
    icon = extension->icon.ToImageSkia();
    views::Link* link = new views::Link(elided_title);
    link->set_listener(view);
    label = link;
    stars = new StarsView(extension->average_rating);
  }

  view->delegate_ = delegate;

  view->SetLayoutManager(new SuggestedExtensionsLayout);

  view->icon_ = new views::ImageView();

  view->icon_->SetImage(icon);
  view->AddChildView(view->icon_);

  view->title_link_ = label;
  view->AddChildView(view->title_link_);

  if (stars != NULL) {
    view->stars_ = stars;
    view->AddChildView(view->stars_);
  }

  view->install_button_ = new ThrobberNativeTextButton(
      view, view->GetActionButtonMessage());
  view->install_button_->set_preferred_width(preferred_width);
  view->AddChildView(view->install_button_);

  return view;
}

IntentRowView::~IntentRowView() {}

void IntentRowView::ButtonPressed(views::Button* sender,
                                  const ui::Event& event) {
  if (type_ == ACTION_INSTALL)
    delegate_->OnExtensionInstallClicked(extension_id_);
  else
    delegate_->OnActionButtonClicked(type_, tag_);
}

void IntentRowView::LinkClicked(views::Link* source,
                                int event_flags) {
  delegate_->OnExtensionLinkClicked(extension_id_,
      chrome::DispositionFromEventFlags(event_flags));
}

void IntentRowView::MarkBusy(const string16& extension_id) {
  SetEnabled(false);
  if (extension_id == extension_id_)
    StartThrobber();
}

void IntentRowView::StartThrobber() {
  install_button_->StartThrobber();
  install_button_->SetText(string16());
}

void IntentRowView::StopThrobber() {
  install_button_->StopThrobber();
  install_button_->SetText(GetActionButtonMessage());
}

void IntentRowView::OnEnabledChanged() {
  title_link_->SetEnabled(enabled());
  if (stars_)
    stars_->SetEnabled(enabled());
  install_button_->SetEnabled(enabled());
  View::OnEnabledChanged();
  Layout();
}

void IntentRowView::PaintChildren(gfx::Canvas* canvas) {
  View::PaintChildren(canvas);
  if (!enabled())
    canvas->FillRect(GetLocalBounds(), kHalfOpacityWhite);
}

string16 IntentRowView::GetActionButtonMessage() {
  int message_id = 0;

  if (type_ == ACTION_INVOKE)
    message_id = IDS_INTENT_PICKER_SELECT_INTENT;
  else  if (type_ == ACTION_INSTALL)
    message_id = IDS_INTENT_PICKER_INSTALL_EXTENSION;
  else
    NOTREACHED();

  return l10n_util::GetStringUTF16(message_id);
}

// IntentsView -----------------------------------------------------

// A view that contains both installed services and suggested extensions
// from the Chrome Web Store that provide an intent service matching the
// action/type pair.
class IntentsView : public views::View {
 public:
  IntentsView(const WebIntentPickerModel* model,
              IntentRowView::Delegate* delegate);

  virtual ~IntentsView();

  // Update the view to the new model data.
  void Update();

  // Show the install throbber for the row containing |extension_id|. This
  // function also hides hides and disables other buttons and links.
  void StartThrobber(const string16& extension_id);

  // Hide the install throbber. This function re-enables all buttons and links.
  void StopThrobber();

  // Adjusts a given width to account for language-specific strings.
  int AdjustWidth(int old_width);

 protected:
  virtual void OnEnabledChanged() OVERRIDE;

 private:
  const WebIntentPickerModel* model_;
  IntentRowView::Delegate* delegate_;

  // Width for the action button, adjusted to be wide enough for all possible
  // strings.
  int button_width_;

  DISALLOW_COPY_AND_ASSIGN(IntentsView);
};

IntentsView::IntentsView(
    const WebIntentPickerModel* model,
    IntentRowView::Delegate* delegate)
    : model_(model),
      delegate_(delegate),
      button_width_(0){
  Update();
}

IntentsView::~IntentsView() {
}

void IntentsView::Update() {
  RemoveAllChildViews(true);

  ThrobberNativeTextButton size_helper(
      NULL, l10n_util::GetStringUTF16(IDS_INTENT_PICKER_INSTALL_EXTENSION));
  size_helper.SetText(
      l10n_util::GetStringUTF16(IDS_INTENT_PICKER_SELECT_INTENT));
  button_width_ = std::max(
      kButtonWidth, size_helper.GetPreferredSize().width());

  views::BoxLayout* layout = new views::BoxLayout(
      views::BoxLayout::kVertical, 0, 0, views::kRelatedControlVerticalSpacing);
  SetLayoutManager(layout);

  int available_rows = kMaxRowCount;

  for (size_t i = 0;
      available_rows > 0 && i < model_->GetInstalledServiceCount();
      ++i, --available_rows) {
    const WebIntentPickerModel::InstalledService& service =
      model_->GetInstalledServiceAt(i);
    AddChildView(IntentRowView::CreateHandlerRow(&service, NULL, i,
                                                 delegate_, button_width_));
  }

  // Only fill up with suggestions if we filled less than kMinRowCount rows.
  available_rows -= (kMaxRowCount - kMinRowCount);

  for (size_t i = 0;
      available_rows > 0 && i < model_->GetSuggestedExtensionCount();
      ++i, --available_rows) {
    const WebIntentPickerModel::SuggestedExtension& extension =
        model_->GetSuggestedExtensionAt(i);
    AddChildView(IntentRowView::CreateHandlerRow(NULL, &extension, i,
                                                 delegate_, button_width_));
  }
}

void IntentsView::StartThrobber(const string16& extension_id) {
  for (int i = 0; i < child_count(); ++i)
    static_cast<IntentRowView*>(child_at(i))->MarkBusy(extension_id);
}

void IntentsView::StopThrobber() {
  for (int i = 0; i < child_count(); ++i) {
    IntentRowView* row =
        static_cast<IntentRowView*>(child_at(i));
    row->SetEnabled(true);
    row->StopThrobber();
  }
}

int IntentsView::AdjustWidth(int old_width) {
  return old_width - kButtonWidth + button_width_;
}

void IntentsView::OnEnabledChanged() {
  EnableChildViews(this, enabled());
  View::OnEnabledChanged();
}

}  // namespace

// WebIntentPickerViews --------------------------------------------------------

// Views implementation of WebIntentPicker.
class WebIntentPickerViews : public views::ButtonListener,
                             public views::DialogDelegate,
                             public views::LinkListener,
                             public WebIntentPicker,
                             public WebIntentPickerModelObserver,
                             public IntentRowView::Delegate {
 public:
  WebIntentPickerViews(TabContents* tab_contents,
                       WebIntentPickerDelegate* delegate,
                       WebIntentPickerModel* model);
  virtual ~WebIntentPickerViews();

  // views::ButtonListener implementation.
  // This method is called when the user cancels the picker dialog.
  virtual void ButtonPressed(views::Button* sender,
                             const ui::Event& event) OVERRIDE;

  // views::DialogDelegate implementation.
  virtual void WindowClosing() OVERRIDE;
  virtual void DeleteDelegate() OVERRIDE;
  virtual views::Widget* GetWidget() OVERRIDE;
  virtual const views::Widget* GetWidget() const OVERRIDE;
  virtual views::View* GetContentsView() OVERRIDE;
  virtual int GetDialogButtons() const OVERRIDE;
  virtual bool Cancel() OVERRIDE;

  // LinkListener implementation.
  virtual void LinkClicked(views::Link* source, int event_flags) OVERRIDE;

  // WebIntentPicker implementation.
  virtual void Close() OVERRIDE;
  virtual void SetActionString(const string16& action) OVERRIDE;
  virtual void OnExtensionInstallSuccess(const std::string& id) OVERRIDE;
  virtual void OnExtensionInstallFailure(const std::string& id) OVERRIDE;
  virtual void OnInlineDispositionAutoResize(const gfx::Size& size) OVERRIDE;
  virtual void OnPendingAsyncCompleted() OVERRIDE;
  virtual void OnInlineDispositionWebContentsLoaded(
      content::WebContents* web_contents) OVERRIDE;

  // WebIntentPickerModelObserver implementation.
  virtual void OnModelChanged(WebIntentPickerModel* model) OVERRIDE;
  virtual void OnFaviconChanged(WebIntentPickerModel* model,
                                size_t index) OVERRIDE;
  virtual void OnExtensionIconChanged(WebIntentPickerModel* model,
                                      const string16& extension_id) OVERRIDE;
  virtual void OnInlineDisposition(const string16& title,
                                   const GURL& url) OVERRIDE;

  // SuggestedExtensionsRowView::Delegate implementation.
  virtual void OnExtensionInstallClicked(const string16& extension_id) OVERRIDE;
  virtual void OnExtensionLinkClicked(
      const string16& extension_id,
      WindowOpenDisposition disposition) OVERRIDE;
  virtual void OnActionButtonClicked(
      IntentRowView::ActionType type,
      size_t tag) OVERRIDE;

 private:
  // Update picker contents to reflect the current state of the model.
  void UpdateContents();

  // Updates the dialog with the list of available services, suggestions,
  // and a nice link to CWS to find more suggestions. This is the "Main"
  // view of the picker.
  void ShowAvailableServices();

  // Informs the user that there are no services available to handle
  // the intent, and that there are no suggestions from the Chrome Web Store.
  void ShowNoServicesMessage();

  // Restore the contents of the picker to the initial contents.
  void ResetContents();

  // Resize the constrained window to the size of its contents.
  void SizeToContents();

  // Clear the contents of the picker.
  void ClearContents();

  // Returns the service selection question text used in the title
  // of the picker.
  const string16 GetActionTitle();

  // A weak pointer to the WebIntentPickerDelegate to notify when the user
  // chooses a service or cancels.
  WebIntentPickerDelegate* delegate_;

  // A weak pointer to the picker model.
  WebIntentPickerModel* model_;

  // A weak pointer to the action string label.
  // Created locally, owned by Views.
  views::Label* action_label_;

  // A weak pointer to the header label for the extension suggestions.
  // Created locally, owned by Views.
  views::Label* suggestions_label_;

  // A weak pointer to the intents view.
  // Created locally, owned by Views view hierarchy.
  IntentsView* extensions_;

  // Delegate for inline disposition tab contents.
  scoped_ptr<WebIntentInlineDispositionDelegate> inline_disposition_delegate_;

  // A weak pointer to the TabContents this picker is in.
  TabContents* tab_contents_;

  // A weak pointer to the WebView that hosts the WebContents being displayed.
  // Created locally, owned by Views.
  views::WebView* webview_;

  // A weak pointer to the view that contains all other views in the picker.
  // Created locally, owned by Views.
  views::View* contents_;

  // A weak pointer to the constrained window.
  // Created locally, owned by Views.
  ConstrainedWindowViews* window_;

  // A weak pointer to the more suggestions link.
  // Created locally, owned by Views.
  views::Link* more_suggestions_link_;

  // A weak pointer to the choose another service link.
  // Created locally, owned by Views.
  views::Link* choose_another_service_link_;

  // Weak pointer to "Waiting for CWS" display. Owned by parent view.
  WaitingView* waiting_view_;

  // Set to true when displaying the inline disposition web contents. Used to
  // prevent laying out the inline disposition widgets twice.
  bool displaying_web_contents_;

  // The text for the current action.
  string16 action_text_;

  // Ownership of the WebContents we are displaying in the inline disposition.
  scoped_ptr<WebContents> inline_web_contents_;

  // Indicate if dialog should display its own close button.
  // TODO(groby): Only relevant until new ConstrainedWindow is implemented,
  // from then on always true.
  bool use_close_button_;

  // Signals if the picker can be closed. False during extension install.
  bool can_close_;

  DISALLOW_COPY_AND_ASSIGN(WebIntentPickerViews);
};

// static
WebIntentPicker* WebIntentPicker::Create(content::WebContents* web_contents,
                                         WebIntentPickerDelegate* delegate,
                                         WebIntentPickerModel* model) {
  TabContents* tab_contents = TabContents::FromWebContents(web_contents);
  return new WebIntentPickerViews(tab_contents, delegate, model);
}

WebIntentPickerViews::WebIntentPickerViews(TabContents* tab_contents,
                                           WebIntentPickerDelegate* delegate,
                                           WebIntentPickerModel* model)
    : delegate_(delegate),
      model_(model),
      action_label_(NULL),
      suggestions_label_(NULL),
      extensions_(NULL),
      tab_contents_(tab_contents),
      webview_(new views::WebView(tab_contents->profile())),
      window_(NULL),
      more_suggestions_link_(NULL),
      choose_another_service_link_(NULL),
      waiting_view_(NULL),
      displaying_web_contents_(false),
      can_close_(true) {
  use_close_button_ = CommandLine::ForCurrentProcess()->HasSwitch(
      switches::kEnableFramelessConstrainedDialogs);

  model_->set_observer(this);
  contents_ = new views::View();
  // Show the dialog.
  window_ = new ConstrainedWindowViews(tab_contents, this);

  UpdateContents();
}

WebIntentPickerViews::~WebIntentPickerViews() {
  model_->set_observer(NULL);
}

void WebIntentPickerViews::ButtonPressed(views::Button* sender,
                                         const ui::Event& event) {
  delegate_->OnUserCancelledPickerDialog();
}

void WebIntentPickerViews::WindowClosing() {
  delegate_->OnClosing();
}

void WebIntentPickerViews::DeleteDelegate() {
  delete this;
}

views::Widget* WebIntentPickerViews::GetWidget() {
  return contents_->GetWidget();
}

const views::Widget* WebIntentPickerViews::GetWidget() const {
  return contents_->GetWidget();
}

views::View* WebIntentPickerViews::GetContentsView() {
  return contents_;
}

int WebIntentPickerViews::GetDialogButtons() const {
  return ui::DIALOG_BUTTON_NONE;
}

bool WebIntentPickerViews::Cancel() {
  return can_close_;
}

void WebIntentPickerViews::LinkClicked(views::Link* source, int event_flags) {
  if (source == more_suggestions_link_) {
    delegate_->OnSuggestionsLinkClicked(
        chrome::DispositionFromEventFlags(event_flags));
  } else if (source == choose_another_service_link_) {
    // Signal cancellation of inline disposition.
    delegate_->OnChooseAnotherService();
    ResetContents();
  } else {
    NOTREACHED();
  }
}

void WebIntentPickerViews::Close() {
  window_->CloseConstrainedWindow();
}

void WebIntentPickerViews::SetActionString(const string16& action) {
  action_text_ = action;

  if (action_label_)
    action_label_->SetText(GetActionTitle());
    contents_->Layout();
    SizeToContents();
}

void WebIntentPickerViews::OnExtensionInstallSuccess(const std::string& id) {
  can_close_ = true;
}

void WebIntentPickerViews::OnExtensionInstallFailure(const std::string& id) {
  extensions_->StopThrobber();
  more_suggestions_link_->SetEnabled(true);
  can_close_ = true;
  contents_->Layout();
  SizeToContents();

  // TODO(binji): What to display to user on failure?
}

void WebIntentPickerViews::OnInlineDispositionAutoResize(
    const gfx::Size& size) {
  webview_->SetPreferredSize(size);
  contents_->Layout();
  SizeToContents();
}

void WebIntentPickerViews::OnPendingAsyncCompleted() {
  UpdateContents();
}

void WebIntentPickerViews::ShowNoServicesMessage() {
  ClearContents();

  views::GridLayout* grid_layout = new views::GridLayout(contents_);
  contents_->SetLayoutManager(grid_layout);

  grid_layout->SetInsets(kContentAreaBorder, kContentAreaBorder,
                         kContentAreaBorder, kContentAreaBorder);
  views::ColumnSet* main_cs = grid_layout->AddColumnSet(0);
  main_cs->AddColumn(GridLayout::FILL, GridLayout::LEADING, 1,
                     GridLayout::USE_PREF, 0, 0);

  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();

  grid_layout->StartRow(0, 0);
  views::Label* header = new views::Label();
  header->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
  header->SetFont(rb.GetFont(ui::ResourceBundle::MediumFont));
  header->SetText(l10n_util::GetStringUTF16(
      IDS_INTENT_PICKER_NO_SERVICES_TITLE));
  grid_layout->AddView(header);

  grid_layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);

  grid_layout->StartRow(0, 0);
  views::Label* body = new views::Label();
  body->SetMultiLine(true);
  body->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
  body->SetText(l10n_util::GetStringUTF16(IDS_INTENT_PICKER_NO_SERVICES));
  grid_layout->AddView(body);

  int height = contents_->GetHeightForWidth(kWindowWidth);
  contents_->SetSize(gfx::Size(kWindowWidth, height));
}

void WebIntentPickerViews::OnInlineDispositionWebContentsLoaded(
    content::WebContents* web_contents) {
  if (displaying_web_contents_)
    return;

  // Replace the picker with the inline disposition.
  ClearContents();

  views::GridLayout* grid_layout = new views::GridLayout(contents_);
  contents_->SetLayoutManager(grid_layout);

  grid_layout->SetInsets(kContentAreaBorder, kContentAreaBorder,
                         kContentAreaBorder, kContentAreaBorder);
  views::ColumnSet* header_cs = grid_layout->AddColumnSet(0);
  header_cs->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0,
                       GridLayout::USE_PREF, 0, 0);  // Icon.
  header_cs->AddPaddingColumn(0, 4);
  header_cs->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0,
                       GridLayout::USE_PREF, 0, 0);  // Title.
  header_cs->AddPaddingColumn(0, 4);
  header_cs->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0,
                       GridLayout::USE_PREF, 0, 0);  // Link.
  header_cs->AddPaddingColumn(1, views::kUnrelatedControlHorizontalSpacing);
  if (use_close_button_) {
    header_cs->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0,
                         GridLayout::USE_PREF, 0, 0);  // Close Button.
  }

  views::ColumnSet* full_cs = grid_layout->AddColumnSet(1);
  full_cs->AddColumn(GridLayout::FILL, GridLayout::FILL, 1.0,
                     GridLayout::USE_PREF, 0, 0);

  const WebIntentPickerModel::InstalledService* service =
      model_->GetInstalledServiceWithURL(model_->inline_disposition_url());

  // Header row.
  grid_layout->StartRow(0, 0);
  views::ImageView* icon = new views::ImageView();
  icon->SetImage(service->favicon.ToImageSkia());
  grid_layout->AddView(icon);

  string16 elided_title = ui::ElideText(
      service->title, gfx::Font(), kTitleLinkMaxWidth, ui::ELIDE_AT_END);
  views::Label* title = new views::Label(elided_title);
  grid_layout->AddView(title);
  // Add link for "choose another service" if other suggestions are available
  // or if more than one (the current) service is installed.
  if (model_->GetInstalledServiceCount() > 1 ||
       model_->GetSuggestedExtensionCount()) {
    choose_another_service_link_ = new views::Link(
        l10n_util::GetStringUTF16(IDS_INTENT_PICKER_USE_ALTERNATE_SERVICE));
    grid_layout->AddView(choose_another_service_link_);
    choose_another_service_link_->set_listener(this);
  }

  if (use_close_button_)
    grid_layout->AddView(CreateCloseButton(this));

  // Inline web contents row.
  grid_layout->StartRow(0, 1);
  grid_layout->AddView(webview_, 1, 1, GridLayout::CENTER,
                       GridLayout::CENTER, 0, 0);
  contents_->Layout();
  SizeToContents();
  displaying_web_contents_ = true;
}

void WebIntentPickerViews::OnModelChanged(WebIntentPickerModel* model) {
  if (waiting_view_ && !model->IsWaitingForSuggestions())
    UpdateContents();

  if (suggestions_label_) {
    string16 label_text = model->GetSuggestionsLinkText();
    suggestions_label_->SetText(label_text);
    suggestions_label_->SetVisible(!label_text.empty());
  }

  if (extensions_)
    extensions_->Update();
  contents_->Layout();
  SizeToContents();
}

void WebIntentPickerViews::OnFaviconChanged(WebIntentPickerModel* model,
                                            size_t index) {
  // TODO(groby): Update favicons on extensions_;
  contents_->Layout();
  SizeToContents();
}

void WebIntentPickerViews::OnExtensionIconChanged(
    WebIntentPickerModel* model,
    const string16& extension_id) {
  if (extensions_)
    extensions_->Update();

  contents_->Layout();
  SizeToContents();
}

void WebIntentPickerViews::OnInlineDisposition(
    const string16&, const GURL& url) {
  if (!webview_)
    webview_ = new views::WebView(tab_contents_->profile());

  inline_web_contents_.reset(WebContents::Create(
      tab_contents_->profile(),
      tab_util::GetSiteInstanceForNewTab(tab_contents_->profile(), url),
      MSG_ROUTING_NONE, NULL));
  // Does not take ownership, so we keep a scoped_ptr
  // for the WebContents locally.
  webview_->SetWebContents(inline_web_contents_.get());
  Browser* browser = browser::FindBrowserWithWebContents(
      tab_contents_->web_contents());
  inline_disposition_delegate_.reset(
      new WebIntentInlineDispositionDelegate(this, inline_web_contents_.get(),
                                             browser));
  content::WebContents* web_contents = webview_->GetWebContents();

  // Must call this immediately after WebContents creation to avoid race
  // with load.
  delegate_->OnInlineDispositionWebContentsCreated(web_contents);
  web_contents->GetController().LoadURL(
      url,
      content::Referrer(),
      content::PAGE_TRANSITION_AUTO_TOPLEVEL,
      std::string());

  // Disable all buttons.
  // TODO(groby): Add throbber for inline dispo - see http://crbug.com/142519.
  extensions_->SetEnabled(false);
  more_suggestions_link_->SetEnabled(false);
  contents_->Layout();
}

void WebIntentPickerViews::OnExtensionInstallClicked(
    const string16& extension_id) {
  can_close_ = false;
  extensions_->StartThrobber(extension_id);
  more_suggestions_link_->SetEnabled(false);
  contents_->Layout();
  delegate_->OnExtensionInstallRequested(UTF16ToUTF8(extension_id));
}

void WebIntentPickerViews::OnExtensionLinkClicked(
    const string16& extension_id,
    WindowOpenDisposition disposition) {
  delegate_->OnExtensionLinkClicked(UTF16ToUTF8(extension_id), disposition);
}

void WebIntentPickerViews::OnActionButtonClicked(
    IntentRowView::ActionType type, size_t tag) {
  DCHECK_EQ(IntentRowView::ACTION_INVOKE, type);
  const WebIntentPickerModel::InstalledService& service =
      model_->GetInstalledServiceAt(tag);
  delegate_->OnServiceChosen(service.url, service.disposition);
}

void WebIntentPickerViews::UpdateContents() {
  if (model_ && model_->IsWaitingForSuggestions()) {
    ClearContents();
    contents_->SetLayoutManager(new views::FillLayout());
    waiting_view_ = new WaitingView(this, use_close_button_);
    contents_->AddChildView(waiting_view_);
    int height = contents_->GetHeightForWidth(kWindowWidth);
    contents_->SetSize(gfx::Size(kWindowWidth, height));
    contents_->Layout();
  } else if (model_->GetInstalledServiceCount() ||
      model_->GetSuggestedExtensionCount()) {
    ShowAvailableServices();
  } else {
    ShowNoServicesMessage();
  }
  SizeToContents();
}

const string16 WebIntentPickerViews::GetActionTitle() {
  return (!action_text_.empty()) ?
      action_text_ :
      l10n_util::GetStringUTF16(IDS_INTENT_PICKER_CHOOSE_SERVICE);
}

void WebIntentPickerViews::ShowAvailableServices() {
  enum {
    kHeaderRowColumnSet,  // Column set for header layout.
    kFullWidthColumnSet,  // Column set with a single full-width column.
    kIndentedFullWidthColumnSet,  // Single full-width column, indented.
  };

  ClearContents();
  displaying_web_contents_ = false;

  extensions_ = new IntentsView(model_, this);

  views::GridLayout* grid_layout = new views::GridLayout(contents_);
  contents_->SetLayoutManager(grid_layout);

  grid_layout->set_minimum_size(
      gfx::Size(extensions_->AdjustWidth(kWindowWidth), 0));
  grid_layout->SetInsets(kContentAreaBorder, kContentAreaBorder,
                         kContentAreaBorder, kContentAreaBorder);
  views::ColumnSet* header_cs = grid_layout->AddColumnSet(kHeaderRowColumnSet);
  header_cs->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 1,
                       GridLayout::USE_PREF, 0, 0);  // Title.
  if (use_close_button_) {
    header_cs->AddPaddingColumn(0, views::kUnrelatedControlHorizontalSpacing);
    header_cs->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0,
                         GridLayout::USE_PREF, 0, 0);  // Close Button.
  }

  views::ColumnSet* full_cs = grid_layout->AddColumnSet(kFullWidthColumnSet);
  full_cs->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1,
                     GridLayout::USE_PREF, 0, 0);

  views::ColumnSet* indent_cs =
      grid_layout->AddColumnSet(kIndentedFullWidthColumnSet);
  indent_cs->AddPaddingColumn(0, views::kUnrelatedControlHorizontalSpacing);
  indent_cs->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1,
                       GridLayout::USE_PREF, 0, 0);

  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();

  // Header row.
  grid_layout->StartRow(0, kHeaderRowColumnSet);
  action_label_ = new views::Label(GetActionTitle());
  action_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
  action_label_->SetFont(rb.GetFont(ui::ResourceBundle::MediumFont));
  grid_layout->AddView(action_label_);

  if (use_close_button_)
    grid_layout->AddView(CreateCloseButton(this));

  // Padding row.
  grid_layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);

  // Row with app suggestions label.
  grid_layout->StartRow(0, kIndentedFullWidthColumnSet);
  suggestions_label_ = new views::Label();
  suggestions_label_->SetVisible(false);
  suggestions_label_->SetMultiLine(true);
  suggestions_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
  grid_layout->AddView(suggestions_label_);

  // Padding row.
  grid_layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);

  // Row with extension suggestions.
  grid_layout->StartRow(0, kIndentedFullWidthColumnSet);
  grid_layout->AddView(extensions_);

  // Padding row.
  grid_layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);

  // Row with "more suggestions" link.
  grid_layout->StartRow(0, kFullWidthColumnSet);
  views::View* more_view = new views::View();
  more_view->SetLayoutManager(
      new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0,
                           views::kRelatedControlHorizontalSpacing));
  views::ImageView* icon = new views::ImageView();
  icon->SetImage(rb.GetImageNamed(IDR_WEBSTORE_ICON_16).ToImageSkia());
  more_view->AddChildView(icon);
  more_suggestions_link_ = new views::Link(
    l10n_util::GetStringUTF16(IDS_FIND_MORE_INTENT_HANDLER_MESSAGE));
  more_suggestions_link_->SetDisabledColor(kDisabledLinkColor);
  more_suggestions_link_->set_listener(this);
  more_view->AddChildView(more_suggestions_link_);
  grid_layout->AddView(more_view, 1, 1,
                       GridLayout::LEADING, GridLayout::CENTER);
  contents_->Layout();
}

void WebIntentPickerViews::ResetContents() {
  // Abandon both web contents and webview.
  webview_->SetWebContents(NULL);
  inline_web_contents_.reset();
  webview_ = NULL;

  // Re-initialize the UI.
  UpdateContents();

  // Restore previous state.
  extensions_->Update();
  action_label_->SetText(action_text_);
  contents_->Layout();
  SizeToContents();
}

void WebIntentPickerViews::SizeToContents() {
  gfx::Size client_size = contents_->GetPreferredSize();
  gfx::Rect client_bounds(client_size);
  gfx::Rect new_window_bounds = window_->non_client_view()->frame_view()->
      GetWindowBoundsForClientBounds(client_bounds);
  window_->CenterWindow(new_window_bounds.size());
}

void WebIntentPickerViews::ClearContents() {
  DCHECK(contents_);
  // The call RemoveAllChildViews(true) deletes all children of |contents|. If
  // we do not set our weak pointers to NULL, then they will continue to point
  // to where the deleted objects used to be, i.e. unitialized memory. This
  // would cause hard-to-explain crashes.
  contents_->RemoveAllChildViews(true);
  action_label_ = NULL;
  suggestions_label_ = NULL;
  extensions_ = NULL;
  more_suggestions_link_ = NULL;
  choose_another_service_link_ = NULL;
}