• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/ui/views/toolbar/reload_button.h"
6 
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/app/chrome_command_ids.h"
9 #include "chrome/browser/command_updater.h"
10 #include "chrome/browser/search/search.h"
11 #include "chrome/browser/ui/search/search_model.h"
12 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
13 #include "grit/generated_resources.h"
14 #include "grit/theme_resources.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/base/models/simple_menu_model.h"
17 #include "ui/base/theme_provider.h"
18 #include "ui/base/window_open_disposition.h"
19 #include "ui/views/metrics.h"
20 #include "ui/views/widget/widget.h"
21 
22 
23 namespace {
24 
25 // Contents of the Reload drop-down menu.
26 const int kReloadMenuItems[]  = {
27   IDS_RELOAD_MENU_NORMAL_RELOAD_ITEM,
28   IDS_RELOAD_MENU_HARD_RELOAD_ITEM,
29   IDS_RELOAD_MENU_EMPTY_AND_HARD_RELOAD_ITEM,
30 };
31 
32 }  // namespace
33 
34 
35 // ReloadButton ---------------------------------------------------------------
36 
37 // static
38 const char ReloadButton::kViewClassName[] = "ReloadButton";
39 
ReloadButton(LocationBarView * location_bar,CommandUpdater * command_updater)40 ReloadButton::ReloadButton(LocationBarView* location_bar,
41                            CommandUpdater* command_updater)
42     : ToolbarButton(this, CreateMenuModel()),
43       location_bar_(location_bar),
44       command_updater_(command_updater),
45       intended_mode_(MODE_RELOAD),
46       visible_mode_(MODE_RELOAD),
47       double_click_timer_delay_(
48           base::TimeDelta::FromMilliseconds(views::GetDoubleClickInterval())),
49       stop_to_reload_timer_delay_(base::TimeDelta::FromMilliseconds(1350)),
50       menu_enabled_(false),
51       testing_mouse_hovered_(false),
52       testing_reload_count_(0) {
53 }
54 
~ReloadButton()55 ReloadButton::~ReloadButton() {
56 }
57 
ChangeMode(Mode mode,bool force)58 void ReloadButton::ChangeMode(Mode mode, bool force) {
59   intended_mode_ = mode;
60 
61   // If the change is forced, or the user isn't hovering the icon, or it's safe
62   // to change it to the other image type, make the change immediately;
63   // otherwise we'll let it happen later.
64   if (force || (!IsMouseHovered() && !testing_mouse_hovered_) ||
65       ((mode == MODE_STOP) ?
66       !double_click_timer_.IsRunning() : (visible_mode_ != MODE_STOP))) {
67     double_click_timer_.Stop();
68     stop_to_reload_timer_.Stop();
69     if (mode != visible_mode_)
70       ChangeModeInternal(mode);
71     SetEnabled(true);
72 
73   // We want to disable the button if we're preventing a change from stop to
74   // reload due to hovering, but not if we're preventing a change from reload to
75   // stop due to the double-click timer running.  (Disabled reload state is only
76   // applicable when instant extended API is enabled and mode is NTP, which is
77   // handled just above.)
78   } else if (visible_mode_ != MODE_RELOAD) {
79     SetEnabled(false);
80 
81     // Go ahead and change to reload after a bit, which allows repeated reloads
82     // without moving the mouse.
83     if (!stop_to_reload_timer_.IsRunning()) {
84       stop_to_reload_timer_.Start(FROM_HERE, stop_to_reload_timer_delay_, this,
85                                   &ReloadButton::OnStopToReloadTimer);
86     }
87   }
88 }
89 
LoadImages()90 void ReloadButton::LoadImages() {
91   ChangeModeInternal(visible_mode_);
92 
93   SchedulePaint();
94   PreferredSizeChanged();
95 }
96 
OnMouseExited(const ui::MouseEvent & event)97 void ReloadButton::OnMouseExited(const ui::MouseEvent& event) {
98   ToolbarButton::OnMouseExited(event);
99   if (!IsMenuShowing())
100     ChangeMode(intended_mode_, true);
101 }
102 
GetTooltipText(const gfx::Point & p,base::string16 * tooltip) const103 bool ReloadButton::GetTooltipText(const gfx::Point& p,
104                                   base::string16* tooltip) const {
105   int reload_tooltip = menu_enabled_ ?
106       IDS_TOOLTIP_RELOAD_WITH_MENU : IDS_TOOLTIP_RELOAD;
107   int text_id = (visible_mode_ == MODE_RELOAD) ?
108       reload_tooltip : IDS_TOOLTIP_STOP;
109   tooltip->assign(l10n_util::GetStringUTF16(text_id));
110   return true;
111 }
112 
GetClassName() const113 const char* ReloadButton::GetClassName() const {
114   return kViewClassName;
115 }
116 
GetAccessibleState(ui::AccessibleViewState * state)117 void ReloadButton::GetAccessibleState(ui::AccessibleViewState* state) {
118   if (menu_enabled_)
119     ToolbarButton::GetAccessibleState(state);
120   else
121     CustomButton::GetAccessibleState(state);
122 }
123 
ShouldShowMenu()124 bool ReloadButton::ShouldShowMenu() {
125   return menu_enabled_ && (visible_mode_ == MODE_RELOAD);
126 }
127 
ShowDropDownMenu(ui::MenuSourceType source_type)128 void ReloadButton::ShowDropDownMenu(ui::MenuSourceType source_type) {
129   ToolbarButton::ShowDropDownMenu(source_type);  // Blocks.
130   ChangeMode(intended_mode_, true);
131 }
132 
ButtonPressed(views::Button *,const ui::Event & event)133 void ReloadButton::ButtonPressed(views::Button* /* button */,
134                                  const ui::Event& event) {
135   ClearPendingMenu();
136 
137   if (visible_mode_ == MODE_STOP) {
138     if (command_updater_)
139       command_updater_->ExecuteCommandWithDisposition(IDC_STOP, CURRENT_TAB);
140     // The user has clicked, so we can feel free to update the button,
141     // even if the mouse is still hovering.
142     ChangeMode(MODE_RELOAD, true);
143   } else if (!double_click_timer_.IsRunning()) {
144     // Shift-clicking or ctrl-clicking the reload button means we should ignore
145     // any cached content.
146     int command;
147     int flags = event.flags();
148     if (event.IsShiftDown() || event.IsControlDown()) {
149       command = IDC_RELOAD_IGNORING_CACHE;
150       // Mask off Shift and Control so they don't affect the disposition below.
151       flags &= ~(ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN);
152     } else {
153       command = IDC_RELOAD;
154     }
155 
156     // Start a timer - while this timer is running, the reload button cannot be
157     // changed to a stop button.  We do not set |intended_mode_| to MODE_STOP
158     // here as the browser will do that when it actually starts loading (which
159     // may happen synchronously, thus the need to do this before telling the
160     // browser to execute the reload command).
161     double_click_timer_.Start(FROM_HERE, double_click_timer_delay_, this,
162                               &ReloadButton::OnDoubleClickTimer);
163 
164     ExecuteBrowserCommand(command, flags);
165     ++testing_reload_count_;
166   }
167 }
168 
IsCommandIdChecked(int command_id) const169 bool ReloadButton::IsCommandIdChecked(int command_id) const {
170   return false;
171 }
172 
IsCommandIdEnabled(int command_id) const173 bool ReloadButton::IsCommandIdEnabled(int command_id) const {
174   return true;
175 }
176 
IsCommandIdVisible(int command_id) const177 bool ReloadButton::IsCommandIdVisible(int command_id) const {
178   return true;
179 }
180 
GetAcceleratorForCommandId(int command_id,ui::Accelerator * accelerator)181 bool ReloadButton::GetAcceleratorForCommandId(int command_id,
182     ui::Accelerator* accelerator) {
183   switch (command_id) {
184     case IDS_RELOAD_MENU_NORMAL_RELOAD_ITEM:
185       GetWidget()->GetAccelerator(IDC_RELOAD, accelerator);
186       return true;
187     case IDS_RELOAD_MENU_HARD_RELOAD_ITEM:
188       GetWidget()->GetAccelerator(IDC_RELOAD_IGNORING_CACHE, accelerator);
189       return true;
190   }
191   return GetWidget()->GetAccelerator(command_id, accelerator);
192 }
193 
ExecuteCommand(int command_id,int event_flags)194 void ReloadButton::ExecuteCommand(int command_id, int event_flags) {
195   int browser_command = 0;
196   switch (command_id) {
197     case IDS_RELOAD_MENU_NORMAL_RELOAD_ITEM:
198       browser_command = IDC_RELOAD;
199       break;
200     case IDS_RELOAD_MENU_HARD_RELOAD_ITEM:
201       browser_command = IDC_RELOAD_IGNORING_CACHE;
202       break;
203     case IDS_RELOAD_MENU_EMPTY_AND_HARD_RELOAD_ITEM:
204       browser_command = IDC_RELOAD_CLEARING_CACHE;
205       break;
206     default:
207       NOTREACHED();
208   }
209   ExecuteBrowserCommand(browser_command, event_flags);
210 }
211 
CreateMenuModel()212 ui::SimpleMenuModel* ReloadButton::CreateMenuModel() {
213   ui::SimpleMenuModel* menu_model = new ui::SimpleMenuModel(this);
214   for (size_t i = 0; i < arraysize(kReloadMenuItems); ++i)
215     menu_model->AddItemWithStringId(kReloadMenuItems[i], kReloadMenuItems[i]);
216 
217   return menu_model;
218 }
219 
ExecuteBrowserCommand(int command,int event_flags)220 void ReloadButton::ExecuteBrowserCommand(int command, int event_flags) {
221   if (!command_updater_)
222     return;
223 
224   WindowOpenDisposition disposition =
225       ui::DispositionFromEventFlags(event_flags);
226   if ((disposition == CURRENT_TAB) && location_bar_) {
227     // Forcibly reset the location bar, since otherwise it won't discard any
228     // ongoing user edits, since it doesn't realize this is a user-initiated
229     // action.
230     location_bar_->Revert();
231   }
232   command_updater_->ExecuteCommandWithDisposition(command, disposition);
233 }
234 
ChangeModeInternal(Mode mode)235 void ReloadButton::ChangeModeInternal(Mode mode) {
236   ui::ThemeProvider* tp = GetThemeProvider();
237   // |tp| can be NULL in unit tests.
238   if (tp) {
239     SetImage(views::Button::STATE_NORMAL, *(tp->GetImageSkiaNamed(
240         (mode == MODE_RELOAD) ? IDR_RELOAD : IDR_STOP)));
241     SetImage(views::Button::STATE_DISABLED, *(tp->GetImageSkiaNamed(
242         (mode == MODE_RELOAD) ? IDR_RELOAD_D : IDR_STOP_D)));
243   }
244 
245   visible_mode_ = mode;
246   SchedulePaint();
247 }
248 
OnDoubleClickTimer()249 void ReloadButton::OnDoubleClickTimer() {
250   if (!IsMenuShowing())
251     ChangeMode(intended_mode_, false);
252 }
253 
OnStopToReloadTimer()254 void ReloadButton::OnStopToReloadTimer() {
255   DCHECK(!IsMenuShowing());
256   ChangeMode(intended_mode_, true);
257 }
258