Adding Priority field to cookies.

This CL focuses on cookie parser, CanonicalCookie, and making callers pass PRIORITY_DEFAULT where applicable.

What's NOT included in this CL:
- TODO(rogerm): Persistence in SQL database.
- TODO(huangs): Using cookie priority to affect cookie eviction.
- TODO(huangs): Make priorities available for extension API (right now default value is used to set).
- TODO(lower priority): Cookie viewer update.
- TODO(lower priority): WebCookie update in webkit/glue.

BUG=232693

Review URL: https://codereview.chromium.org/14113014

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@195245 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/net/cookies/parsed_cookie_unittest.cc b/net/cookies/parsed_cookie_unittest.cc
index 57de115..ad4aba6 100644
--- a/net/cookies/parsed_cookie_unittest.cc
+++ b/net/cookies/parsed_cookie_unittest.cc
@@ -4,17 +4,12 @@
 
 #include <string>
 
+#include "net/cookies/cookie_constants.h"
 #include "net/cookies/parsed_cookie.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
 namespace net {
 
-namespace {
-
-class ParsedCookieTest : public testing::Test { };
-
-}  // namespace
-
 TEST(ParsedCookieTest, TestBasic) {
   ParsedCookie pc("a=b");
   EXPECT_TRUE(pc.IsValid());
@@ -74,10 +69,11 @@
   EXPECT_EQ("/", pc.Path());
   EXPECT_EQ("", pc.Name());
   EXPECT_EQ("BLAHHH", pc.Value());
+  EXPECT_EQ(COOKIE_PRIORITY_DEFAULT, pc.Priority());
 }
 
 TEST(ParsedCookieTest, TestAttributeCase) {
-  ParsedCookie pc("BLAHHH; Path=/; sECuRe; httpONLY");
+  ParsedCookie pc("BLAHHH; Path=/; sECuRe; httpONLY; pRIoRitY=hIgH");
   EXPECT_TRUE(pc.IsValid());
   EXPECT_TRUE(pc.IsSecure());
   EXPECT_TRUE(pc.IsHttpOnly());
@@ -85,7 +81,8 @@
   EXPECT_EQ("/", pc.Path());
   EXPECT_EQ("", pc.Name());
   EXPECT_EQ("BLAHHH", pc.Value());
-  EXPECT_EQ(3U, pc.NumberOfAttributes());
+  EXPECT_EQ(COOKIE_PRIORITY_HIGH, pc.Priority());
+  EXPECT_EQ(4U, pc.NumberOfAttributes());
 }
 
 TEST(ParsedCookieTest, TestDoubleQuotedNameless) {
@@ -96,6 +93,7 @@
   EXPECT_EQ("/", pc.Path());
   EXPECT_EQ("", pc.Name());
   EXPECT_EQ("\"BLA\\\"HHH\"", pc.Value());
+  EXPECT_EQ(COOKIE_PRIORITY_DEFAULT, pc.Priority());
   EXPECT_EQ(2U, pc.NumberOfAttributes());
 }
 
@@ -104,6 +102,7 @@
   EXPECT_TRUE(pc.IsValid());
   EXPECT_EQ("a", pc.Name());
   EXPECT_EQ("\"B", pc.Value());
+  EXPECT_EQ(COOKIE_PRIORITY_DEFAULT, pc.Priority());
   EXPECT_EQ(0U, pc.NumberOfAttributes());
 }
 
@@ -112,6 +111,7 @@
   EXPECT_TRUE(pc.IsValid());
   EXPECT_EQ("", pc.Name());
   EXPECT_EQ("ABC", pc.Value());
+  EXPECT_EQ(COOKIE_PRIORITY_DEFAULT, pc.Priority());
   EXPECT_EQ(0U, pc.NumberOfAttributes());
 }
 
@@ -122,6 +122,7 @@
   EXPECT_EQ("", pc.Value());
   EXPECT_TRUE(pc.HasPath());
   EXPECT_EQ("/wee", pc.Path());
+  EXPECT_EQ(COOKIE_PRIORITY_DEFAULT, pc.Priority());
   EXPECT_EQ(1U, pc.NumberOfAttributes());
 }
 
@@ -134,6 +135,7 @@
   EXPECT_FALSE(pc.HasDomain());
   EXPECT_TRUE(pc.IsSecure());
   EXPECT_TRUE(pc.IsHttpOnly());
+  EXPECT_EQ(COOKIE_PRIORITY_DEFAULT, pc.Priority());
   // We parse anything between ; as attributes, so we end up with two
   // attributes with an empty string name and value.
   EXPECT_EQ(4U, pc.NumberOfAttributes());
@@ -147,6 +149,7 @@
   EXPECT_FALSE(pc.HasDomain());
   EXPECT_TRUE(pc.IsSecure());
   EXPECT_TRUE(pc.IsHttpOnly());
+  EXPECT_EQ(COOKIE_PRIORITY_DEFAULT, pc.Priority());
   EXPECT_EQ(4U, pc.NumberOfAttributes());
 }
 
@@ -161,6 +164,7 @@
   EXPECT_TRUE(pc.HasExpires());
   EXPECT_TRUE(pc.HasPath());
   EXPECT_EQ("/", pc.Path());
+  EXPECT_EQ(COOKIE_PRIORITY_DEFAULT, pc.Priority());
   EXPECT_EQ(2U, pc.NumberOfAttributes());
 }
 
@@ -174,6 +178,7 @@
   EXPECT_TRUE(pc.HasExpires());
   EXPECT_TRUE(pc.HasPath());
   EXPECT_EQ("/", pc.Path());
