1// Copyright (c) 2010 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5// Defines common functionality used by the implementation of the Chrome 6// Extensions Cookies API implemented in 7// chrome/browser/extensions/extension_cookies_api.cc. This separate interface 8// exposes pieces of the API implementation mainly for unit testing purposes. 9 10#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_COOKIES_HELPERS_H_ 11#define CHROME_BROWSER_EXTENSIONS_EXTENSION_COOKIES_HELPERS_H_ 12#pragma once 13 14#include <string> 15 16#include "net/base/cookie_monster.h" 17 18class Browser; 19class DictionaryValue; 20class Extension; 21class ListValue; 22class Profile; 23 24namespace extension_cookies_helpers { 25 26// Returns either the original profile or the incognito profile, based on the 27// given store ID. Returns NULL if the profile doesn't exist or is not allowed 28// (e.g. if incognito mode is not enabled for the extension). 29Profile* ChooseProfileFromStoreId(const std::string& store_id, 30 Profile* profile, 31 bool include_incognito); 32 33// Returns the store ID for a particular user profile. 34const char* GetStoreIdFromProfile(Profile* profile); 35 36// Constructs a Cookie object as defined by the cookies API. This function 37// allocates a new DictionaryValue object; the caller is responsible for 38// freeing it. 39DictionaryValue* CreateCookieValue( 40 const net::CookieMonster::CanonicalCookie& cookie, 41 const std::string& store_id); 42 43// Constructs a CookieStore object as defined by the cookies API. This function 44// allocates a new DictionaryValue object; the caller is responsible for 45// freeing it. 46DictionaryValue* CreateCookieStoreValue(Profile* profile, 47 ListValue* tab_ids); 48 49// Retrieves all cookies from the given cookie store corresponding to the given 50// URL. If the URL is empty, all cookies in the cookie store are retrieved. 51// This can only be called on the IO thread. 52net::CookieList GetCookieListFromStore( 53 net::CookieStore* cookie_store, const GURL& url); 54 55// Constructs a URL from a cookie's information for use in checking 56// a cookie against the extension's host permissions. The Secure 57// property of the cookie defines the URL scheme, and the cookie's 58// domain becomes the URL host. 59GURL GetURLFromCanonicalCookie( 60 const net::CookieMonster::CanonicalCookie& cookie); 61 62// Looks through all cookies in the given cookie store, and appends to the 63// match list all the cookies that both match the given URL and cookie details 64// and are allowed by extension host permissions. 65void AppendMatchingCookiesToList( 66 const net::CookieList& all_cookies, 67 const std::string& store_id, 68 const GURL& url, const DictionaryValue* details, 69 const Extension* extension, 70 ListValue* match_list); 71 72// Appends the IDs of all tabs belonging to the given browser to the 73// given list. 74void AppendToTabIdList(Browser* browser, ListValue* tab_ids); 75 76// A class representing the cookie filter parameters passed into 77// cookies.getAll(). 78// This class is essentially a convenience wrapper for the details dictionary 79// passed into the cookies.getAll() API by the user. If the dictionary contains 80// no filter parameters, the MatchFilter will always trivially 81// match all cookies. 82class MatchFilter { 83 public: 84 // Takes the details dictionary argument given by the user as input. 85 // This class does not take ownership of the lifetime of the DictionaryValue 86 // object. 87 explicit MatchFilter(const DictionaryValue* details); 88 89 // Returns true if the given cookie matches the properties in the match 90 // filter. 91 bool MatchesCookie(const net::CookieMonster::CanonicalCookie& cookie); 92 93 private: 94 // Returns true if the details dictionary contains a string with the given 95 // key and value. Also returns true if the dictionary doesn't contain the 96 // given key at all (trival match). 97 bool MatchesString(const char* key, const std::string& value); 98 99 // Returns true if the details dictionary contains a boolean with the given 100 // key and value. Also returns true if the dictionary doesn't contain the 101 // given key at all (trival match). 102 bool MatchesBoolean(const char* key, bool value); 103 104 // Returns true if the given cookie domain string matches the filter's 105 // domain. Any cookie domain which is equal to or is a subdomain of the 106 // filter's domain will be matched; leading '.' characters indicating 107 // host-only domains have no meaning in the match filter domain (for 108 // instance, a match filter domain of 'foo.bar.com' will be treated the same 109 // as '.foo.bar.com', and both will match cookies with domain values of 110 // 'foo.bar.com', '.foo.bar.com', and 'baz.foo.bar.com'. 111 bool MatchesDomain(const std::string& domain); 112 113 const DictionaryValue* details_; 114}; 115 116} // namespace extension_cookies_helpers 117 118#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_COOKIES_HELPERS_H_ 119