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
|
// Copyright (c) 2009 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/location_bar_view.h"
#if defined(OS_LINUX)
#include <gtk/gtk.h>
#endif
#include "build/build_config.h"
#include "app/gfx/canvas.h"
#include "app/gfx/color_utils.h"
#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "base/file_util.h"
#include "base/keyboard_codes.h"
#include "base/path_service.h"
#include "base/string_util.h"
#include "chrome/app/chrome_dll_resource.h"
#include "chrome/browser/alternate_nav_url_fetcher.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/bubble_positioner.h"
#include "chrome/browser/command_updater.h"
#include "chrome/browser/extensions/extension_browser_event_router.h"
#include "chrome/browser/extensions/extension_tabs_module.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/page_info_window.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_model.h"
#include "chrome/browser/tab_contents/navigation_entry.h"
#include "chrome/browser/view_ids.h"
#include "chrome/browser/views/info_bubble.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/page_action.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "views/focus/focus_manager.h"
#include "views/widget/root_view.h"
#include "views/widget/widget.h"
#if defined(OS_WIN)
#include "app/win_util.h"
#include "chrome/browser/views/first_run_bubble.h"
#endif
using views::View;
// static
const int LocationBarView::kVertMargin = 2;
// Padding on the right and left of the entry field.
static const int kEntryPadding = 3;
// Padding between the entry and the leading/trailing views.
static const int kInnerPadding = 3;
static const SkBitmap* kBackground = NULL;
static const SkBitmap* kPopupBackground = NULL;
// The delay the mouse has to be hovering over the lock/warning icon before the
// info bubble is shown.
static const int kInfoBubbleHoverDelayMs = 500;
// The tab key image.
static const SkBitmap* kTabButtonBitmap = NULL;
// Returns the short name for a keyword.
static std::wstring GetKeywordName(Profile* profile,
const std::wstring& keyword) {
// Make sure the TemplateURL still exists.
// TODO(sky): Once LocationBarView adds a listener to the TemplateURLModel
// to track changes to the model, this should become a DCHECK.
const TemplateURL* template_url =
profile->GetTemplateURLModel()->GetTemplateURLForKeyword(keyword);
if (template_url)
return template_url->AdjustedShortNameForLocaleDirection();
return std::wstring();
}
LocationBarView::LocationBarView(Profile* profile,
CommandUpdater* command_updater,
ToolbarModel* model,
Delegate* delegate,
bool popup_window_mode,
const BubblePositioner* bubble_positioner)
: profile_(profile),
command_updater_(command_updater),
model_(model),
delegate_(delegate),
disposition_(CURRENT_TAB),
location_entry_view_(NULL),
selected_keyword_view_(profile),
keyword_hint_view_(profile),
type_to_search_view_(l10n_util::GetString(IDS_OMNIBOX_EMPTY_TEXT)),
security_image_view_(profile, model, bubble_positioner),
popup_window_mode_(popup_window_mode),
first_run_bubble_(this),
bubble_positioner_(bubble_positioner) {
DCHECK(profile_);
SetID(VIEW_ID_LOCATION_BAR);
SetFocusable(true);
if (!kBackground) {
ResourceBundle &rb = ResourceBundle::GetSharedInstance();
kBackground = rb.GetBitmapNamed(IDR_LOCATIONBG);
kPopupBackground = rb.GetBitmapNamed(IDR_LOCATIONBG_POPUPMODE_CENTER);
}
}
LocationBarView::~LocationBarView() {
#if defined(OS_LINUX)
// We must release the ref that the NativeViewHost has on the
// AutocompleteEditViewGtk, otherwise its internal OwnedWidgetGtk will
// complain about its refcount not being 1 as it is destroyed.
location_entry_view_->Detach();
#endif
DeletePageActionViews();
}
void LocationBarView::Init() {
if (popup_window_mode_) {
font_ = ResourceBundle::GetSharedInstance().GetFont(
ResourceBundle::BaseFont);
} else {
// Use a larger version of the system font.
font_ = font_.DeriveFont(3);
}
// URL edit field.
// View container for URL edit field.
#if defined(OS_WIN)
views::Widget* widget = GetWidget();
location_entry_.reset(new AutocompleteEditViewWin(font_, this, model_, this,
widget->GetNativeView(),
profile_, command_updater_,
popup_window_mode_,
bubble_positioner_));
#else
location_entry_.reset(new AutocompleteEditViewGtk(this, model_, profile_,
command_updater_,
popup_window_mode_,
bubble_positioner_));
location_entry_->Init();
// Make all the children of the widget visible. NOTE: this won't display
// anything, it just toggles the visible flag.
gtk_widget_show_all(location_entry_->widget());
// Hide the widget. NativeViewHostGtk will make it visible again as
// necessary.
gtk_widget_hide(location_entry_->widget());
#endif
location_entry_view_ = new views::NativeViewHost;
location_entry_view_->SetID(VIEW_ID_AUTOCOMPLETE);
AddChildView(location_entry_view_);
location_entry_view_->set_focus_view(this);
location_entry_view_->Attach(
#if defined(OS_WIN)
location_entry_->m_hWnd
#else
location_entry_->widget()
#endif
);
AddChildView(&selected_keyword_view_);
selected_keyword_view_.SetFont(font_);
selected_keyword_view_.SetVisible(false);
selected_keyword_view_.SetParentOwned(false);
SkColor dimmed_text = GetColor(false, DEEMPHASIZED_TEXT);
AddChildView(&type_to_search_view_);
type_to_search_view_.SetVisible(false);
type_to_search_view_.SetFont(font_);
type_to_search_view_.SetColor(dimmed_text);
type_to_search_view_.SetParentOwned(false);
AddChildView(&keyword_hint_view_);
keyword_hint_view_.SetVisible(false);
keyword_hint_view_.SetFont(font_);
keyword_hint_view_.SetColor(dimmed_text);
keyword_hint_view_.SetParentOwned(false);
AddChildView(&security_image_view_);
security_image_view_.SetVisible(false);
security_image_view_.SetParentOwned(false);
AddChildView(&info_label_);
info_label_.SetVisible(false);
info_label_.SetParentOwned(false);
// Notify us when any ancestor is resized. In this case we want to tell the
// AutocompleteEditView to close its popup.
SetNotifyWhenVisibleBoundsInRootChanges(true);
// Initialize the location entry. We do this to avoid a black flash which is
// visible when the location entry has just been initialized.
Update(NULL);
OnChanged();
}
bool LocationBarView::IsInitialized() const {
return location_entry_view_ != NULL;
}
// static
SkColor LocationBarView::GetColor(bool is_secure, ColorKind kind) {
enum SecurityState {
NOT_SECURE = 0,
SECURE,
NUM_STATES
};
static bool initialized = false;
static SkColor colors[NUM_STATES][NUM_KINDS];
if (!initialized) {
#if defined(OS_WIN)
colors[NOT_SECURE][BACKGROUND] = color_utils::GetSysSkColor(COLOR_WINDOW);
colors[NOT_SECURE][TEXT] = color_utils::GetSysSkColor(COLOR_WINDOWTEXT);
colors[NOT_SECURE][SELECTED_TEXT] =
color_utils::GetSysSkColor(COLOR_HIGHLIGHTTEXT);
#else
// TODO(beng): source from theme provider.
colors[NOT_SECURE][BACKGROUND] = SK_ColorWHITE;
colors[NOT_SECURE][TEXT] = SK_ColorBLACK;
colors[NOT_SECURE][SELECTED_TEXT] = SK_ColorWHITE;
#endif
colors[SECURE][BACKGROUND] = SkColorSetRGB(255, 245, 195);
colors[SECURE][TEXT] = SkColorSetRGB(0, 0, 0);
colors[SECURE][SELECTED_TEXT] = 0; // Unused
colors[NOT_SECURE][DEEMPHASIZED_TEXT] =
color_utils::AlphaBlend(colors[NOT_SECURE][TEXT],
colors[NOT_SECURE][BACKGROUND], 128);
colors[SECURE][DEEMPHASIZED_TEXT] =
color_utils::AlphaBlend(colors[SECURE][TEXT],
colors[SECURE][BACKGROUND], 128);
const SkColor kDarkNotSecureText = SkColorSetRGB(200, 0, 0);
const SkColor kLightNotSecureText = SkColorSetRGB(255, 55, 55);
colors[NOT_SECURE][SECURITY_TEXT] =
color_utils::PickMoreReadableColor(kDarkNotSecureText,
kLightNotSecureText,
colors[NOT_SECURE][BACKGROUND]);
colors[SECURE][SECURITY_TEXT] = SkColorSetRGB(0, 150, 20);
colors[NOT_SECURE][SECURITY_INFO_BUBBLE_TEXT] =
colors[NOT_SECURE][SECURITY_TEXT];
const SkColor kDarkSecureInfoBubbleText = SkColorSetRGB(0, 153, 51);
const SkColor kLightSecureInfoBubbleText = SkColorSetRGB(102, 255, 152);
colors[SECURE][SECURITY_INFO_BUBBLE_TEXT] =
color_utils::PickMoreReadableColor(kDarkSecureInfoBubbleText,
kLightSecureInfoBubbleText,
colors[NOT_SECURE][BACKGROUND]);
const SkColor kDarkSchemeStrikeout = SkColorSetRGB(210, 0, 0);
const SkColor kLightSchemeStrikeout = SkColorSetRGB(255, 45, 45);
colors[NOT_SECURE][SCHEME_STRIKEOUT] =
color_utils::PickMoreReadableColor(kDarkSchemeStrikeout,
kLightSchemeStrikeout,
colors[NOT_SECURE][BACKGROUND]);
colors[SECURE][SCHEME_STRIKEOUT] = 0; // Unused
initialized = true;
}
return colors[is_secure ? SECURE : NOT_SECURE][kind];
}
void LocationBarView::Update(const TabContents* tab_for_state_restoring) {
SetSecurityIcon(model_->GetIcon());
RefreshPageActionViews();
std::wstring info_text, info_tooltip;
ToolbarModel::InfoTextType info_text_type =
model_->GetInfoText(&info_text, &info_tooltip);
SetInfoText(info_text, info_text_type, info_tooltip);
location_entry_->Update(tab_for_state_restoring);
Layout();
SchedulePaint();
}
void LocationBarView::UpdatePageActions() {
RefreshPageActionViews();
Layout();
SchedulePaint();
}
void LocationBarView::InvalidatePageActions() {
DeletePageActionViews();
}
void LocationBarView::Focus() {
// Focus the location entry native view.
location_entry_->SetFocus();
}
void LocationBarView::SetProfile(Profile* profile) {
DCHECK(profile);
if (profile_ != profile) {
profile_ = profile;
location_entry_->model()->SetProfile(profile);
selected_keyword_view_.set_profile(profile);
keyword_hint_view_.set_profile(profile);
security_image_view_.set_profile(profile);
}
}
gfx::Size LocationBarView::GetPreferredSize() {
return gfx::Size(0,
(popup_window_mode_ ? kPopupBackground : kBackground)->height());
}
void LocationBarView::Layout() {
DoLayout(true);
}
void LocationBarView::Paint(gfx::Canvas* canvas) {
View::Paint(canvas);
const SkBitmap* background =
popup_window_mode_ ?
kPopupBackground :
GetThemeProvider()->GetBitmapNamed(IDR_LOCATIONBG);
canvas->TileImageInt(*background, 0, 0, 0, 0, width(), height());
int top_margin = TopMargin();
canvas->FillRectInt(
GetColor(model_->GetSchemeSecurityLevel() == ToolbarModel::SECURE,
BACKGROUND),
0, top_margin, width(), std::max(height() - top_margin - kVertMargin, 0));
}
void LocationBarView::VisibleBoundsInRootChanged() {
location_entry_->ClosePopup();
}
#if defined(OS_WIN)
bool LocationBarView::OnMousePressed(const views::MouseEvent& event) {
UINT msg;
if (event.IsLeftMouseButton()) {
msg = (event.GetFlags() & views::MouseEvent::EF_IS_DOUBLE_CLICK) ?
WM_LBUTTONDBLCLK : WM_LBUTTONDOWN;
} else if (event.IsMiddleMouseButton()) {
msg = (event.GetFlags() & views::MouseEvent::EF_IS_DOUBLE_CLICK) ?
WM_MBUTTONDBLCLK : WM_MBUTTONDOWN;
} else if (event.IsRightMouseButton()) {
msg = (event.GetFlags() & views::MouseEvent::EF_IS_DOUBLE_CLICK) ?
WM_RBUTTONDBLCLK : WM_RBUTTONDOWN;
} else {
NOTREACHED();
return false;
}
OnMouseEvent(event, msg);
return true;
}
bool LocationBarView::OnMouseDragged(const views::MouseEvent& event) {
OnMouseEvent(event, WM_MOUSEMOVE);
return true;
}
void LocationBarView::OnMouseReleased(const views::MouseEvent& event,
bool canceled) {
UINT msg;
if (canceled) {
msg = WM_CAPTURECHANGED;
} else if (event.IsLeftMouseButton()) {
msg = WM_LBUTTONUP;
} else if (event.IsMiddleMouseButton()) {
msg = WM_MBUTTONUP;
} else if (event.IsRightMouseButton()) {
msg = WM_RBUTTONUP;
} else {
NOTREACHED();
return;
}
OnMouseEvent(event, msg);
}
#endif
void LocationBarView::OnAutocompleteAccept(
const GURL& url,
WindowOpenDisposition disposition,
PageTransition::Type transition,
const GURL& alternate_nav_url) {
if (!url.is_valid())
return;
location_input_ = UTF8ToWide(url.spec());
disposition_ = disposition;
transition_ = transition;
if (command_updater_) {
if (!alternate_nav_url.is_valid()) {
command_updater_->ExecuteCommand(IDC_OPEN_CURRENT_URL);
return;
}
scoped_ptr<AlternateNavURLFetcher> fetcher(
new AlternateNavURLFetcher(alternate_nav_url));
// The AlternateNavURLFetcher will listen for the pending navigation
// notification that will be issued as a result of the "open URL." It
// will automatically install itself into that navigation controller.
command_updater_->ExecuteCommand(IDC_OPEN_CURRENT_URL);
if (fetcher->state() == AlternateNavURLFetcher::NOT_STARTED) {
// I'm not sure this should be reachable, but I'm not also sure enough
// that it shouldn't to stick in a NOTREACHED(). In any case, this is
// harmless; we can simply let the fetcher get deleted here and it will
// clean itself up properly.
} else {
fetcher.release(); // The navigation controller will delete the fetcher.
}
}
}
void LocationBarView::OnChanged() {
DoLayout(false);
}
void LocationBarView::OnSetFocus() {
views::FocusManager* focus_manager = GetFocusManager();
if (!focus_manager) {
NOTREACHED();
return;
}
focus_manager->SetFocusedView(this);
}
SkBitmap LocationBarView::GetFavIcon() const {
DCHECK(delegate_);
DCHECK(delegate_->GetTabContents());
return delegate_->GetTabContents()->GetFavIcon();
}
std::wstring LocationBarView::GetTitle() const {
DCHECK(delegate_);
DCHECK(delegate_->GetTabContents());
return UTF16ToWideHack(delegate_->GetTabContents()->GetTitle());
}
void LocationBarView::DoLayout(const bool force_layout) {
if (!location_entry_.get())
return;
int entry_width = width() - (kEntryPadding * 2);
gfx::Size page_action_size;
for (size_t i = 0; i < page_action_image_views_.size(); i++) {
if (page_action_image_views_[i]->IsVisible()) {
page_action_size = page_action_image_views_[i]->GetPreferredSize();
entry_width -= page_action_size.width() + kInnerPadding;
}
}
gfx::Size security_image_size;
if (security_image_view_.IsVisible()) {
security_image_size = security_image_view_.GetPreferredSize();
entry_width -= security_image_size.width() + kInnerPadding;
}
gfx::Size info_label_size;
if (info_label_.IsVisible()) {
info_label_size = info_label_.GetPreferredSize();
entry_width -= (info_label_size.width() + kInnerPadding);
}
#if defined(OS_WIN)
RECT formatting_rect;
location_entry_->GetRect(&formatting_rect);
RECT edit_bounds;
location_entry_->GetClientRect(&edit_bounds);
int max_edit_width = entry_width - formatting_rect.left -
(edit_bounds.right - formatting_rect.right);
#else
int max_edit_width = entry_width;
#endif
if (max_edit_width < 0)
return;
const int available_width = AvailableWidth(max_edit_width);
bool needs_layout = force_layout;
needs_layout |= AdjustHints(available_width);
if (!needs_layout)
return;
// TODO(sky): baseline layout.
int location_y = TopMargin();
int location_height = std::max(height() - location_y - kVertMargin, 0);
// First set the bounds for the label that appears to the right of the
// security icon.
int offset = width() - kEntryPadding;
if (info_label_.IsVisible()) {
offset -= info_label_size.width();
info_label_.SetBounds(offset, location_y,
info_label_size.width(), location_height);
offset -= kInnerPadding;
}
if (security_image_view_.IsVisible()) {
offset -= security_image_size.width();
security_image_view_.SetBounds(offset, location_y,
security_image_size.width(),
location_height);
offset -= kInnerPadding;
}
for (size_t i = 0; i < page_action_image_views_.size(); i++) {
if (page_action_image_views_[i]->IsVisible()) {
page_action_size = page_action_image_views_[i]->GetPreferredSize();
offset -= page_action_size.width();
page_action_image_views_[i]->SetBounds(offset, location_y,
page_action_size.width(),
location_height);
offset -= kInnerPadding;
}
}
gfx::Rect location_bounds(kEntryPadding, location_y, entry_width,
location_height);
if (selected_keyword_view_.IsVisible()) {
LayoutView(true, &selected_keyword_view_, available_width,
&location_bounds);
} else if (keyword_hint_view_.IsVisible()) {
LayoutView(false, &keyword_hint_view_, available_width,
&location_bounds);
} else if (type_to_search_view_.IsVisible()) {
LayoutView(false, &type_to_search_view_, available_width,
&location_bounds);
}
location_entry_view_->SetBounds(location_bounds);
if (!force_layout) {
// If force_layout is false and we got this far it means one of the views
// was added/removed or changed in size. We need to paint ourselves.
SchedulePaint();
}
}
int LocationBarView::TopMargin() const {
return std::min(kVertMargin, height());
}
int LocationBarView::AvailableWidth(int location_bar_width) {
#if defined(OS_WIN)
// Use font_.GetStringWidth() instead of
// PosFromChar(location_entry_->GetTextLength()) because PosFromChar() is
// apparently buggy. In both LTR UI and RTL UI with left-to-right layout,
// PosFromChar(i) might return 0 when i is greater than 1.
return std::max(
location_bar_width - font_.GetStringWidth(location_entry_->GetText()), 0);
#else
return location_bar_width - location_entry_->TextWidth();
#endif
}
bool LocationBarView::UsePref(int pref_width, int available_width) {
return (pref_width + kInnerPadding <= available_width);
}
bool LocationBarView::NeedsResize(View* view, int available_width) {
gfx::Size size = view->GetPreferredSize();
if (!UsePref(size.width(), available_width))
size = view->GetMinimumSize();
return (view->width() != size.width());
}
bool LocationBarView::AdjustHints(int available_width) {
const std::wstring keyword(location_entry_->model()->keyword());
const bool is_keyword_hint(location_entry_->model()->is_keyword_hint());
const bool show_selected_keyword = !keyword.empty() && !is_keyword_hint;
const bool show_keyword_hint = !keyword.empty() && is_keyword_hint;
bool show_search_hint(location_entry_->model()->show_search_hint());
DCHECK(keyword.empty() || !show_search_hint);
if (show_search_hint) {
// Only show type to search if all the text fits.
gfx::Size view_pref = type_to_search_view_.GetPreferredSize();
show_search_hint = UsePref(view_pref.width(), available_width);
}
// NOTE: This isn't just one big || statement as ToggleVisibility MUST be
// invoked for each view.
bool needs_layout = false;
needs_layout |= ToggleVisibility(show_selected_keyword,
&selected_keyword_view_);
needs_layout |= ToggleVisibility(show_keyword_hint, &keyword_hint_view_);
needs_layout |= ToggleVisibility(show_search_hint, &type_to_search_view_);
if (show_selected_keyword) {
if (selected_keyword_view_.keyword() != keyword) {
needs_layout = true;
selected_keyword_view_.SetKeyword(keyword);
}
needs_layout |= NeedsResize(&selected_keyword_view_, available_width);
} else if (show_keyword_hint) {
if (keyword_hint_view_.keyword() != keyword) {
needs_layout = true;
keyword_hint_view_.SetKeyword(keyword);
}
needs_layout |= NeedsResize(&keyword_hint_view_, available_width);
}
return needs_layout;
}
void LocationBarView::LayoutView(bool leading,
views::View* view,
int available_width,
gfx::Rect* bounds) {
DCHECK(view && bounds);
gfx::Size view_size = view->GetPreferredSize();
if (!UsePref(view_size.width(), available_width))
view_size = view->GetMinimumSize();
if (view_size.width() + kInnerPadding < bounds->width()) {
view->SetVisible(true);
if (leading) {
view->SetBounds(bounds->x(), bounds->y(), view_size.width(),
bounds->height());
bounds->Offset(view_size.width() + kInnerPadding, 0);
} else {
view->SetBounds(bounds->right() - view_size.width(), bounds->y(),
view_size.width(), bounds->height());
}
bounds->set_width(bounds->width() - view_size.width() - kInnerPadding);
} else {
view->SetVisible(false);
}
}
void LocationBarView::SetSecurityIcon(ToolbarModel::Icon icon) {
switch (icon) {
case ToolbarModel::LOCK_ICON:
security_image_view_.SetImageShown(SecurityImageView::LOCK);
security_image_view_.SetVisible(true);
break;
case ToolbarModel::WARNING_ICON:
security_image_view_.SetImageShown(SecurityImageView::WARNING);
security_image_view_.SetVisible(true);
break;
case ToolbarModel::NO_ICON:
security_image_view_.SetVisible(false);
break;
default:
NOTREACHED();
security_image_view_.SetVisible(false);
break;
}
}
void LocationBarView::DeletePageActionViews() {
if (!page_action_image_views_.empty()) {
for (size_t i = 0; i < page_action_image_views_.size(); ++i)
RemoveChildView(page_action_image_views_[i]);
STLDeleteContainerPointers(page_action_image_views_.begin(),
page_action_image_views_.end());
page_action_image_views_.clear();
}
}
void LocationBarView::RefreshPageActionViews() {
std::vector<PageAction*> page_actions;
if (profile_->GetExtensionsService())
page_actions = profile_->GetExtensionsService()->GetPageActions();
// On startup we sometimes haven't loaded any extensions. This makes sure
// we catch up when the extensions (and any page actions) load.
if (page_actions.size() != page_action_image_views_.size()) {
DeletePageActionViews(); // Delete the old views (if any).
page_action_image_views_.resize(page_actions.size());
for (size_t i = 0; i < page_actions.size(); ++i) {
page_action_image_views_[i] = new PageActionImageView(this, profile_,
page_actions[i], bubble_positioner_);
page_action_image_views_[i]->SetVisible(false);
page_action_image_views_[i]->SetParentOwned(false);
AddChildView(page_action_image_views_[i]);
}
}
TabContents* contents = delegate_->GetTabContents();
if (!page_action_image_views_.empty() && contents) {
GURL url = GURL(WideToUTF8(model_->GetText()));
for (size_t i = 0; i < page_action_image_views_.size(); i++)
page_action_image_views_[i]->UpdateVisibility(contents, url);
}
}
void LocationBarView::SetInfoText(const std::wstring& text,
ToolbarModel::InfoTextType text_type,
const std::wstring& tooltip_text) {
info_label_.SetVisible(!text.empty());
info_label_.SetText(text);
if (text_type == ToolbarModel::INFO_EV_TEXT)
info_label_.SetColor(GetColor(true, SECURITY_TEXT));
info_label_.SetTooltipText(tooltip_text);
}
bool LocationBarView::ToggleVisibility(bool new_vis, View* view) {
DCHECK(view);
if (view->IsVisible() != new_vis) {
view->SetVisible(new_vis);
return true;
}
return false;
}
#if defined(OS_WIN)
void LocationBarView::OnMouseEvent(const views::MouseEvent& event, UINT msg) {
UINT flags = 0;
if (event.IsControlDown())
flags |= MK_CONTROL;
if (event.IsShiftDown())
flags |= MK_SHIFT;
if (event.IsLeftMouseButton())
flags |= MK_LBUTTON;
if (event.IsMiddleMouseButton())
flags |= MK_MBUTTON;
if (event.IsRightMouseButton())
flags |= MK_RBUTTON;
gfx::Point screen_point(event.location());
ConvertPointToScreen(this, &screen_point);
location_entry_->HandleExternalMsg(msg, flags, screen_point.ToPOINT());
}
#endif
bool LocationBarView::GetAccessibleName(std::wstring* name) {
DCHECK(name);
if (!accessible_name_.empty()) {
name->assign(accessible_name_);
return true;
}
return false;
}
bool LocationBarView::GetAccessibleRole(AccessibilityTypes::Role* role) {
DCHECK(role);
*role = AccessibilityTypes::ROLE_GROUPING;
return true;
}
void LocationBarView::SetAccessibleName(const std::wstring& name) {
accessible_name_.assign(name);
}
// SelectedKeywordView -------------------------------------------------------
// The background is drawn using ImagePainter3. This is the left/center/right
// image names.
static const int kBorderImages[] = {
IDR_LOCATION_BAR_SELECTED_KEYWORD_BACKGROUND_L,
IDR_LOCATION_BAR_SELECTED_KEYWORD_BACKGROUND_C,
IDR_LOCATION_BAR_SELECTED_KEYWORD_BACKGROUND_R };
// Insets around the label.
static const int kTopInset = 0;
static const int kBottomInset = 0;
static const int kLeftInset = 4;
static const int kRightInset = 4;
// Offset from the top the background is drawn at.
static const int kBackgroundYOffset = 2;
LocationBarView::SelectedKeywordView::SelectedKeywordView(Profile* profile)
: background_painter_(kBorderImages),
profile_(profile) {
AddChildView(&full_label_);
AddChildView(&partial_label_);
// Full_label and partial_label are deleted by us, make sure View doesn't
// delete them too.
full_label_.SetParentOwned(false);
partial_label_.SetParentOwned(false);
full_label_.SetVisible(false);
partial_label_.SetVisible(false);
full_label_.set_border(
views::Border::CreateEmptyBorder(kTopInset, kLeftInset, kBottomInset,
kRightInset));
partial_label_.set_border(
views::Border::CreateEmptyBorder(kTopInset, kLeftInset, kBottomInset,
kRightInset));
}
LocationBarView::SelectedKeywordView::~SelectedKeywordView() {
}
void LocationBarView::SelectedKeywordView::SetFont(const gfx::Font& font) {
full_label_.SetFont(font);
partial_label_.SetFont(font);
}
void LocationBarView::SelectedKeywordView::Paint(gfx::Canvas* canvas) {
canvas->TranslateInt(0, kBackgroundYOffset);
background_painter_.Paint(width(), height() - kTopInset, canvas);
canvas->TranslateInt(0, -kBackgroundYOffset);
}
gfx::Size LocationBarView::SelectedKeywordView::GetPreferredSize() {
return full_label_.GetPreferredSize();
}
gfx::Size LocationBarView::SelectedKeywordView::GetMinimumSize() {
return partial_label_.GetMinimumSize();
}
void LocationBarView::SelectedKeywordView::Layout() {
gfx::Size pref = GetPreferredSize();
bool at_pref = (width() == pref.width());
if (at_pref)
full_label_.SetBounds(0, 0, width(), height());
else
partial_label_.SetBounds(0, 0, width(), height());
full_label_.SetVisible(at_pref);
partial_label_.SetVisible(!at_pref);
}
void LocationBarView::SelectedKeywordView::SetKeyword(
const std::wstring& keyword) {
keyword_ = keyword;
if (keyword.empty())
return;
DCHECK(profile_);
if (!profile_->GetTemplateURLModel())
return;
const std::wstring short_name = GetKeywordName(profile_, keyword);
full_label_.SetText(l10n_util::GetStringF(IDS_OMNIBOX_KEYWORD_TEXT,
short_name));
const std::wstring min_string = CalculateMinString(short_name);
if (!min_string.empty()) {
partial_label_.SetText(
l10n_util::GetStringF(IDS_OMNIBOX_KEYWORD_TEXT, min_string));
} else {
partial_label_.SetText(full_label_.GetText());
}
}
std::wstring LocationBarView::SelectedKeywordView::CalculateMinString(
const std::wstring& description) {
// Chop at the first '.' or whitespace.
const size_t dot_index = description.find(L'.');
const size_t ws_index = description.find_first_of(kWhitespaceWide);
size_t chop_index = std::min(dot_index, ws_index);
std::wstring min_string;
if (chop_index == std::wstring::npos) {
// No dot or whitespace, truncate to at most 3 chars.
min_string = l10n_util::TruncateString(description, 3);
} else {
min_string = description.substr(0, chop_index);
}
l10n_util::AdjustStringForLocaleDirection(min_string, &min_string);
return min_string;
}
// KeywordHintView -------------------------------------------------------------
// Amount of space to offset the tab image from the top of the view by.
static const int kTabImageYOffset = 4;
LocationBarView::KeywordHintView::KeywordHintView(Profile* profile)
: profile_(profile) {
AddChildView(&leading_label_);
AddChildView(&trailing_label_);
if (!kTabButtonBitmap) {
kTabButtonBitmap = ResourceBundle::GetSharedInstance().
GetBitmapNamed(IDR_LOCATION_BAR_KEYWORD_HINT_TAB);
}
}
LocationBarView::KeywordHintView::~KeywordHintView() {
// Labels are freed by us. Remove them so that View doesn't
// try to free them too.
RemoveChildView(&leading_label_);
RemoveChildView(&trailing_label_);
}
void LocationBarView::KeywordHintView::SetFont(const gfx::Font& font) {
leading_label_.SetFont(font);
trailing_label_.SetFont(font);
}
void LocationBarView::KeywordHintView::SetColor(const SkColor& color) {
leading_label_.SetColor(color);
trailing_label_.SetColor(color);
}
void LocationBarView::KeywordHintView::SetKeyword(const std::wstring& keyword) {
keyword_ = keyword;
if (keyword_.empty())
return;
DCHECK(profile_);
if (!profile_->GetTemplateURLModel())
return;
std::vector<size_t> content_param_offsets;
const std::wstring keyword_hint(l10n_util::GetStringF(
IDS_OMNIBOX_KEYWORD_HINT, std::wstring(),
GetKeywordName(profile_, keyword), &content_param_offsets));
if (content_param_offsets.size() == 2) {
leading_label_.SetText(keyword_hint.substr(0,
content_param_offsets.front()));
trailing_label_.SetText(keyword_hint.substr(content_param_offsets.front()));
} else {
// See comments on an identical NOTREACHED() in search_provider.cc.
NOTREACHED();
}
}
void LocationBarView::KeywordHintView::Paint(gfx::Canvas* canvas) {
int image_x = leading_label_.IsVisible() ? leading_label_.width() : 0;
// Since we paint the button image directly on the canvas (instead of using a
// child view), we must mirror the button's position manually if the locale
// is right-to-left.
gfx::Rect tab_button_bounds(image_x,
kTabImageYOffset,
kTabButtonBitmap->width(),
kTabButtonBitmap->height());
tab_button_bounds.set_x(MirroredLeftPointForRect(tab_button_bounds));
canvas->DrawBitmapInt(*kTabButtonBitmap,
tab_button_bounds.x(),
tab_button_bounds.y());
}
gfx::Size LocationBarView::KeywordHintView::GetPreferredSize() {
// TODO(sky): currently height doesn't matter, once baseline support is
// added this should check baselines.
gfx::Size prefsize = leading_label_.GetPreferredSize();
int width = prefsize.width();
width += kTabButtonBitmap->width();
prefsize = trailing_label_.GetPreferredSize();
width += prefsize.width();
return gfx::Size(width, prefsize.height());
}
gfx::Size LocationBarView::KeywordHintView::GetMinimumSize() {
// TODO(sky): currently height doesn't matter, once baseline support is
// added this should check baselines.
return gfx::Size(kTabButtonBitmap->width(), 0);
}
void LocationBarView::KeywordHintView::Layout() {
// TODO(sky): baseline layout.
bool show_labels = (width() != kTabButtonBitmap->width());
leading_label_.SetVisible(show_labels);
trailing_label_.SetVisible(show_labels);
int x = 0;
gfx::Size pref;
if (show_labels) {
pref = leading_label_.GetPreferredSize();
leading_label_.SetBounds(x, 0, pref.width(), height());
x += pref.width() + kTabButtonBitmap->width();
pref = trailing_label_.GetPreferredSize();
trailing_label_.SetBounds(x, 0, pref.width(), height());
}
}
bool LocationBarView::SkipDefaultKeyEventProcessing(const views::KeyEvent& e) {
if (keyword_hint_view_.IsVisible() &&
views::FocusManager::IsTabTraversalKeyEvent(e)) {
// We want to receive tab key events when the hint is showing.
return true;
}
#if defined(OS_WIN)
return location_entry_->SkipDefaultKeyEventProcessing(e);
#else
// TODO(jcampan): We need to refactor the code of
// AutocompleteEditViewWin::SkipDefaultKeyEventProcessing into this class so
// it can be shared between Windows and Linux.
// For now, we just override back-space as it is the accelerator for back
// navigation.
if (e.GetCharacter() == base::VKEY_BACK)
return true;
return false;
#endif
}
// ShowInfoBubbleTask-----------------------------------------------------------
class LocationBarView::ShowInfoBubbleTask : public Task {
public:
explicit ShowInfoBubbleTask(
LocationBarView::LocationBarImageView* image_view);
virtual void Run();
void Cancel();
private:
LocationBarView::LocationBarImageView* image_view_;
bool cancelled_;
DISALLOW_COPY_AND_ASSIGN(ShowInfoBubbleTask);
};
LocationBarView::ShowInfoBubbleTask::ShowInfoBubbleTask(
LocationBarView::LocationBarImageView* image_view)
: image_view_(image_view),
cancelled_(false) {
}
void LocationBarView::ShowInfoBubbleTask::Run() {
if (cancelled_)
return;
if (!image_view_->GetWidget()->IsActive()) {
// The browser is no longer active. Let's not show the info bubble, this
// would make the browser the active window again. Also makes sure we NULL
// show_info_bubble_task_ to prevent the SecurityImageView from keeping a
// dangling pointer.
image_view_->show_info_bubble_task_ = NULL;
return;
}
image_view_->ShowInfoBubble();
}
void LocationBarView::ShowInfoBubbleTask::Cancel() {
cancelled_ = true;
}
// -----------------------------------------------------------------------------
void LocationBarView::ShowFirstRunBubbleInternal(bool use_OEM_bubble) {
if (!location_entry_view_)
return;
if (!location_entry_view_->GetWidget()->IsActive()) {
// The browser is no longer active. Let's not show the info bubble, this
// would make the browser the active window again.
return;
}
gfx::Point location;
// If the UI layout is RTL, the coordinate system is not transformed and
// therefore we need to adjust the X coordinate so that bubble appears on the
// right hand side of the location bar.
if (UILayoutIsRightToLeft())
location.Offset(width(), 0);
views::View::ConvertPointToScreen(this, &location);
// We try to guess that 20 pixels offset is a good place for the first
// letter in the OmniBox.
gfx::Rect bounds(location.x(), location.y(), 20, height());
// Moving the bounds "backwards" so that it appears within the location bar
// if the UI layout is RTL.
if (UILayoutIsRightToLeft())
bounds.set_x(location.x() - 20);
#if defined(OS_WIN)
FirstRunBubble::Show(profile_, GetWindow(), bounds, use_OEM_bubble);
#else
// First run bubble doesn't make sense for Chrome OS.
#endif
}
// LocationBarImageView---------------------------------------------------------
LocationBarView::LocationBarImageView::LocationBarImageView(
const BubblePositioner* bubble_positioner)
: info_bubble_(NULL),
show_info_bubble_task_(NULL),
bubble_positioner_(bubble_positioner) {
}
LocationBarView::LocationBarImageView::~LocationBarImageView() {
if (show_info_bubble_task_)
show_info_bubble_task_->Cancel();
if (info_bubble_)
info_bubble_->Close();
}
void LocationBarView::LocationBarImageView::OnMouseMoved(
const views::MouseEvent& event) {
if (show_info_bubble_task_) {
show_info_bubble_task_->Cancel();
show_info_bubble_task_ = NULL;
}
if (info_bubble_) {
// If an info bubble is currently showing, nothing to do.
return;
}
show_info_bubble_task_ = new ShowInfoBubbleTask(this);
MessageLoop::current()->PostDelayedTask(FROM_HERE, show_info_bubble_task_,
kInfoBubbleHoverDelayMs);
}
void LocationBarView::LocationBarImageView::OnMouseExited(
const views::MouseEvent& event) {
if (show_info_bubble_task_) {
show_info_bubble_task_->Cancel();
show_info_bubble_task_ = NULL;
}
if (info_bubble_)
info_bubble_->Close();
}
void LocationBarView::LocationBarImageView::InfoBubbleClosing(
InfoBubble* info_bubble, bool closed_by_escape) {
info_bubble_ = NULL;
}
void LocationBarView::LocationBarImageView::ShowInfoBubbleImpl(
const std::wstring& text, SkColor text_color) {
gfx::Rect bounds(bubble_positioner_->GetLocationStackBounds());
gfx::Point location;
views::View::ConvertPointToScreen(this, &location);
bounds.set_x(location.x());
bounds.set_width(width());
views::Label* label = new views::Label(text);
label->SetMultiLine(true);
label->SetColor(text_color);
label->SetFont(ResourceBundle::GetSharedInstance().GetFont(
ResourceBundle::BaseFont).DeriveFont(2));
label->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
label->SizeToFit(0);
DCHECK(info_bubble_ == NULL);
info_bubble_ = InfoBubble::Show(GetWindow(), bounds, label, this);
show_info_bubble_task_ = NULL;
}
// SecurityImageView------------------------------------------------------------
// static
SkBitmap* LocationBarView::SecurityImageView::lock_icon_ = NULL;
SkBitmap* LocationBarView::SecurityImageView::warning_icon_ = NULL;
LocationBarView::SecurityImageView::SecurityImageView(
Profile* profile,
ToolbarModel* model,
const BubblePositioner* bubble_positioner)
: LocationBarImageView(bubble_positioner),
profile_(profile),
model_(model) {
if (!lock_icon_) {
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
lock_icon_ = rb.GetBitmapNamed(IDR_LOCK);
warning_icon_ = rb.GetBitmapNamed(IDR_WARNING);
}
SetImageShown(LOCK);
}
LocationBarView::SecurityImageView::~SecurityImageView() {
}
void LocationBarView::SecurityImageView::SetImageShown(Image image) {
switch (image) {
case LOCK:
ImageView::SetImage(lock_icon_);
break;
case WARNING:
ImageView::SetImage(warning_icon_);
break;
default:
NOTREACHED();
break;
}
}
bool LocationBarView::SecurityImageView::OnMousePressed(
const views::MouseEvent& event) {
TabContents* tab = BrowserList::GetLastActive()->GetSelectedTabContents();
NavigationEntry* nav_entry = tab->controller().GetActiveEntry();
if (!nav_entry) {
NOTREACHED();
return true;
}
tab->ShowPageInfo(nav_entry->url(), nav_entry->ssl(), true);
return true;
}
void LocationBarView::SecurityImageView::ShowInfoBubble() {
std::wstring text;
model_->GetIconHoverText(&text);
ShowInfoBubbleImpl(text, GetColor(
model_->GetSecurityLevel() == ToolbarModel::SECURE,
SECURITY_INFO_BUBBLE_TEXT));
}
// PageActionImageView----------------------------------------------------------
LocationBarView::PageActionImageView::PageActionImageView(
LocationBarView* owner,
Profile* profile,
const PageAction* page_action,
const BubblePositioner* bubble_positioner)
: LocationBarImageView(bubble_positioner),
owner_(owner),
profile_(profile),
page_action_(page_action),
current_tab_id_(-1),
tooltip_(page_action_->name()) {
Extension* extension = profile->GetExtensionsService()->GetExtensionById(
page_action->extension_id());
DCHECK(extension);
// Load the images this view needs asynchronously on the file thread. We'll
// get a call back into OnImageLoaded if the image loads successfully. If not,
// the ImageView will have no image and will not appear in the Omnibox.
DCHECK(!page_action->icon_paths().empty());
const std::vector<std::string>& icon_paths = page_action->icon_paths();
page_action_icons_.resize(icon_paths.size());
tracker_ = new ImageLoadingTracker(this, icon_paths.size());
for (std::vector<std::string>::const_iterator iter = icon_paths.begin();
iter != icon_paths.end(); ++iter) {
FilePath path = extension->GetResourcePath(*iter);
tracker_->PostLoadImageTask(path);
}
}
LocationBarView::PageActionImageView::~PageActionImageView() {
if (tracker_)
tracker_->StopTrackingImageLoad();
}
bool LocationBarView::PageActionImageView::OnMousePressed(
const views::MouseEvent& event) {
int button = -1;
if (event.IsLeftMouseButton())
button = 1;
else if (event.IsMiddleMouseButton())
button = 2;
else if (event.IsRightMouseButton())
button = 3;
// Our PageAction icon was clicked on, notify proper authorities.
ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted(
profile_, page_action_->extension_id(), page_action_->id(),
current_tab_id_, current_url_.spec(), button);
return true;
}
void LocationBarView::PageActionImageView::ShowInfoBubble() {
ShowInfoBubbleImpl(ASCIIToWide(tooltip_), GetColor(false, TEXT));
}
void LocationBarView::PageActionImageView::OnImageLoaded(SkBitmap* image,
size_t index) {
DCHECK(index < page_action_icons_.size());
if (index == page_action_icons_.size() - 1)
tracker_ = NULL; // The tracker object will delete itself when we return.
page_action_icons_[index] = *image;
owner_->UpdatePageActions();
}
void LocationBarView::PageActionImageView::UpdateVisibility(
TabContents* contents, GURL url) {
// Save this off so we can pass it back to the extension when the action gets
// executed. See PageActionImageView::OnMousePressed.
current_tab_id_ = ExtensionTabUtil::GetTabId(contents);
current_url_ = url;
const PageActionState* state = contents->GetPageActionState(page_action_);
bool visible = state != NULL;
if (visible) {
// Set the tooltip.
if (state->title().empty())
tooltip_ = page_action_->name();
else
tooltip_ = state->title();
// Set the image.
int index = state->icon_index();
// The image index (if not within bounds) will be set to the first image.
if (index < 0 || index >= static_cast<int>(page_action_icons_.size()))
index = 0;
ImageView::SetImage(page_action_icons_[index]);
}
SetVisible(visible);
}
////////////////////////////////////////////////////////////////////////////////
// LocationBarView, LocationBar implementation:
void LocationBarView::ShowFirstRunBubble(bool use_OEM_bubble) {
// We wait 30 milliseconds to open. It allows less flicker.
Task* task = first_run_bubble_.NewRunnableMethod(
&LocationBarView::ShowFirstRunBubbleInternal, use_OEM_bubble);
MessageLoop::current()->PostDelayedTask(FROM_HERE, task, 30);
}
std::wstring LocationBarView::GetInputString() const {
return location_input_;
}
WindowOpenDisposition LocationBarView::GetWindowOpenDisposition() const {
return disposition_;
}
PageTransition::Type LocationBarView::GetPageTransition() const {
return transition_;
}
void LocationBarView::AcceptInput() {
location_entry_->model()->AcceptInput(CURRENT_TAB, false);
}
void LocationBarView::AcceptInputWithDisposition(WindowOpenDisposition disp) {
location_entry_->model()->AcceptInput(disp, false);
}
void LocationBarView::FocusLocation() {
location_entry_->SetFocus();
location_entry_->SelectAll(true);
}
void LocationBarView::FocusSearch() {
location_entry_->SetFocus();
location_entry_->SetForcedQuery();
}
void LocationBarView::SaveStateToContents(TabContents* contents) {
location_entry_->SaveStateToTab(contents);
}
void LocationBarView::Revert() {
location_entry_->RevertAll();
}
int LocationBarView::PageActionVisibleCount() {
int result = 0;
for (size_t i = 0; i < page_action_image_views_.size(); i++) {
if (page_action_image_views_[i]->IsVisible())
++result;
}
return result;
}
|