| 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_sorting.h" | 5 #include "chrome/browser/extensions/extension_sorting.h" |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/extension_scoped_prefs.h" | 7 #include "chrome/browser/extensions/extension_scoped_prefs.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/common/chrome_notification_types.h" | 9 #include "chrome/common/chrome_notification_types.h" |
| 10 #include "content/public/browser/notification_service.h" | 10 #include "content/public/browser/notification_service.h" |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 } | 183 } |
| 184 } | 184 } |
| 185 } | 185 } |
| 186 | 186 |
| 187 content::NotificationService::current()->Notify( | 187 content::NotificationService::current()->Notify( |
| 188 chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, | 188 chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, |
| 189 content::Source<ExtensionSorting>(this), | 189 content::Source<ExtensionSorting>(this), |
| 190 content::NotificationService::NoDetails()); | 190 content::NotificationService::NoDetails()); |
| 191 } | 191 } |
| 192 | 192 |
| 193 void ExtensionSorting::EnsureValidOrdinals(const std::string& extension_id) { |
| 194 StringOrdinal page_ordinal = GetPageOrdinal(extension_id); |
| 195 if (!page_ordinal.IsValid()) { |
| 196 // The webstore app should always start be on the first page. |
| 197 page_ordinal = extension_id == extension_misc::kWebStoreAppId ? |
| 198 CreateFirstAppPageOrdinal() : |
| 199 GetNaturalAppPageOrdinal(); |
| 200 SetPageOrdinal(extension_id, page_ordinal); |
| 201 } |
| 202 |
| 203 StringOrdinal app_launch_ordinal = GetAppLaunchOrdinal(extension_id); |
| 204 if (!app_launch_ordinal.IsValid()) { |
| 205 // The webstore app should always start in the position. |
| 206 app_launch_ordinal = extension_id == extension_misc::kWebStoreAppId ? |
| 207 CreateFirstAppLaunchOrdinal(page_ordinal) : |
| 208 CreateNextAppLaunchOrdinal(page_ordinal); |
| 209 SetAppLaunchOrdinal(extension_id, |
| 210 app_launch_ordinal); |
| 211 } |
| 212 } |
| 213 |
| 193 void ExtensionSorting::OnExtensionMoved( | 214 void ExtensionSorting::OnExtensionMoved( |
| 194 const std::string& moved_extension_id, | 215 const std::string& moved_extension_id, |
| 195 const std::string& predecessor_extension_id, | 216 const std::string& predecessor_extension_id, |
| 196 const std::string& successor_extension_id) { | 217 const std::string& successor_extension_id) { |
| 197 // We only need to change the StringOrdinal if there are neighbours. | 218 // We only need to change the StringOrdinal if there are neighbours. |
| 198 if (!predecessor_extension_id.empty() || !successor_extension_id.empty()) { | 219 if (!predecessor_extension_id.empty() || !successor_extension_id.empty()) { |
| 199 if (predecessor_extension_id.empty()) { | 220 if (predecessor_extension_id.empty()) { |
| 200 // Only a successor. | 221 // Only a successor. |
| 201 SetAppLaunchOrdinal( | 222 SetAppLaunchOrdinal( |
| 202 moved_extension_id, | 223 moved_extension_id, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 } | 257 } |
| 237 | 258 |
| 238 void ExtensionSorting::SetAppLaunchOrdinal( | 259 void ExtensionSorting::SetAppLaunchOrdinal( |
| 239 const std::string& extension_id, | 260 const std::string& extension_id, |
| 240 const StringOrdinal& new_app_launch_ordinal) { | 261 const StringOrdinal& new_app_launch_ordinal) { |
| 241 StringOrdinal page_ordinal = GetPageOrdinal(extension_id); | 262 StringOrdinal page_ordinal = GetPageOrdinal(extension_id); |
| 242 RemoveOrdinalMapping( | 263 RemoveOrdinalMapping( |
| 243 extension_id, page_ordinal, GetAppLaunchOrdinal(extension_id)); | 264 extension_id, page_ordinal, GetAppLaunchOrdinal(extension_id)); |
| 244 AddOrdinalMapping(extension_id, page_ordinal, new_app_launch_ordinal); | 265 AddOrdinalMapping(extension_id, page_ordinal, new_app_launch_ordinal); |
| 245 | 266 |
| 267 Value* new_value = new_app_launch_ordinal.IsValid() ? |
| 268 Value::CreateStringValue(new_app_launch_ordinal.ToString()) : |
| 269 NULL; |
| 270 |
| 246 extension_scoped_prefs_->UpdateExtensionPref( | 271 extension_scoped_prefs_->UpdateExtensionPref( |
| 247 extension_id, | 272 extension_id, |
| 248 kPrefAppLaunchOrdinal, | 273 kPrefAppLaunchOrdinal, |
| 249 Value::CreateStringValue(new_app_launch_ordinal.ToString())); | 274 new_value); |
| 250 } | 275 } |
| 251 | 276 |
| 252 StringOrdinal ExtensionSorting::CreateFirstAppLaunchOrdinal( | 277 StringOrdinal ExtensionSorting::CreateFirstAppLaunchOrdinal( |
| 253 const StringOrdinal& page_ordinal) const { | 278 const StringOrdinal& page_ordinal) const { |
| 254 const StringOrdinal& min_ordinal = | 279 const StringOrdinal& min_ordinal = |
| 255 GetMinOrMaxAppLaunchOrdinalsOnPage(page_ordinal, | 280 GetMinOrMaxAppLaunchOrdinalsOnPage(page_ordinal, |
| 256 ExtensionSorting::MIN_ORDINAL); | 281 ExtensionSorting::MIN_ORDINAL); |
| 257 | 282 |
| 258 if (min_ordinal.IsValid()) | 283 if (min_ordinal.IsValid()) |
| 259 return min_ordinal.CreateBefore(); | 284 return min_ordinal.CreateBefore(); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 return StringOrdinal(raw_data); | 338 return StringOrdinal(raw_data); |
| 314 } | 339 } |
| 315 | 340 |
| 316 void ExtensionSorting::SetPageOrdinal(const std::string& extension_id, | 341 void ExtensionSorting::SetPageOrdinal(const std::string& extension_id, |
| 317 const StringOrdinal& new_page_ordinal) { | 342 const StringOrdinal& new_page_ordinal) { |
| 318 StringOrdinal app_launch_ordinal = GetAppLaunchOrdinal(extension_id); | 343 StringOrdinal app_launch_ordinal = GetAppLaunchOrdinal(extension_id); |
| 319 RemoveOrdinalMapping( | 344 RemoveOrdinalMapping( |
| 320 extension_id, GetPageOrdinal(extension_id), app_launch_ordinal); | 345 extension_id, GetPageOrdinal(extension_id), app_launch_ordinal); |
| 321 AddOrdinalMapping(extension_id, new_page_ordinal, app_launch_ordinal); | 346 AddOrdinalMapping(extension_id, new_page_ordinal, app_launch_ordinal); |
| 322 | 347 |
| 348 Value* new_value = new_page_ordinal.IsValid() ? |
| 349 Value::CreateStringValue(new_page_ordinal.ToString()) : |
| 350 NULL; |
| 351 |
| 323 extension_scoped_prefs_->UpdateExtensionPref( | 352 extension_scoped_prefs_->UpdateExtensionPref( |
| 324 extension_id, | 353 extension_id, |
| 325 kPrefPageOrdinal, | 354 kPrefPageOrdinal, |
| 326 Value::CreateStringValue(new_page_ordinal.ToString())); | 355 new_value); |
| 327 } | 356 } |
| 328 | 357 |
| 329 void ExtensionSorting::ClearOrdinals(const std::string& extension_id) { | 358 void ExtensionSorting::ClearOrdinals(const std::string& extension_id) { |
| 330 RemoveOrdinalMapping(extension_id, | 359 RemoveOrdinalMapping(extension_id, |
| 331 GetPageOrdinal(extension_id), | 360 GetPageOrdinal(extension_id), |
| 332 GetAppLaunchOrdinal(extension_id)); | 361 GetAppLaunchOrdinal(extension_id)); |
| 333 | 362 |
| 334 extension_scoped_prefs_->UpdateExtensionPref( | 363 extension_scoped_prefs_->UpdateExtensionPref( |
| 335 extension_id, kPrefPageOrdinal, NULL); | 364 extension_id, kPrefPageOrdinal, NULL); |
| 336 extension_scoped_prefs_->UpdateExtensionPref( | 365 extension_scoped_prefs_->UpdateExtensionPref( |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 | 477 |
| 449 for (AppLaunchOrdinalMap::iterator it = | 478 for (AppLaunchOrdinalMap::iterator it = |
| 450 page_map->second.find(app_launch_ordinal); | 479 page_map->second.find(app_launch_ordinal); |
| 451 it != page_map->second.end(); ++it) { | 480 it != page_map->second.end(); ++it) { |
| 452 if (it->second == extension_id) { | 481 if (it->second == extension_id) { |
| 453 page_map->second.erase(it); | 482 page_map->second.erase(it); |
| 454 break; | 483 break; |
| 455 } | 484 } |
| 456 } | 485 } |
| 457 } | 486 } |
| OLD | NEW |