Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: components/search_engines/template_url_service.cc

Issue 2814743006: Refactoring after comments in https://codereview.chromium.org/2639153002 (Closed)
Patch Set: Fixed after review, round 2 Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/search_engines/template_url_service.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "components/search_engines/template_url_service.h" 5 #include "components/search_engines/template_url_service.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/auto_reset.h" 10 #include "base/auto_reset.h"
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 TemplateURL* initial_dsp = initial_default_search_provider_.get(); 424 TemplateURL* initial_dsp = initial_default_search_provider_.get();
425 return (initial_dsp && 425 return (initial_dsp &&
426 (initial_dsp->GenerateSearchURL(search_terms_data()).host_piece() == 426 (initial_dsp->GenerateSearchURL(search_terms_data()).host_piece() ==
427 host)) 427 host))
428 ? initial_dsp 428 ? initial_dsp
429 : nullptr; 429 : nullptr;
430 } 430 }
431 431
432 TemplateURL* TemplateURLService::Add( 432 TemplateURL* TemplateURLService::Add(
433 std::unique_ptr<TemplateURL> template_url) { 433 std::unique_ptr<TemplateURL> template_url) {
434 DCHECK(template_url);
435 DCHECK(
436 !IsCreatedByExtension(template_url.get()) ||
437 (!FindTemplateURLForExtension(template_url->extension_info_->extension_id,
438 template_url->type()) &&
439 template_url->id() == kInvalidTemplateURLID));
440
434 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); 441 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get());
435 TemplateURL* template_url_ptr = AddNoNotify(std::move(template_url), true); 442 TemplateURL* template_url_ptr = AddNoNotify(std::move(template_url), true);
436 if (template_url_ptr) 443 if (template_url_ptr)
437 NotifyObservers(); 444 NotifyObservers();
438 return template_url_ptr; 445 return template_url_ptr;
439 } 446 }
440 447
441 TemplateURL* TemplateURLService::AddWithOverrides( 448 TemplateURL* TemplateURLService::AddWithOverrides(
442 std::unique_ptr<TemplateURL> template_url, 449 std::unique_ptr<TemplateURL> template_url,
443 const base::string16& short_name, 450 const base::string16& short_name,
444 const base::string16& keyword, 451 const base::string16& keyword,
445 const std::string& url) { 452 const std::string& url) {
446 DCHECK(!short_name.empty()); 453 DCHECK(!short_name.empty());
447 DCHECK(!keyword.empty()); 454 DCHECK(!keyword.empty());
448 DCHECK(!url.empty()); 455 DCHECK(!url.empty());
449 template_url->data_.SetShortName(short_name); 456 template_url->data_.SetShortName(short_name);
450 template_url->data_.SetKeyword(keyword); 457 template_url->data_.SetKeyword(keyword);
451 template_url->SetURL(url); 458 template_url->SetURL(url);
452 return Add(std::move(template_url)); 459 return Add(std::move(template_url));
453 } 460 }
454 461
455 TemplateURL* TemplateURLService::AddExtensionControlledTURL(
456 std::unique_ptr<TemplateURL> template_url,
457 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info) {
458 DCHECK(template_url);
459 DCHECK_EQ(kInvalidTemplateURLID, template_url->id());
460 DCHECK(info);
461 DCHECK_NE(TemplateURL::NORMAL, template_url->type());
462 DCHECK(
463 !FindTemplateURLForExtension(info->extension_id, template_url->type()));
464
465 template_url->extension_info_.swap(info);
466
467 return Add(std::move(template_url));
468 }
469
470 void TemplateURLService::Remove(TemplateURL* template_url) { 462 void TemplateURLService::Remove(TemplateURL* template_url) {
471 RemoveNoNotify(template_url); 463 RemoveNoNotify(template_url);
472 NotifyObservers(); 464 NotifyObservers();
473 } 465 }
474 466
475 void TemplateURLService::RemoveExtensionControlledTURL( 467 void TemplateURLService::RemoveExtensionControlledTURL(
476 const std::string& extension_id, 468 const std::string& extension_id,
477 TemplateURL::Type type) { 469 TemplateURL::Type type) {
478 TemplateURL* url = FindTemplateURLForExtension(extension_id, type); 470 TemplateURL* url = FindTemplateURLForExtension(extension_id, type);
479 if (!url) 471 if (!url)
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 DCHECK(loaded_); 519 DCHECK(loaded_);
528 520
529 if (FindTemplateURLForExtension(extension_id, 521 if (FindTemplateURLForExtension(extension_id,
530 TemplateURL::OMNIBOX_API_EXTENSION)) 522 TemplateURL::OMNIBOX_API_EXTENSION))
531 return; 523 return;
532 524
533 TemplateURLData data; 525 TemplateURLData data;
534 data.SetShortName(base::UTF8ToUTF16(extension_name)); 526 data.SetShortName(base::UTF8ToUTF16(extension_name));
535 data.SetKeyword(base::UTF8ToUTF16(keyword)); 527 data.SetKeyword(base::UTF8ToUTF16(keyword));
536 data.SetURL(template_url_string); 528 data.SetURL(template_url_string);
537 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info( 529 Add(base::MakeUnique<TemplateURL>(data, TemplateURL::OMNIBOX_API_EXTENSION,
538 new TemplateURL::AssociatedExtensionInfo(extension_id)); 530 extension_id, extension_install_time,
539 info->install_time = extension_install_time; 531 false));
540 AddExtensionControlledTURL(
541 base::MakeUnique<TemplateURL>(data, TemplateURL::OMNIBOX_API_EXTENSION),
542 std::move(info));
543 } 532 }
544 533
545 TemplateURLService::TemplateURLVector TemplateURLService::GetTemplateURLs() { 534 TemplateURLService::TemplateURLVector TemplateURLService::GetTemplateURLs() {
546 TemplateURLVector result; 535 TemplateURLVector result;
547 for (const auto& turl : template_urls_) 536 for (const auto& turl : template_urls_)
548 result.push_back(turl.get()); 537 result.push_back(turl.get());
549 return result; 538 return result;
550 } 539 }
551 540
552 void TemplateURLService::IncrementUsageCount(TemplateURL* url) { 541 void TemplateURLService::IncrementUsageCount(TemplateURL* url) {
(...skipping 1419 matching lines...) Expand 10 before | Expand all | Expand 10 after
1972 update_data.SetKeyword(default_search_provider_->keyword()); 1961 update_data.SetKeyword(default_search_provider_->keyword());
1973 update_data.SetShortName(default_search_provider_->short_name()); 1962 update_data.SetShortName(default_search_provider_->short_name());
1974 } 1963 }
1975 UpdateNoNotify(default_search_provider_, TemplateURL(update_data)); 1964 UpdateNoNotify(default_search_provider_, TemplateURL(update_data));
1976 } else { 1965 } else {
1977 // Normally the prepopulated fallback should be present in 1966 // Normally the prepopulated fallback should be present in
1978 // |template_urls_|, but in a few cases it might not be: 1967 // |template_urls_|, but in a few cases it might not be:
1979 // (1) Tests that initialize the TemplateURLService in peculiar ways. 1968 // (1) Tests that initialize the TemplateURLService in peculiar ways.
1980 // (2) If the user deleted the pre-populated default and we subsequently 1969 // (2) If the user deleted the pre-populated default and we subsequently
1981 // lost their user-selected value. 1970 // lost their user-selected value.
1982 std::unique_ptr<TemplateURL> new_dse_ptr = 1971 default_search_provider_ =
1983 base::MakeUnique<TemplateURL>(*data); 1972 AddNoNotify(base::MakeUnique<TemplateURL>(*data), true);
1984 TemplateURL* new_dse = new_dse_ptr.get();
1985 if (AddNoNotify(std::move(new_dse_ptr), true))
1986 default_search_provider_ = new_dse;
1987 } 1973 }
1988 } else if (source == DefaultSearchManager::FROM_USER) { 1974 } else if (source == DefaultSearchManager::FROM_USER) {
1989 default_search_provider_ = GetTemplateURLForGUID(data->sync_guid); 1975 default_search_provider_ = GetTemplateURLForGUID(data->sync_guid);
1990 if (!default_search_provider_ && data->prepopulate_id) { 1976 if (!default_search_provider_ && data->prepopulate_id) {
1991 default_search_provider_ = 1977 default_search_provider_ =
1992 FindPrepopulatedTemplateURL(data->prepopulate_id); 1978 FindPrepopulatedTemplateURL(data->prepopulate_id);
1993 } 1979 }
1994 TemplateURLData new_data(*data); 1980 TemplateURLData new_data(*data);
1995 if (default_search_provider_) { 1981 if (default_search_provider_) {
1996 UpdateNoNotify(default_search_provider_, TemplateURL(new_data)); 1982 UpdateNoNotify(default_search_provider_, TemplateURL(new_data));
1997 } else { 1983 } else {
1998 new_data.id = kInvalidTemplateURLID; 1984 new_data.id = kInvalidTemplateURLID;
1999 std::unique_ptr<TemplateURL> new_dse_ptr = 1985 default_search_provider_ =
2000 base::MakeUnique<TemplateURL>(new_data); 1986 AddNoNotify(base::MakeUnique<TemplateURL>(new_data), true);
2001 TemplateURL* new_dse = new_dse_ptr.get();
2002 if (AddNoNotify(std::move(new_dse_ptr), true))
2003 default_search_provider_ = new_dse;
2004 } 1987 }
2005 if (default_search_provider_ && prefs_) { 1988 if (default_search_provider_ && prefs_) {
2006 prefs_->SetString(prefs::kSyncedDefaultSearchProviderGUID, 1989 prefs_->SetString(prefs::kSyncedDefaultSearchProviderGUID,
2007 default_search_provider_->sync_guid()); 1990 default_search_provider_->sync_guid());
2008 } 1991 }
2009 } 1992 }
2010 1993
2011 bool changed = default_search_provider_ != previous_default_search_engine; 1994 bool changed = default_search_provider_ != previous_default_search_engine;
2012 if (changed) 1995 if (changed)
2013 RequestGoogleURLTrackerServerCheckIfNecessary(); 1996 RequestGoogleURLTrackerServerCheckIfNecessary();
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after
2480 if (!IsCreatedByExtension(template_url.get())) { 2463 if (!IsCreatedByExtension(template_url.get())) {
2481 if (keyword_to_template_url.find(template_url->keyword()) != 2464 if (keyword_to_template_url.find(template_url->keyword()) !=
2482 keyword_to_template_url.end()) { 2465 keyword_to_template_url.end()) {
2483 return true; 2466 return true;
2484 } 2467 }
2485 keyword_to_template_url[template_url->keyword()] = template_url.get(); 2468 keyword_to_template_url[template_url->keyword()] = template_url.get();
2486 } 2469 }
2487 } 2470 }
2488 return false; 2471 return false;
2489 } 2472 }
OLDNEW
« no previous file with comments | « components/search_engines/template_url_service.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698