OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/first_run_search_engine_view.h" | 5 #include "chrome/browser/ui/views/first_run_search_engine_view.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | 8 #include <map> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 bool randomize_search_engine_experiment) { | 57 bool randomize_search_engine_experiment) { |
58 // If the default search is managed via policy, we don't ask the user to | 58 // If the default search is managed via policy, we don't ask the user to |
59 // choose. | 59 // choose. |
60 TemplateURLService* model = TemplateURLServiceFactory::GetForProfile(profile); | 60 TemplateURLService* model = TemplateURLServiceFactory::GetForProfile(profile); |
61 if (!FirstRun::ShouldShowSearchEngineSelector(model)) | 61 if (!FirstRun::ShouldShowSearchEngineSelector(model)) |
62 return; | 62 return; |
63 | 63 |
64 views::Widget* window = views::Widget::CreateWindow( | 64 views::Widget* window = views::Widget::CreateWindow( |
65 new FirstRunSearchEngineView( | 65 new FirstRunSearchEngineView( |
66 profile, randomize_search_engine_experiment)); | 66 profile, randomize_search_engine_experiment)); |
67 DCHECK(window); | |
68 | |
69 window->SetAlwaysOnTop(true); | 67 window->SetAlwaysOnTop(true); |
70 window->Show(); | 68 window->Show(); |
71 views::AcceleratorHandler accelerator_handler; | 69 views::AcceleratorHandler accelerator_handler; |
72 MessageLoopForUI::current()->RunWithDispatcher(&accelerator_handler); | 70 MessageLoopForUI::current()->RunWithDispatcher(&accelerator_handler); |
73 window->Close(); | |
74 } | 71 } |
75 | 72 |
76 } // namespace first_run | 73 } // namespace first_run |
77 | 74 |
78 SearchEngineChoice::SearchEngineChoice(views::ButtonListener* listener, | 75 SearchEngineChoice::SearchEngineChoice(views::ButtonListener* listener, |
79 const TemplateURL* search_engine, | 76 const TemplateURL* search_engine, |
80 bool use_small_logos) | 77 bool use_small_logos) |
81 : NativeTextButton( | 78 : NativeTextButton( |
82 listener, | 79 listener, |
83 UTF16ToWide(l10n_util::GetStringUTF16(IDS_FR_SEARCH_CHOOSE))), | 80 UTF16ToWide(l10n_util::GetStringUTF16(IDS_FR_SEARCH_CHOOSE))), |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 void SearchEngineChoice::SetChoiceViewBounds(int x, int y, int width, | 136 void SearchEngineChoice::SetChoiceViewBounds(int x, int y, int width, |
140 int height) { | 137 int height) { |
141 choice_view_->SetBounds(x, y, width, height); | 138 choice_view_->SetBounds(x, y, width, height); |
142 } | 139 } |
143 | 140 |
144 FirstRunSearchEngineView::FirstRunSearchEngineView(Profile* profile, | 141 FirstRunSearchEngineView::FirstRunSearchEngineView(Profile* profile, |
145 bool randomize) | 142 bool randomize) |
146 : background_image_(NULL), | 143 : background_image_(NULL), |
147 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)), | 144 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)), |
148 text_direction_is_rtl_(base::i18n::IsRTL()), | 145 text_direction_is_rtl_(base::i18n::IsRTL()), |
149 template_url_service_loaded_(false), | |
150 added_to_view_hierarchy_(false), | 146 added_to_view_hierarchy_(false), |
151 randomize_(randomize) { | 147 randomize_(randomize), |
| 148 user_chosen_engine_(false), |
| 149 fallback_choice_(NULL), |
| 150 quit_on_closing_(true) { |
152 // Don't show ourselves until all the search engines have loaded from | 151 // Don't show ourselves until all the search engines have loaded from |
153 // the profile -- otherwise we have nothing to show. | 152 // the profile -- otherwise we have nothing to show. |
154 SetVisible(false); | 153 SetVisible(false); |
155 | 154 |
156 // Start loading the search engines for the given profile. | 155 // Start loading the search engines for the given profile. The service is |
| 156 // already loaded in tests. |
157 DCHECK(template_url_service_); | 157 DCHECK(template_url_service_); |
158 DCHECK(!template_url_service_->loaded()); | 158 if (!template_url_service_->loaded()) { |
159 template_url_service_->AddObserver(this); | 159 template_url_service_->AddObserver(this); |
160 template_url_service_->Load(); | 160 template_url_service_->Load(); |
| 161 } |
161 } | 162 } |
162 | 163 |
163 FirstRunSearchEngineView::~FirstRunSearchEngineView() { | 164 FirstRunSearchEngineView::~FirstRunSearchEngineView() { |
164 template_url_service_->RemoveObserver(this); | 165 template_url_service_->RemoveObserver(this); |
165 } | 166 } |
166 | 167 |
167 string16 FirstRunSearchEngineView::GetWindowTitle() const { | 168 string16 FirstRunSearchEngineView::GetWindowTitle() const { |
168 return l10n_util::GetStringUTF16(IDS_FIRSTRUN_DLG_TITLE); | 169 return l10n_util::GetStringUTF16(IDS_FIRSTRUN_DLG_TITLE); |
169 } | 170 } |
170 | 171 |
| 172 void FirstRunSearchEngineView::WindowClosing() { |
| 173 // If the window is closed by clicking the close button, we default to the |
| 174 // engine in the first slot. |
| 175 if (!user_chosen_engine_) |
| 176 ChooseSearchEngine(fallback_choice_); |
| 177 if (quit_on_closing_) |
| 178 MessageLoop::current()->Quit(); |
| 179 } |
| 180 |
171 void FirstRunSearchEngineView::ButtonPressed(views::Button* sender, | 181 void FirstRunSearchEngineView::ButtonPressed(views::Button* sender, |
172 const views::Event& event) { | 182 const views::Event& event) { |
173 SearchEngineChoice* choice = static_cast<SearchEngineChoice*>(sender); | 183 ChooseSearchEngine(static_cast<SearchEngineChoice*>(sender)); |
174 DCHECK(template_url_service_); | 184 GetWidget()->Close(); |
175 template_url_service_->SetSearchEngineDialogSlot(choice->slot()); | 185 // This will call through to WindowClosing() above and will quit the message |
176 const TemplateURL* default_search = choice->GetSearchEngine(); | 186 // loop. |
177 if (default_search) | |
178 template_url_service_->SetDefaultSearchProvider(default_search); | |
179 | |
180 MessageLoop::current()->Quit(); | |
181 } | 187 } |
182 | 188 |
183 void FirstRunSearchEngineView::OnPaint(gfx::Canvas* canvas) { | 189 void FirstRunSearchEngineView::OnPaint(gfx::Canvas* canvas) { |
184 // Fill in behind the background image with the standard gray toolbar color. | 190 // Fill in behind the background image with the standard gray toolbar color. |
185 canvas->FillRect(GetThemeProvider()->GetColor(ThemeService::COLOR_TOOLBAR), | 191 canvas->FillRect(GetThemeProvider()->GetColor(ThemeService::COLOR_TOOLBAR), |
186 gfx::Rect(0, 0, width(), background_image_->height())); | 192 gfx::Rect(0, 0, width(), background_image_->height())); |
187 // The rest of the dialog background should be white. | 193 // The rest of the dialog background should be white. |
188 DCHECK(height() > background_image_->height()); | 194 DCHECK(height() > background_image_->height()); |
189 canvas->FillRect(SK_ColorWHITE, | 195 canvas->FillRect(SK_ColorWHITE, |
190 gfx::Rect(0, background_image_->height(), width(), | 196 gfx::Rect(0, background_image_->height(), width(), |
191 height() - background_image_->height())); | 197 height() - background_image_->height())); |
192 } | 198 } |
193 | 199 |
194 void FirstRunSearchEngineView::OnTemplateURLServiceChanged() { | 200 void FirstRunSearchEngineView::OnTemplateURLServiceChanged() { |
195 // We only watch the search engine model change once, on load. Remove | 201 // We only watch the search engine model change once, on load. Remove |
196 // observer so we don't try to redraw if engines change under us. | 202 // observer so we don't try to redraw if engines change under us. |
197 template_url_service_->RemoveObserver(this); | 203 template_url_service_->RemoveObserver(this); |
198 | |
199 template_url_service_loaded_ = true; | |
200 AddSearchEnginesIfPossible(); | 204 AddSearchEnginesIfPossible(); |
201 } | 205 } |
202 | 206 |
203 gfx::Size FirstRunSearchEngineView::GetPreferredSize() { | 207 gfx::Size FirstRunSearchEngineView::GetPreferredSize() { |
204 return views::Widget::GetLocalizedContentsSize( | 208 return views::Widget::GetLocalizedContentsSize( |
205 IDS_FIRSTRUN_SEARCH_ENGINE_SELECTION_WIDTH_CHARS, | 209 IDS_FIRSTRUN_SEARCH_ENGINE_SELECTION_WIDTH_CHARS, |
206 IDS_FIRSTRUN_SEARCH_ENGINE_SELECTION_HEIGHT_LINES); | 210 IDS_FIRSTRUN_SEARCH_ENGINE_SELECTION_HEIGHT_LINES); |
207 } | 211 } |
208 | 212 |
209 void FirstRunSearchEngineView::Layout() { | 213 void FirstRunSearchEngineView::Layout() { |
210 // Disable the close button. | |
211 GetWidget()->EnableClose(false); | |
212 | |
213 gfx::Size pref_size = background_image_->GetPreferredSize(); | 214 gfx::Size pref_size = background_image_->GetPreferredSize(); |
214 background_image_->SetBounds(0, 0, GetPreferredSize().width(), | 215 background_image_->SetBounds(0, 0, GetPreferredSize().width(), |
215 pref_size.height()); | 216 pref_size.height()); |
216 | 217 |
217 // General vertical spacing between elements: | 218 // General vertical spacing between elements: |
218 const int kVertSpacing = 8; | 219 const int kVertSpacing = 8; |
219 // Percentage of vertical space around logos to use for upper padding. | 220 // Percentage of vertical space around logos to use for upper padding. |
220 const double kUpperPaddingPercent = 0.4; | 221 const double kUpperPaddingPercent = 0.4; |
221 | 222 |
222 int num_choices = search_engine_choices_.size(); | 223 int num_choices = search_engine_choices_.size(); |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 AddSearchEnginesIfPossible(); | 363 AddSearchEnginesIfPossible(); |
363 } | 364 } |
364 } | 365 } |
365 | 366 |
366 void FirstRunSearchEngineView::GetAccessibleState( | 367 void FirstRunSearchEngineView::GetAccessibleState( |
367 ui::AccessibleViewState* state) { | 368 ui::AccessibleViewState* state) { |
368 state->role = ui::AccessibilityTypes::ROLE_ALERT; | 369 state->role = ui::AccessibilityTypes::ROLE_ALERT; |
369 } | 370 } |
370 | 371 |
371 void FirstRunSearchEngineView::AddSearchEnginesIfPossible() { | 372 void FirstRunSearchEngineView::AddSearchEnginesIfPossible() { |
372 if (!template_url_service_loaded_ || !added_to_view_hierarchy_) | 373 if (!template_url_service_->loaded() || !added_to_view_hierarchy_) |
373 return; | 374 return; |
374 | 375 |
375 // Add search engines in template_url_service_ to buttons list. The | 376 // Add search engines in template_url_service_ to buttons list. The |
376 // first three will always be from prepopulated data. | 377 // first three will always be from prepopulated data. |
377 std::vector<const TemplateURL*> template_urls = | 378 std::vector<const TemplateURL*> template_urls = |
378 template_url_service_->GetTemplateURLs(); | 379 template_url_service_->GetTemplateURLs(); |
379 | 380 |
380 // If we have fewer than two search engines, end search engine dialog | 381 // If we have fewer than two search engines, end search engine dialog |
381 // immediately, leaving imported default search engine setting intact. | 382 // immediately, leaving imported default search engine setting intact. |
382 if (template_urls.size() < 2) { | 383 if (template_urls.size() < 2) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 AddChildView(choice->GetView()); // The logo or text view. | 425 AddChildView(choice->GetView()); // The logo or text view. |
425 AddChildView(choice); // The button associated with the choice. | 426 AddChildView(choice); // The button associated with the choice. |
426 } | 427 } |
427 // Push the default choice to the fourth position. | 428 // Push the default choice to the fourth position. |
428 if (default_choice) { | 429 if (default_choice) { |
429 search_engine_choices_.push_back(default_choice); | 430 search_engine_choices_.push_back(default_choice); |
430 AddChildView(default_choice->GetView()); // The logo or text view. | 431 AddChildView(default_choice->GetView()); // The logo or text view. |
431 AddChildView(default_choice); // The button associated with the choice. | 432 AddChildView(default_choice); // The button associated with the choice. |
432 } | 433 } |
433 | 434 |
| 435 // It is critically important that this line happens before randomization is |
| 436 // done below. |
| 437 fallback_choice_ = search_engine_choices_.front(); |
| 438 |
434 // Randomize order of logos if option has been set. | 439 // Randomize order of logos if option has been set. |
435 if (randomize_) { | 440 if (randomize_) { |
436 std::random_shuffle(search_engine_choices_.begin(), | 441 std::random_shuffle(search_engine_choices_.begin(), |
437 search_engine_choices_.end(), | 442 search_engine_choices_.end(), |
438 base::RandGenerator); | 443 base::RandGenerator); |
439 // Assign to each choice the position in which it is shown on the screen. | 444 // Assign to each choice the position in which it is shown on the screen. |
440 std::vector<SearchEngineChoice*>::iterator it; | 445 std::vector<SearchEngineChoice*>::iterator it; |
441 int slot = 0; | 446 int slot = 0; |
442 for (it = search_engine_choices_.begin(); | 447 for (it = search_engine_choices_.begin(); |
443 it != search_engine_choices_.end(); | 448 it != search_engine_choices_.end(); |
(...skipping 18 matching lines...) Expand all Loading... |
462 (*it)->SetText((*it)->GetSearchEngine()->short_name()); | 467 (*it)->SetText((*it)->GetSearchEngine()->short_name()); |
463 } | 468 } |
464 } | 469 } |
465 | 470 |
466 // This will tell screenreaders that they should read the full text | 471 // This will tell screenreaders that they should read the full text |
467 // of this dialog to the user now (rather than waiting for the user | 472 // of this dialog to the user now (rather than waiting for the user |
468 // to explore it). | 473 // to explore it). |
469 GetWidget()->NotifyAccessibilityEvent( | 474 GetWidget()->NotifyAccessibilityEvent( |
470 this, ui::AccessibilityTypes::EVENT_ALERT, true); | 475 this, ui::AccessibilityTypes::EVENT_ALERT, true); |
471 } | 476 } |
| 477 |
| 478 void FirstRunSearchEngineView::ChooseSearchEngine(SearchEngineChoice* choice) { |
| 479 user_chosen_engine_ = true; |
| 480 DCHECK(template_url_service_); |
| 481 template_url_service_->SetSearchEngineDialogSlot(choice->slot()); |
| 482 const TemplateURL* default_search = choice->GetSearchEngine(); |
| 483 if (default_search) |
| 484 template_url_service_->SetDefaultSearchProvider(default_search); |
| 485 } |
OLD | NEW |