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

Side by Side Diff: ios/chrome/browser/search_engines/search_engines_util.cc

Issue 2307663002: Remove ScopedVector from search_engines. (Closed)
Patch Set: devlin Created 4 years, 3 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/util.cc ('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 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 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 "ios/chrome/browser/search_engines/search_engines_util.h" 5 #include "ios/chrome/browser/search_engines/search_engines_util.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 10 matching lines...) Expand all
21 // Id of the google id in template_url_prepopulate_data.cc. 21 // Id of the google id in template_url_prepopulate_data.cc.
22 static const int kGoogleEnginePrepopulatedId = 1; 22 static const int kGoogleEnginePrepopulatedId = 1;
23 23
24 // Update the search engine of the given service to the default ones for the 24 // Update the search engine of the given service to the default ones for the
25 // current locale. 25 // current locale.
26 void UpdateSearchEngine(TemplateURLService* service) { 26 void UpdateSearchEngine(TemplateURLService* service) {
27 DCHECK(service); 27 DCHECK(service);
28 DCHECK(service->loaded()); 28 DCHECK(service->loaded());
29 std::vector<TemplateURL*> old_engines = service->GetTemplateURLs(); 29 std::vector<TemplateURL*> old_engines = service->GetTemplateURLs();
30 size_t default_engine; 30 size_t default_engine;
31 ScopedVector<TemplateURLData> new_engines = 31 std::vector<std::unique_ptr<TemplateURLData>> new_engines =
32 TemplateURLPrepopulateData::GetPrepopulatedEngines(nullptr, 32 TemplateURLPrepopulateData::GetPrepopulatedEngines(nullptr,
33 &default_engine); 33 &default_engine);
34 DCHECK(default_engine == 0); 34 DCHECK(default_engine == 0);
35 DCHECK(new_engines[0]->prepopulate_id == kGoogleEnginePrepopulatedId); 35 DCHECK(new_engines[0]->prepopulate_id == kGoogleEnginePrepopulatedId);
36 // The aim is to replace the old search engines with the new ones. 36 // The aim is to replace the old search engines with the new ones.
37 // It is not possible to remove all of them, because removing the current 37 // It is not possible to remove all of them, because removing the current
38 // selected engine is not allowed. 38 // selected engine is not allowed.
39 // It is not possible to add all the new ones first, because the service gets 39 // It is not possible to add all the new ones first, because the service gets
40 // confused when a prepopulated engine is there more than once. 40 // confused when a prepopulated engine is there more than once.
41 // Instead, this will in a first pass makes google as the default engine. In 41 // Instead, this will in a first pass makes google as the default engine. In
42 // a second pass, it will remove all other search engine. At last, in a third 42 // a second pass, it will remove all other search engines. At last, in a third
43 // pass, it will add all new engine but google. 43 // pass, it will add all new engines but google.
44 for (auto* engine : old_engines) { 44 for (auto* engine : old_engines) {
45 if (engine->prepopulate_id() == kGoogleEnginePrepopulatedId) 45 if (engine->prepopulate_id() == kGoogleEnginePrepopulatedId)
46 service->SetUserSelectedDefaultSearchProvider(engine); 46 service->SetUserSelectedDefaultSearchProvider(engine);
47 } 47 }
48 for (auto* engine : old_engines) { 48 for (auto* engine : old_engines) {
49 if (engine->prepopulate_id() != kGoogleEnginePrepopulatedId) 49 if (engine->prepopulate_id() != kGoogleEnginePrepopulatedId)
50 service->Remove(engine); 50 service->Remove(engine);
51 } 51 }
52 ScopedVector<TemplateURLData>::iterator it = new_engines.begin(); 52 for (const auto& engine : new_engines) {
53 while (it != new_engines.end()) { 53 if (engine->prepopulate_id != kGoogleEnginePrepopulatedId)
54 if ((*it)->prepopulate_id != kGoogleEnginePrepopulatedId) { 54 service->Add(base::MakeUnique<TemplateURL>(*engine));
55 // service->Add takes ownership on Added TemplateURL.
56 service->Add(base::MakeUnique<TemplateURL>(**it));
57 it = new_engines.weak_erase(it);
droger 2016/09/02 08:34:26 If I understand correctly this was a memory leak.
Avi (use Gerrit) 2016/09/02 14:39:38 Yes, this was a memory leak; we were constructing
58 } else {
59 ++it;
60 }
61 } 55 }
62 } 56 }
63 57
64 // Observer class that allows to wait for the TemplateURLService to be loaded. 58 // Observer class that allows to wait for the TemplateURLService to be loaded.
65 // This class will delete itself as soon as the TemplateURLService is loaded. 59 // This class will delete itself as soon as the TemplateURLService is loaded.
66 class LoadedObserver : public TemplateURLServiceObserver { 60 class LoadedObserver : public TemplateURLServiceObserver {
67 public: 61 public:
68 explicit LoadedObserver(TemplateURLService* service) : service_(service) { 62 explicit LoadedObserver(TemplateURLService* service) : service_(service) {
69 DCHECK(service_); 63 DCHECK(service_);
70 DCHECK(!service_->loaded()); 64 DCHECK(!service_->loaded());
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 // removed then the engines can be updated again. 103 // removed then the engines can be updated again.
110 if (!service || service->is_default_search_managed()) 104 if (!service || service->is_default_search_managed())
111 return; 105 return;
112 if (service->loaded()) 106 if (service->loaded())
113 UpdateSearchEngine(service); 107 UpdateSearchEngine(service);
114 else 108 else
115 new LoadedObserver(service); // The observer manages its own lifetime. 109 new LoadedObserver(service); // The observer manages its own lifetime.
116 } 110 }
117 111
118 } // namespace search_engines 112 } // namespace search_engines
OLDNEW
« no previous file with comments | « components/search_engines/util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698