OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "chrome/browser/extensions/extension_web_ui.h" | 5 #include "chrome/browser/extensions/extension_web_ui.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <set> | 9 #include <set> |
10 #include <utility> | 10 #include <utility> |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 // Iterates over |list| and: | 60 // Iterates over |list| and: |
61 // - Converts any entries of the form <entry> to | 61 // - Converts any entries of the form <entry> to |
62 // { 'entry': <entry>, 'active': true }. | 62 // { 'entry': <entry>, 'active': true }. |
63 // - Removes any duplicate entries. | 63 // - Removes any duplicate entries. |
64 // We do the conversion because we previously stored these values as strings | 64 // We do the conversion because we previously stored these values as strings |
65 // rather than objects. | 65 // rather than objects. |
66 // TODO(devlin): Remove the conversion once everyone's updated. | 66 // TODO(devlin): Remove the conversion once everyone's updated. |
67 void InitializeOverridesList(base::ListValue* list) { | 67 void InitializeOverridesList(base::ListValue* list) { |
68 base::ListValue migrated; | 68 base::ListValue migrated; |
69 std::set<std::string> seen_entries; | 69 std::set<std::string> seen_entries; |
70 for (auto& val : *list) { | 70 for (const auto& val : *list) { |
71 std::unique_ptr<base::DictionaryValue> new_dict( | 71 std::unique_ptr<base::DictionaryValue> new_dict( |
72 new base::DictionaryValue()); | 72 new base::DictionaryValue()); |
73 std::string entry_name; | 73 std::string entry_name; |
74 base::DictionaryValue* existing_dict = nullptr; | 74 base::DictionaryValue* existing_dict = nullptr; |
75 if (val.GetAsDictionary(&existing_dict)) { | 75 if (val->GetAsDictionary(&existing_dict)) { |
76 bool success = existing_dict->GetString(kEntry, &entry_name); | 76 bool success = existing_dict->GetString(kEntry, &entry_name); |
77 if (!success) // See comment about CHECK(success) in ForEachOverrideList. | 77 if (!success) // See comment about CHECK(success) in ForEachOverrideList. |
78 continue; | 78 continue; |
79 new_dict->Swap(existing_dict); | 79 new_dict->Swap(existing_dict); |
80 } else if (val.GetAsString(&entry_name)) { | 80 } else if (val->GetAsString(&entry_name)) { |
81 new_dict->SetString(kEntry, entry_name); | 81 new_dict->SetString(kEntry, entry_name); |
82 new_dict->SetBoolean(kActive, true); | 82 new_dict->SetBoolean(kActive, true); |
83 } else { | 83 } else { |
84 NOTREACHED(); | 84 NOTREACHED(); |
85 continue; | 85 continue; |
86 } | 86 } |
87 | 87 |
88 if (seen_entries.count(entry_name) == 0) { | 88 if (seen_entries.count(entry_name) == 0) { |
89 seen_entries.insert(entry_name); | 89 seen_entries.insert(entry_name); |
90 migrated.Append(std::move(new_dict)); | 90 migrated.Append(std::move(new_dict)); |
91 } | 91 } |
92 } | 92 } |
93 | 93 |
94 list->Swap(&migrated); | 94 list->Swap(&migrated); |
95 } | 95 } |
96 | 96 |
97 // Adds |override| to |list|, or, if there's already an entry for the override, | 97 // Adds |override| to |list|, or, if there's already an entry for the override, |
98 // marks it as active. | 98 // marks it as active. |
99 void AddOverridesToList(base::ListValue* list, | 99 void AddOverridesToList(base::ListValue* list, |
100 const std::string& override) { | 100 const std::string& override) { |
101 for (auto& val : *list) { | 101 for (const auto& val : *list) { |
102 base::DictionaryValue* dict = nullptr; | 102 base::DictionaryValue* dict = nullptr; |
103 std::string entry; | 103 std::string entry; |
104 if (!val.GetAsDictionary(&dict) || !dict->GetString(kEntry, &entry)) { | 104 if (!val->GetAsDictionary(&dict) || !dict->GetString(kEntry, &entry)) { |
105 NOTREACHED(); | 105 NOTREACHED(); |
106 continue; | 106 continue; |
107 } | 107 } |
108 if (entry == override) { | 108 if (entry == override) { |
109 dict->SetBoolean(kActive, true); | 109 dict->SetBoolean(kActive, true); |
110 return; // All done! | 110 return; // All done! |
111 } | 111 } |
112 } | 112 } |
113 | 113 |
114 auto dict = base::MakeUnique<base::DictionaryValue>(); | 114 auto dict = base::MakeUnique<base::DictionaryValue>(); |
115 dict->SetString(kEntry, override); | 115 dict->SetString(kEntry, override); |
116 dict->SetBoolean(kActive, true); | 116 dict->SetBoolean(kActive, true); |
117 // Add the entry to the front of the list. | 117 // Add the entry to the front of the list. |
118 list->Insert(0, std::move(dict)); | 118 list->Insert(0, std::move(dict)); |
119 } | 119 } |
120 | 120 |
121 // Validates that each entry in |list| contains a valid url and points to an | 121 // Validates that each entry in |list| contains a valid url and points to an |
122 // extension contained in |all_extensions| (and, if not, removes it). | 122 // extension contained in |all_extensions| (and, if not, removes it). |
123 void ValidateOverridesList(const extensions::ExtensionSet* all_extensions, | 123 void ValidateOverridesList(const extensions::ExtensionSet* all_extensions, |
124 base::ListValue* list) { | 124 base::ListValue* list) { |
125 base::ListValue migrated; | 125 base::ListValue migrated; |
126 for (auto& val : *list) { | 126 for (const auto& val : *list) { |
127 base::DictionaryValue* dict = nullptr; | 127 base::DictionaryValue* dict = nullptr; |
128 std::string entry; | 128 std::string entry; |
129 if (!val.GetAsDictionary(&dict) || !dict->GetString(kEntry, &entry)) { | 129 if (!val->GetAsDictionary(&dict) || !dict->GetString(kEntry, &entry)) { |
130 NOTREACHED(); | 130 NOTREACHED(); |
131 continue; | 131 continue; |
132 } | 132 } |
133 std::unique_ptr<base::DictionaryValue> new_dict( | 133 std::unique_ptr<base::DictionaryValue> new_dict( |
134 new base::DictionaryValue()); | 134 new base::DictionaryValue()); |
135 new_dict->Swap(dict); | 135 new_dict->Swap(dict); |
136 GURL override_url(entry); | 136 GURL override_url(entry); |
137 if (!override_url.is_valid()) | 137 if (!override_url.is_valid()) |
138 continue; | 138 continue; |
139 | 139 |
(...skipping 30 matching lines...) Expand all Loading... |
170 enum UpdateBehavior { | 170 enum UpdateBehavior { |
171 UPDATE_DEACTIVATE, // Mark 'active' as false. | 171 UPDATE_DEACTIVATE, // Mark 'active' as false. |
172 UPDATE_REMOVE, // Remove the entry from the list. | 172 UPDATE_REMOVE, // Remove the entry from the list. |
173 }; | 173 }; |
174 | 174 |
175 // Updates the entry (if any) for |override_url| in |overrides_list| according | 175 // Updates the entry (if any) for |override_url| in |overrides_list| according |
176 // to |behavior|. Returns true if anything changed. | 176 // to |behavior|. Returns true if anything changed. |
177 bool UpdateOverridesList(base::ListValue* overrides_list, | 177 bool UpdateOverridesList(base::ListValue* overrides_list, |
178 const std::string& override_url, | 178 const std::string& override_url, |
179 UpdateBehavior behavior) { | 179 UpdateBehavior behavior) { |
180 base::ListValue::iterator iter = std::find_if( | 180 base::ListValue::iterator iter = |
181 overrides_list->begin(), overrides_list->end(), | 181 std::find_if(overrides_list->begin(), overrides_list->end(), |
182 [&override_url](const base::Value& value) { | 182 [&override_url](const std::unique_ptr<base::Value>& value) { |
183 std::string entry; | 183 std::string entry; |
184 const base::DictionaryValue* dict = nullptr; | 184 const base::DictionaryValue* dict = nullptr; |
185 return value.GetAsDictionary(&dict) && | 185 return value->GetAsDictionary(&dict) && |
186 dict->GetString(kEntry, &entry) && entry == override_url; | 186 dict->GetString(kEntry, &entry) && |
187 }); | 187 entry == override_url; |
| 188 }); |
188 if (iter != overrides_list->end()) { | 189 if (iter != overrides_list->end()) { |
189 switch (behavior) { | 190 switch (behavior) { |
190 case UPDATE_DEACTIVATE: { | 191 case UPDATE_DEACTIVATE: { |
191 base::DictionaryValue* dict = nullptr; | 192 base::DictionaryValue* dict = nullptr; |
192 bool success = iter->GetAsDictionary(&dict); | 193 bool success = (*iter)->GetAsDictionary(&dict); |
193 // See comment about CHECK(success) in ForEachOverrideList. | 194 // See comment about CHECK(success) in ForEachOverrideList. |
194 if (success) { | 195 if (success) { |
195 dict->SetBoolean(kActive, false); | 196 dict->SetBoolean(kActive, false); |
196 break; | 197 break; |
197 } | 198 } |
198 // Else fall through and erase the broken pref. | 199 // Else fall through and erase the broken pref. |
199 } | 200 } |
200 case UPDATE_REMOVE: | 201 case UPDATE_REMOVE: |
201 overrides_list->Erase(iter, nullptr); | 202 overrides_list->Erase(iter, nullptr); |
202 break; | 203 break; |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 // chrome://bookmarks/#1 for display in the omnibox. | 421 // chrome://bookmarks/#1 for display in the omnibox. |
421 for (base::DictionaryValue::Iterator dict_iter(*overrides); | 422 for (base::DictionaryValue::Iterator dict_iter(*overrides); |
422 !dict_iter.IsAtEnd(); dict_iter.Advance()) { | 423 !dict_iter.IsAtEnd(); dict_iter.Advance()) { |
423 const base::ListValue* url_list = nullptr; | 424 const base::ListValue* url_list = nullptr; |
424 if (!dict_iter.value().GetAsList(&url_list)) | 425 if (!dict_iter.value().GetAsList(&url_list)) |
425 continue; | 426 continue; |
426 | 427 |
427 for (base::ListValue::const_iterator list_iter = url_list->begin(); | 428 for (base::ListValue::const_iterator list_iter = url_list->begin(); |
428 list_iter != url_list->end(); ++list_iter) { | 429 list_iter != url_list->end(); ++list_iter) { |
429 const base::DictionaryValue* dict = nullptr; | 430 const base::DictionaryValue* dict = nullptr; |
430 if (!list_iter->GetAsDictionary(&dict)) | 431 if (!(*list_iter)->GetAsDictionary(&dict)) |
431 continue; | 432 continue; |
432 std::string override; | 433 std::string override; |
433 if (!dict->GetString(kEntry, &override)) | 434 if (!dict->GetString(kEntry, &override)) |
434 continue; | 435 continue; |
435 if (base::StartsWith(url->spec(), override, | 436 if (base::StartsWith(url->spec(), override, |
436 base::CompareCase::SENSITIVE)) { | 437 base::CompareCase::SENSITIVE)) { |
437 GURL original_url(content::kChromeUIScheme + std::string("://") + | 438 GURL original_url(content::kChromeUIScheme + std::string("://") + |
438 dict_iter.key() + | 439 dict_iter.key() + |
439 url->spec().substr(override.length())); | 440 url->spec().substr(override.length())); |
440 *url = original_url; | 441 *url = original_url; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 extensions::ImageLoader::ImageRepresentation::ALWAYS_RESIZE, | 529 extensions::ImageLoader::ImageRepresentation::ALWAYS_RESIZE, |
529 gfx::Size(pixel_size, pixel_size), | 530 gfx::Size(pixel_size, pixel_size), |
530 resource_scale_factor)); | 531 resource_scale_factor)); |
531 } | 532 } |
532 | 533 |
533 // LoadImagesAsync actually can run callback synchronously. We want to force | 534 // LoadImagesAsync actually can run callback synchronously. We want to force |
534 // async. | 535 // async. |
535 extensions::ImageLoader::Get(profile)->LoadImagesAsync( | 536 extensions::ImageLoader::Get(profile)->LoadImagesAsync( |
536 extension, info_list, base::Bind(&RunFaviconCallbackAsync, callback)); | 537 extension, info_list, base::Bind(&RunFaviconCallbackAsync, callback)); |
537 } | 538 } |
OLD | NEW |