+  EXPECT_EQ(COOKIE_PRIORITY_DEFAULT, pc.Priority());
   EXPECT_EQ(2U, pc.NumberOfAttributes());
 }
 
@@ -249,10 +254,10 @@
 TEST(ParsedCookieTest, SerializeCookieLine) {
   const char input[] = "ANCUUID=zohNumRKgI0oxyhSsV3Z7D  ; "
                        "expires=Sun, 18-Apr-2027 21:06:29 GMT ; "
-                       "path=/  ;  ";
+                       "path=/  ;  priority=low  ;  ";
   const char output[] = "ANCUUID=zohNumRKgI0oxyhSsV3Z7D; "
                         "expires=Sun, 18-Apr-2027 21:06:29 GMT; "
-                        "path=/";
+                        "path=/; priority=low";
   ParsedCookie pc(input);
   EXPECT_EQ(output, pc.ToCookieLine());
 }
@@ -331,9 +336,11 @@
   EXPECT_TRUE(pc.SetMaxAge("12345"));
   EXPECT_TRUE(pc.SetIsSecure(true));
   EXPECT_TRUE(pc.SetIsHttpOnly(true));
+  EXPECT_TRUE(pc.SetIsHttpOnly(true));
+  EXPECT_TRUE(pc.SetPriority("HIGH"));
   EXPECT_EQ("name=value; domain=domain.com; path=/; "
             "expires=Sun, 18-Apr-2027 21:06:29 GMT; max-age=12345; secure; "
-            "httponly",
+            "httponly; priority=HIGH",
             pc.ToCookieLine());
   EXPECT_TRUE(pc.HasDomain());
   EXPECT_TRUE(pc.HasPath());
@@ -341,6 +348,7 @@
   EXPECT_TRUE(pc.HasMaxAge());
   EXPECT_TRUE(pc.IsSecure());
   EXPECT_TRUE(pc.IsHttpOnly());
+  EXPECT_EQ(COOKIE_PRIORITY_HIGH, pc.Priority());
 
   // Clear one attribute from the middle.
   EXPECT_TRUE(pc.SetPath("/foo"));
@@ -351,7 +359,14 @@
   EXPECT_TRUE(pc.IsHttpOnly());
   EXPECT_EQ("name=value; domain=domain.com; path=/foo; "
             "expires=Sun, 18-Apr-2027 21:06:29 GMT; max-age=12345; secure; "
-            "httponly",
+            "httponly; priority=HIGH",
+            pc.ToCookieLine());
+
+  // Set priority to medium.
+  EXPECT_TRUE(pc.SetPriority("medium"));
+  EXPECT_EQ("name=value; domain=domain.com; path=/foo; "
+            "expires=Sun, 18-Apr-2027 21:06:29 GMT; max-age=12345; secure; "
+            "httponly; priority=medium",
             pc.ToCookieLine());
 
   // Clear the rest and change the name and value.
@@ -363,6 +378,7 @@
   EXPECT_TRUE(pc.SetIsHttpOnly(false));
   EXPECT_TRUE(pc.SetName("name2"));
   EXPECT_TRUE(pc.SetValue("value2"));
+  EXPECT_TRUE(pc.SetPriority(std::string()));
   EXPECT_FALSE(pc.HasDomain());
   EXPECT_FALSE(pc.HasPath());
   EXPECT_FALSE(pc.HasExpires());
@@ -372,4 +388,38 @@
   EXPECT_EQ("name2=value2", pc.ToCookieLine());
 }
 
+TEST(ParsedCookieTest, SetPriority) {
+  ParsedCookie pc("name=value");
+  EXPECT_TRUE(pc.IsValid());
+
+  EXPECT_EQ("name=value", pc.ToCookieLine());
+  EXPECT_EQ(COOKIE_PRIORITY_DEFAULT, pc.Priority());
+
+  // Test each priority, expect case-insensitive compare.
+  EXPECT_TRUE(pc.SetPriority("high"));
+  EXPECT_EQ("name=value; priority=high", pc.ToCookieLine());
+  EXPECT_EQ(COOKIE_PRIORITY_HIGH, pc.Priority());
+
+  EXPECT_TRUE(pc.SetPriority("mEDium"));
+  EXPECT_EQ("name=value; priority=mEDium", pc.ToCookieLine());
+  EXPECT_EQ(COOKIE_PRIORITY_MEDIUM, pc.Priority());
+
+  EXPECT_TRUE(pc.SetPriority("LOW"));
+  EXPECT_EQ("name=value; priority=LOW", pc.ToCookieLine());
+  EXPECT_EQ(COOKIE_PRIORITY_LOW, pc.Priority());
+
+  // Interpret invalid priority values as COOKIE_PRIORITY_DEFAULT.
+  EXPECT_TRUE(pc.SetPriority("Blah"));
+  EXPECT_EQ("name=value; priority=Blah", pc.ToCookieLine());
+  EXPECT_EQ(COOKIE_PRIORITY_DEFAULT, pc.Priority());
+
+  EXPECT_TRUE(pc.SetPriority("lowerest"));
+  EXPECT_EQ("name=value; priority=lowerest", pc.ToCookieLine());
+  EXPECT_EQ(COOKIE_PRIORITY_DEFAULT, pc.Priority());
+
+  EXPECT_TRUE(pc.SetPriority(""));
+  EXPECT_EQ("name=value", pc.ToCookieLine());
+  EXPECT_EQ(COOKIE_PRIORITY_DEFAULT, pc.Priority());
+}
+
 }  // namespace net