summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--chrome/browser/dom_ui/app_launcher_handler.cc8
-rw-r--r--chrome/browser/extensions/extension_prefs.cc36
-rw-r--r--chrome/browser/extensions/extension_prefs.h13
-rw-r--r--chrome/browser/extensions/extension_prefs_unittest.cc33
-rw-r--r--chrome/browser/resources/ntp/apps.js4
5 files changed, 94 insertions, 0 deletions
diff --git a/chrome/browser/dom_ui/app_launcher_handler.cc b/chrome/browser/dom_ui/app_launcher_handler.cc
index e834754..674d951 100644
--- a/chrome/browser/dom_ui/app_launcher_handler.cc
+++ b/chrome/browser/dom_ui/app_launcher_handler.cc
@@ -121,6 +121,14 @@ void AppLauncherHandler::CreateAppInfo(Extension* extension,
value->SetInteger("launch_container", extension->launch_container());
value->SetInteger("launch_type",
extension_prefs->GetLaunchType(extension->id()));
+
+ int app_launch_index = extension_prefs->GetAppLaunchIndex(extension->id());
+ if (app_launch_index == -1) {
+ // Make sure every app has a launch index (some predate the launch index).
+ app_launch_index = extension_prefs->GetNextAppLaunchIndex();
+ extension_prefs->SetAppLaunchIndex(extension->id(), app_launch_index);
+ }
+ value->SetInteger("app_launch_index", app_launch_index);
}
void AppLauncherHandler::FillAppDictionary(DictionaryValue* dictionary) {
diff --git a/chrome/browser/extensions/extension_prefs.cc b/chrome/browser/extensions/extension_prefs.cc
index 9c7a1cf..2381c01 100644
--- a/chrome/browser/extensions/extension_prefs.cc
+++ b/chrome/browser/extensions/extension_prefs.cc
@@ -79,6 +79,9 @@ const char kWebStoreLogin[] = "extensions.webstore_login";
// used for apps.
const char kPrefLaunchType[] = "launchType";
+// A preference determining the order of which the apps appear on the NTP.
+const char kPrefAppLaunchIndex[] = "app_launcher_index";
+
} // namespace
////////////////////////////////////////////////////////////////////////////////
@@ -538,6 +541,8 @@ void ExtensionPrefs::OnExtensionInstalled(
UpdateExtensionPref(id, kPrefManifest,
extension->manifest_value()->DeepCopy());
}
+ UpdateExtensionPref(id, kPrefAppLaunchIndex,
+ Value::CreateIntegerValue(GetNextAppLaunchIndex()));
SavePrefsAndNotify();
}
@@ -864,6 +869,37 @@ void ExtensionPrefs::SetWebStoreLogin(const std::string& login) {
SavePrefsAndNotify();
}
+int ExtensionPrefs::GetAppLaunchIndex(const std::string& extension_id) {
+ int value;
+ if (ReadExtensionPrefInteger(extension_id, kPrefAppLaunchIndex, &value))
+ return value;
+
+ return -1;
+}
+
+void ExtensionPrefs::SetAppLaunchIndex(const std::string& extension_id,
+ int index) {
+ DCHECK_GE(index, 0);
+ UpdateExtensionPref(extension_id, kPrefAppLaunchIndex,
+ Value::CreateIntegerValue(index));
+ SavePrefsAndNotify();
+}
+
+int ExtensionPrefs::GetNextAppLaunchIndex() {
+ const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
+ if (!extensions)
+ return 0;
+
+ int max_value = -1;
+ for (DictionaryValue::key_iterator extension_id = extensions->begin_keys();
+ extension_id != extensions->end_keys(); ++extension_id) {
+ int value = GetAppLaunchIndex(*extension_id);
+ if (value > max_value)
+ max_value = value;
+ }
+ return max_value + 1;
+}
+
// static
void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) {
prefs->RegisterDictionaryPref(kExtensionsPref);
diff --git a/chrome/browser/extensions/extension_prefs.h b/chrome/browser/extensions/extension_prefs.h
index 7bfe9eb..97547ea 100644
--- a/chrome/browser/extensions/extension_prefs.h
+++ b/chrome/browser/extensions/extension_prefs.h
@@ -174,6 +174,19 @@ class ExtensionPrefs {
bool GetWebStoreLogin(std::string* result);
void SetWebStoreLogin(const std::string& login);
+ // Get the application launch index for an extension with |extension_id|. This
+ // determines the order of which the applications appear on the New Tab Page.
+ // A value of 0 generally indicates top left. If the extension has no launch
+ // index a -1 value is returned.
+ int GetAppLaunchIndex(const std::string& extension_id);
+
+ // Sets a specific launch index for an extension with |extension_id|.
+ void SetAppLaunchIndex(const std::string& extension_id, int index);
+
+ // Gets the next available application launch index. This is 1 higher than the
+ // highest current application launch index found.
+ int GetNextAppLaunchIndex();
+
static void RegisterUserPrefs(PrefService* prefs);
// The underlying PrefService.
diff --git a/chrome/browser/extensions/extension_prefs_unittest.cc b/chrome/browser/extensions/extension_prefs_unittest.cc
index 11194e7..c9f9336 100644
--- a/chrome/browser/extensions/extension_prefs_unittest.cc
+++ b/chrome/browser/extensions/extension_prefs_unittest.cc
@@ -334,3 +334,36 @@ class ExtensionPrefsOnExtensionInstalled : public ExtensionPrefsTest {
};
TEST_F(ExtensionPrefsOnExtensionInstalled,
ExtensionPrefsOnExtensionInstalled) {}
+
+class ExtensionPrefsAppLaunchIndex : public ExtensionPrefsTest {
+public:
+ virtual void Initialize() {
+ // No extensions yet.
+ EXPECT_EQ(0, prefs()->GetNextAppLaunchIndex());
+
+ extension_.reset(prefs_.AddExtension("on_extension_installed"));
+ EXPECT_EQ(Extension::ENABLED,
+ prefs()->GetExtensionState(extension_->id()));
+ prefs()->OnExtensionInstalled(extension_.get(),
+ Extension::ENABLED, false);
+ }
+
+ virtual void Verify() {
+ int launch_index = prefs()->GetAppLaunchIndex(extension_->id());
+ // Extension should have been assigned a launch index > 0.
+ EXPECT_GT(launch_index, 0);
+ EXPECT_EQ(launch_index + 1, prefs()->GetNextAppLaunchIndex());
+ // Set a new launch index of one higher and verify.
+ prefs()->SetAppLaunchIndex(extension_->id(),
+ prefs()->GetNextAppLaunchIndex());
+ int new_launch_index = prefs()->GetAppLaunchIndex(extension_->id());
+ EXPECT_EQ(launch_index + 1, new_launch_index);
+
+ // This extension doesn't exist, so it should return -1.
+ EXPECT_EQ(-1, prefs()->GetAppLaunchIndex("foo"));
+ }
+
+private:
+ scoped_ptr<Extension> extension_;
+};
+TEST_F(ExtensionPrefsAppLaunchIndex, ExtensionPrefsAppLaunchIndex) {}
diff --git a/chrome/browser/resources/ntp/apps.js b/chrome/browser/resources/ntp/apps.js
index e6f79a2..da91af7 100644
--- a/chrome/browser/resources/ntp/apps.js
+++ b/chrome/browser/resources/ntp/apps.js
@@ -14,6 +14,10 @@ function getAppsCallback(data) {
appsSectionContent.removeChild(appsSectionContent.lastChild);
}
+ data.apps.sort(function(a,b) {
+ return a.app_launch_index - b.app_launch_index
+ });
+
clearClosedMenu(apps.menu);
if (data.apps.length == 0) {
appsSection.classList.add('disabled');