| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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_dom_ui.h" | 5 #include "chrome/browser/extensions/extension_dom_ui.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "chrome/browser/browser.h" | 8 #include "chrome/browser/browser.h" |
| 9 #include "chrome/browser/browser_list.h" | 9 #include "chrome/browser/browser_list.h" |
| 10 #include "chrome/browser/profile.h" | 10 #include "chrome/browser/profile.h" |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 } | 131 } |
| 132 | 132 |
| 133 *url = extension_url; | 133 *url = extension_url; |
| 134 return true; | 134 return true; |
| 135 } | 135 } |
| 136 return false; | 136 return false; |
| 137 } | 137 } |
| 138 | 138 |
| 139 // static | 139 // static |
| 140 void ExtensionDOMUI::RegisterChromeURLOverrides( | 140 void ExtensionDOMUI::RegisterChromeURLOverrides( |
| 141 Profile* profile, const DictionaryValue* new_overrides) { | 141 Profile* profile, const Extension::URLOverrideMap& overrides) { |
| 142 if (!new_overrides) | 142 if (overrides.empty()) |
| 143 return; | 143 return; |
| 144 | 144 |
| 145 PrefService* prefs = profile->GetPrefs(); | 145 PrefService* prefs = profile->GetPrefs(); |
| 146 DictionaryValue* all_overrides = | 146 DictionaryValue* all_overrides = |
| 147 prefs->GetMutableDictionary(kExtensionURLOverrides); | 147 prefs->GetMutableDictionary(kExtensionURLOverrides); |
| 148 | 148 |
| 149 // For each override provided by the extension, add it to the front of | 149 // For each override provided by the extension, add it to the front of |
| 150 // the override list if it's not already in the list. | 150 // the override list if it's not already in the list. |
| 151 DictionaryValue::key_iterator iter = new_overrides->begin_keys(); | 151 Extension::URLOverrideMap::const_iterator iter = overrides.begin(); |
| 152 for (;iter != new_overrides->end_keys(); ++iter) { | 152 for (;iter != overrides.end(); ++iter) { |
| 153 Value* val; | 153 const std::wstring key = UTF8ToWide((*iter).first); |
| 154 new_overrides->Get(*iter, &val); | |
| 155 std::string string_val; | |
| 156 if (!val->GetAsString(&string_val)) { | |
| 157 NOTREACHED(); | |
| 158 continue; | |
| 159 } | |
| 160 ListValue* page_overrides; | 154 ListValue* page_overrides; |
| 161 if (!all_overrides->GetList(*iter, &page_overrides)) { | 155 if (!all_overrides->GetList(key, &page_overrides)) { |
| 162 page_overrides = new ListValue(); | 156 page_overrides = new ListValue(); |
| 163 all_overrides->Set(*iter, page_overrides); | 157 all_overrides->Set(key, page_overrides); |
| 164 } else { | 158 } else { |
| 165 // Verify that the override isn't already in the list. | 159 // Verify that the override isn't already in the list. |
| 166 ListValue::iterator i = page_overrides->begin(); | 160 ListValue::iterator i = page_overrides->begin(); |
| 167 for (; i != page_overrides->end(); ++i) { | 161 for (; i != page_overrides->end(); ++i) { |
| 168 std::string override_val; | 162 std::string override_val; |
| 169 if (!(*i)->GetAsString(&override_val)) { | 163 if (!(*i)->GetAsString(&override_val)) { |
| 170 NOTREACHED(); | 164 NOTREACHED(); |
| 171 continue; | 165 continue; |
| 172 } | 166 } |
| 173 if (override_val == string_val) | 167 if (override_val == (*iter).first) |
| 174 break; | 168 break; |
| 175 } | 169 } |
| 176 // This value is already in the list, leave it alone. | 170 // This value is already in the list, leave it alone. |
| 177 if (i != page_overrides->end()) | 171 if (i != page_overrides->end()) |
| 178 continue; | 172 continue; |
| 179 } | 173 } |
| 180 // Insert the override at the front of the list. Last registered override | 174 // Insert the override at the front of the list. Last registered override |
| 181 // wins. | 175 // wins. |
| 182 page_overrides->Insert(0, val->DeepCopy()); | 176 page_overrides->Insert(0, new StringValue((*iter).second.spec())); |
| 183 } | 177 } |
| 184 } | 178 } |
| 185 | 179 |
| 186 // static | 180 // static |
| 187 void ExtensionDOMUI::UnregisterAndReplaceOverride(const std::string& page, | 181 void ExtensionDOMUI::UnregisterAndReplaceOverride(const std::string& page, |
| 188 Profile* profile, ListValue* list, Value* override) { | 182 Profile* profile, ListValue* list, Value* override) { |
| 189 int index = list->Remove(*override); | 183 int index = list->Remove(*override); |
| 190 if (index == 0) { | 184 if (index == 0) { |
| 191 // This is the active override, so we need to find all existing | 185 // This is the active override, so we need to find all existing |
| 192 // tabs for this override and get them to reload the original URL. | 186 // tabs for this override and get them to reload the original URL. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 219 // If it's being unregistered, it should already be in the list. | 213 // If it's being unregistered, it should already be in the list. |
| 220 NOTREACHED(); | 214 NOTREACHED(); |
| 221 return; | 215 return; |
| 222 } else { | 216 } else { |
| 223 UnregisterAndReplaceOverride(page, profile, page_overrides, override); | 217 UnregisterAndReplaceOverride(page, profile, page_overrides, override); |
| 224 } | 218 } |
| 225 } | 219 } |
| 226 | 220 |
| 227 // static | 221 // static |
| 228 void ExtensionDOMUI::UnregisterChromeURLOverrides( | 222 void ExtensionDOMUI::UnregisterChromeURLOverrides( |
| 229 Profile* profile, const DictionaryValue* new_overrides) { | 223 Profile* profile, const Extension::URLOverrideMap& overrides) { |
| 230 if (!new_overrides) | 224 if (overrides.empty()) |
| 231 return; | 225 return; |
| 232 PrefService* prefs = profile->GetPrefs(); | 226 PrefService* prefs = profile->GetPrefs(); |
| 233 DictionaryValue* all_overrides = | 227 DictionaryValue* all_overrides = |
| 234 prefs->GetMutableDictionary(kExtensionURLOverrides); | 228 prefs->GetMutableDictionary(kExtensionURLOverrides); |
| 235 DictionaryValue::key_iterator iter = new_overrides->begin_keys(); | 229 Extension::URLOverrideMap::const_iterator iter = overrides.begin(); |
| 236 for (; iter != new_overrides->end_keys(); ++iter) { | 230 for (;iter != overrides.end(); ++iter) { |
| 237 Value* val; | 231 std::wstring page = UTF8ToWide((*iter).first); |
| 238 if (!new_overrides->Get(*iter, &val)) { | |
| 239 NOTREACHED(); | |
| 240 return; | |
| 241 } | |
| 242 ListValue* page_overrides; | 232 ListValue* page_overrides; |
| 243 if (!all_overrides->GetList(*iter, &page_overrides)) { | 233 if (!all_overrides->GetList(page, &page_overrides)) { |
| 244 // If it's being unregistered, it should already be in the list. | 234 // If it's being unregistered, it should already be in the list. |
| 245 NOTREACHED(); | 235 NOTREACHED(); |
| 246 continue; | 236 continue; |
| 247 } else { | 237 } else { |
| 248 UnregisterAndReplaceOverride(WideToUTF8(*iter), profile, page_overrides, | 238 StringValue override((*iter).second.spec()); |
| 249 val); | 239 UnregisterAndReplaceOverride((*iter).first, profile, |
| 240 page_overrides, &override); |
| 250 } | 241 } |
| 251 } | 242 } |
| 252 } | 243 } |
| OLD | NEW |