| 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 |
| 67 window->SetAlwaysOnTop(true); | 69 window->SetAlwaysOnTop(true); |
| 68 window->Show(); | 70 window->Show(); |
| 69 views::AcceleratorHandler accelerator_handler; | 71 views::AcceleratorHandler accelerator_handler; |
| 70 MessageLoopForUI::current()->RunWithDispatcher(&accelerator_handler); | 72 MessageLoopForUI::current()->RunWithDispatcher(&accelerator_handler); |
| 73 window->Close(); |
| 71 } | 74 } |
| 72 | 75 |
| 73 } // namespace first_run | 76 } // namespace first_run |
| 74 | 77 |
| 75 SearchEngineChoice::SearchEngineChoice(views::ButtonListener* listener, | 78 SearchEngineChoice::SearchEngineChoice(views::ButtonListener* listener, |
| 76 const TemplateURL* search_engine, | 79 const TemplateURL* search_engine, |
| 77 bool use_small_logos) | 80 bool use_small_logos) |
| 78 : NativeTextButton( | 81 : NativeTextButton( |
| 79 listener, | 82 listener, |
| 80 UTF16ToWide(l10n_util::GetStringUTF16(IDS_FR_SEARCH_CHOOSE))), | 83 UTF16ToWide(l10n_util::GetStringUTF16(IDS_FR_SEARCH_CHOOSE))), |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 } | 136 } |
| 134 } | 137 } |
| 135 | 138 |
| 136 void SearchEngineChoice::SetChoiceViewBounds(int x, int y, int width, | 139 void SearchEngineChoice::SetChoiceViewBounds(int x, int y, int width, |
| 137 int height) { | 140 int height) { |
| 138 choice_view_->SetBounds(x, y, width, height); | 141 choice_view_->SetBounds(x, y, width, height); |
| 139 } | 142 } |
| 140 | 143 |
| 141 FirstRunSearchEngineView::FirstRunSearchEngineView(Profile* profile, | 144 FirstRunSearchEngineView::FirstRunSearchEngineView(Profile* profile, |
| 142 bool randomize) | 145 bool randomize) |
| 143 : views::ClientView(NULL, NULL), | 146 : background_image_(NULL), |
| 144 background_image_(NULL), | |
| 145 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)), | 147 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)), |
| 146 text_direction_is_rtl_(base::i18n::IsRTL()), | 148 text_direction_is_rtl_(base::i18n::IsRTL()), |
| 149 template_url_service_loaded_(false), |
| 147 added_to_view_hierarchy_(false), | 150 added_to_view_hierarchy_(false), |
| 148 randomize_(randomize), | 151 randomize_(randomize) { |
| 149 user_chosen_engine_(false), | |
| 150 fallback_choice_(NULL), | |
| 151 quit_on_closing_(true) { | |
| 152 // Don't show ourselves until all the search engines have loaded from | 152 // Don't show ourselves until all the search engines have loaded from |
| 153 // the profile -- otherwise we have nothing to show. | 153 // the profile -- otherwise we have nothing to show. |
| 154 SetVisible(false); | 154 SetVisible(false); |
| 155 | 155 |
| 156 // Start loading the search engines for the given profile. The service is | 156 // Start loading the search engines for the given profile. |
| 157 // already loaded in tests. | |
| 158 DCHECK(template_url_service_); | 157 DCHECK(template_url_service_); |
| 159 if (!template_url_service_->loaded()) { | 158 DCHECK(!template_url_service_->loaded()); |
| 160 template_url_service_->AddObserver(this); | 159 template_url_service_->AddObserver(this); |
| 161 template_url_service_->Load(); | 160 template_url_service_->Load(); |
| 162 } | |
| 163 } | 161 } |
| 164 | 162 |
| 165 FirstRunSearchEngineView::~FirstRunSearchEngineView() { | 163 FirstRunSearchEngineView::~FirstRunSearchEngineView() { |
| 166 template_url_service_->RemoveObserver(this); | 164 template_url_service_->RemoveObserver(this); |
| 167 } | 165 } |
| 168 | 166 |
| 169 string16 FirstRunSearchEngineView::GetWindowTitle() const { | 167 string16 FirstRunSearchEngineView::GetWindowTitle() const { |
| 170 return l10n_util::GetStringUTF16(IDS_FIRSTRUN_DLG_TITLE); | 168 return l10n_util::GetStringUTF16(IDS_FIRSTRUN_DLG_TITLE); |
| 171 } | 169 } |
| 172 | 170 |
| 173 views::View* FirstRunSearchEngineView::GetContentsView() { | |
| 174 return this; | |
| 175 } | |
| 176 | |
| 177 views::ClientView* FirstRunSearchEngineView::CreateClientView( | |
| 178 views::Widget* widget) { | |
| 179 return this; | |
| 180 } | |
| 181 | |
| 182 void FirstRunSearchEngineView::WindowClosing() { | |
| 183 // If the window is closed by clicking the close button, we default to the | |
| 184 // engine in the first slot. | |
| 185 if (!user_chosen_engine_) | |
| 186 ChooseSearchEngine(fallback_choice_); | |
| 187 if (quit_on_closing_) | |
| 188 MessageLoop::current()->Quit(); | |
| 189 } | |
| 190 | |
| 191 views::Widget* FirstRunSearchEngineView::GetWidget() { | |
| 192 return View::GetWidget(); | |
| 193 } | |
| 194 | |
| 195 const views::Widget* FirstRunSearchEngineView::GetWidget() const { | |
| 196 return View::GetWidget(); | |
| 197 } | |
| 198 | |
| 199 bool FirstRunSearchEngineView::CanClose() { | |
| 200 // We need a valid search engine to set as default, so if the user tries to | |
| 201 // close the window before the template URL service is loaded, we must prevent | |
| 202 // this from happening. | |
| 203 return fallback_choice_ != NULL; | |
| 204 } | |
| 205 | |
| 206 void FirstRunSearchEngineView::ButtonPressed(views::Button* sender, | 171 void FirstRunSearchEngineView::ButtonPressed(views::Button* sender, |
| 207 const views::Event& event) { | 172 const views::Event& event) { |
| 208 ChooseSearchEngine(static_cast<SearchEngineChoice*>(sender)); | 173 SearchEngineChoice* choice = static_cast<SearchEngineChoice*>(sender); |
| 209 GetWidget()->Close(); | 174 DCHECK(template_url_service_); |
| 210 // This will call through to WindowClosing() above and will quit the message | 175 template_url_service_->SetSearchEngineDialogSlot(choice->slot()); |
| 211 // loop. | 176 const TemplateURL* default_search = choice->GetSearchEngine(); |
| 177 if (default_search) |
| 178 template_url_service_->SetDefaultSearchProvider(default_search); |
| 179 |
| 180 MessageLoop::current()->Quit(); |
| 212 } | 181 } |
| 213 | 182 |
| 214 void FirstRunSearchEngineView::OnPaint(gfx::Canvas* canvas) { | 183 void FirstRunSearchEngineView::OnPaint(gfx::Canvas* canvas) { |
| 215 // Fill in behind the background image with the standard gray toolbar color. | 184 // Fill in behind the background image with the standard gray toolbar color. |
| 216 canvas->FillRect(GetThemeProvider()->GetColor(ThemeService::COLOR_TOOLBAR), | 185 canvas->FillRect(GetThemeProvider()->GetColor(ThemeService::COLOR_TOOLBAR), |
| 217 gfx::Rect(0, 0, width(), background_image_->height())); | 186 gfx::Rect(0, 0, width(), background_image_->height())); |
| 218 // The rest of the dialog background should be white. | 187 // The rest of the dialog background should be white. |
| 219 DCHECK(height() > background_image_->height()); | 188 DCHECK(height() > background_image_->height()); |
| 220 canvas->FillRect(SK_ColorWHITE, | 189 canvas->FillRect(SK_ColorWHITE, |
| 221 gfx::Rect(0, background_image_->height(), width(), | 190 gfx::Rect(0, background_image_->height(), width(), |
| 222 height() - background_image_->height())); | 191 height() - background_image_->height())); |
| 223 } | 192 } |
| 224 | 193 |
| 225 void FirstRunSearchEngineView::OnTemplateURLServiceChanged() { | 194 void FirstRunSearchEngineView::OnTemplateURLServiceChanged() { |
| 226 // We only watch the search engine model change once, on load. Remove | 195 // We only watch the search engine model change once, on load. Remove |
| 227 // observer so we don't try to redraw if engines change under us. | 196 // observer so we don't try to redraw if engines change under us. |
| 228 template_url_service_->RemoveObserver(this); | 197 template_url_service_->RemoveObserver(this); |
| 198 |
| 199 template_url_service_loaded_ = true; |
| 229 AddSearchEnginesIfPossible(); | 200 AddSearchEnginesIfPossible(); |
| 230 } | 201 } |
| 231 | 202 |
| 232 gfx::Size FirstRunSearchEngineView::GetPreferredSize() { | 203 gfx::Size FirstRunSearchEngineView::GetPreferredSize() { |
| 233 return views::Widget::GetLocalizedContentsSize( | 204 return views::Widget::GetLocalizedContentsSize( |
| 234 IDS_FIRSTRUN_SEARCH_ENGINE_SELECTION_WIDTH_CHARS, | 205 IDS_FIRSTRUN_SEARCH_ENGINE_SELECTION_WIDTH_CHARS, |
| 235 IDS_FIRSTRUN_SEARCH_ENGINE_SELECTION_HEIGHT_LINES); | 206 IDS_FIRSTRUN_SEARCH_ENGINE_SELECTION_HEIGHT_LINES); |
| 236 } | 207 } |
| 237 | 208 |
| 238 void FirstRunSearchEngineView::Layout() { | 209 void FirstRunSearchEngineView::Layout() { |
| 210 // Disable the close button. |
| 211 GetWidget()->EnableClose(false); |
| 212 |
| 239 gfx::Size pref_size = background_image_->GetPreferredSize(); | 213 gfx::Size pref_size = background_image_->GetPreferredSize(); |
| 240 background_image_->SetBounds(0, 0, GetPreferredSize().width(), | 214 background_image_->SetBounds(0, 0, GetPreferredSize().width(), |
| 241 pref_size.height()); | 215 pref_size.height()); |
| 242 | 216 |
| 243 // General vertical spacing between elements: | 217 // General vertical spacing between elements: |
| 244 const int kVertSpacing = 8; | 218 const int kVertSpacing = 8; |
| 245 // Percentage of vertical space around logos to use for upper padding. | 219 // Percentage of vertical space around logos to use for upper padding. |
| 246 const double kUpperPaddingPercent = 0.4; | 220 const double kUpperPaddingPercent = 0.4; |
| 247 | 221 |
| 248 int num_choices = search_engine_choices_.size(); | 222 int num_choices = search_engine_choices_.size(); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 AddSearchEnginesIfPossible(); | 362 AddSearchEnginesIfPossible(); |
| 389 } | 363 } |
| 390 } | 364 } |
| 391 | 365 |
| 392 void FirstRunSearchEngineView::GetAccessibleState( | 366 void FirstRunSearchEngineView::GetAccessibleState( |
| 393 ui::AccessibleViewState* state) { | 367 ui::AccessibleViewState* state) { |
| 394 state->role = ui::AccessibilityTypes::ROLE_ALERT; | 368 state->role = ui::AccessibilityTypes::ROLE_ALERT; |
| 395 } | 369 } |
| 396 | 370 |
| 397 void FirstRunSearchEngineView::AddSearchEnginesIfPossible() { | 371 void FirstRunSearchEngineView::AddSearchEnginesIfPossible() { |
| 398 if (!template_url_service_->loaded() || !added_to_view_hierarchy_) | 372 if (!template_url_service_loaded_ || !added_to_view_hierarchy_) |
| 399 return; | 373 return; |
| 400 | 374 |
| 401 // Add search engines in template_url_service_ to buttons list. The | 375 // Add search engines in template_url_service_ to buttons list. The |
| 402 // first three will always be from prepopulated data. | 376 // first three will always be from prepopulated data. |
| 403 std::vector<const TemplateURL*> template_urls = | 377 std::vector<const TemplateURL*> template_urls = |
| 404 template_url_service_->GetTemplateURLs(); | 378 template_url_service_->GetTemplateURLs(); |
| 405 | 379 |
| 406 // If we have fewer than two search engines, end search engine dialog | 380 // If we have fewer than two search engines, end search engine dialog |
| 407 // immediately, leaving imported default search engine setting intact. | 381 // immediately, leaving imported default search engine setting intact. |
| 408 if (template_urls.size() < 2) { | 382 if (template_urls.size() < 2) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 AddChildView(choice->GetView()); // The logo or text view. | 424 AddChildView(choice->GetView()); // The logo or text view. |
| 451 AddChildView(choice); // The button associated with the choice. | 425 AddChildView(choice); // The button associated with the choice. |
| 452 } | 426 } |
| 453 // Push the default choice to the fourth position. | 427 // Push the default choice to the fourth position. |
| 454 if (default_choice) { | 428 if (default_choice) { |
| 455 search_engine_choices_.push_back(default_choice); | 429 search_engine_choices_.push_back(default_choice); |
| 456 AddChildView(default_choice->GetView()); // The logo or text view. | 430 AddChildView(default_choice->GetView()); // The logo or text view. |
| 457 AddChildView(default_choice); // The button associated with the choice. | 431 AddChildView(default_choice); // The button associated with the choice. |
| 458 } | 432 } |
| 459 | 433 |
| 460 // It is critically important that this line happens before randomization is | |
| 461 // done below. | |
| 462 fallback_choice_ = search_engine_choices_.front(); | |
| 463 | |
| 464 // Randomize order of logos if option has been set. | 434 // Randomize order of logos if option has been set. |
| 465 if (randomize_) { | 435 if (randomize_) { |
| 466 std::random_shuffle(search_engine_choices_.begin(), | 436 std::random_shuffle(search_engine_choices_.begin(), |
| 467 search_engine_choices_.end(), | 437 search_engine_choices_.end(), |
| 468 base::RandGenerator); | 438 base::RandGenerator); |
| 469 // Assign to each choice the position in which it is shown on the screen. | 439 // Assign to each choice the position in which it is shown on the screen. |
| 470 std::vector<SearchEngineChoice*>::iterator it; | 440 std::vector<SearchEngineChoice*>::iterator it; |
| 471 int slot = 0; | 441 int slot = 0; |
| 472 for (it = search_engine_choices_.begin(); | 442 for (it = search_engine_choices_.begin(); |
| 473 it != search_engine_choices_.end(); | 443 it != search_engine_choices_.end(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 492 (*it)->SetText((*it)->GetSearchEngine()->short_name()); | 462 (*it)->SetText((*it)->GetSearchEngine()->short_name()); |
| 493 } | 463 } |
| 494 } | 464 } |
| 495 | 465 |
| 496 // This will tell screenreaders that they should read the full text | 466 // This will tell screenreaders that they should read the full text |
| 497 // of this dialog to the user now (rather than waiting for the user | 467 // of this dialog to the user now (rather than waiting for the user |
| 498 // to explore it). | 468 // to explore it). |
| 499 GetWidget()->NotifyAccessibilityEvent( | 469 GetWidget()->NotifyAccessibilityEvent( |
| 500 this, ui::AccessibilityTypes::EVENT_ALERT, true); | 470 this, ui::AccessibilityTypes::EVENT_ALERT, true); |
| 501 } | 471 } |
| 502 | |
| 503 void FirstRunSearchEngineView::ChooseSearchEngine(SearchEngineChoice* choice) { | |
| 504 user_chosen_engine_ = true; | |
| 505 DCHECK(choice && template_url_service_); | |
| 506 template_url_service_->SetSearchEngineDialogSlot(choice->slot()); | |
| 507 const TemplateURL* default_search = choice->GetSearchEngine(); | |
| 508 if (default_search) | |
| 509 template_url_service_->SetDefaultSearchProvider(default_search); | |
| 510 } | |
| OLD | NEW |