Home | History | Annotate | Download | only in renderer
      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 #include "content/renderer/renderer_webcookiejar_impl.h"
      6 
      7 #include "base/strings/utf_string_conversions.h"
      8 #include "content/common/cookie_data.h"
      9 #include "content/common/view_messages.h"
     10 #include "content/public/renderer/content_renderer_client.h"
     11 #include "content/renderer/render_view_impl.h"
     12 #include "third_party/WebKit/public/platform/WebCookie.h"
     13 
     14 using blink::WebCookie;
     15 using blink::WebString;
     16 using blink::WebURL;
     17 using blink::WebVector;
     18 
     19 namespace content {
     20 
     21 void RendererWebCookieJarImpl::setCookie(
     22     const WebURL& url, const WebURL& first_party_for_cookies,
     23     const WebString& value) {
     24   std::string value_utf8 = UTF16ToUTF8(value);
     25   sender_->Send(new ViewHostMsg_SetCookie(
     26       MSG_ROUTING_NONE, url, first_party_for_cookies, value_utf8));
     27 }
     28 
     29 WebString RendererWebCookieJarImpl::cookies(
     30     const WebURL& url, const WebURL& first_party_for_cookies) {
     31   std::string value_utf8;
     32   sender_->Send(new ViewHostMsg_GetCookies(
     33       MSG_ROUTING_NONE, url, first_party_for_cookies, &value_utf8));
     34   return WebString::fromUTF8(value_utf8);
     35 }
     36 
     37 WebString RendererWebCookieJarImpl::cookieRequestHeaderFieldValue(
     38     const WebURL& url, const WebURL& first_party_for_cookies) {
     39   return cookies(url, first_party_for_cookies);
     40 }
     41 
     42 void RendererWebCookieJarImpl::rawCookies(
     43     const WebURL& url, const WebURL& first_party_for_cookies,
     44     WebVector<WebCookie>& raw_cookies) {
     45   std::vector<CookieData> cookies;
     46   // NOTE: This may pump events (see RenderThread::Send).
     47   sender_->Send(new ViewHostMsg_GetRawCookies(
     48       url, first_party_for_cookies, &cookies));
     49 
     50   WebVector<WebCookie> result(cookies.size());
     51   for (size_t i = 0; i < cookies.size(); ++i) {
     52     const CookieData& c = cookies[i];
     53     result[i] = WebCookie(WebString::fromUTF8(c.name),
     54                           WebString::fromUTF8(c.value),
     55                           WebString::fromUTF8(c.domain),
     56                           WebString::fromUTF8(c.path),
     57                           c.expires,
     58                           c.http_only,
     59                           c.secure,
     60                           c.session);
     61   }
     62   raw_cookies.swap(result);
     63 }
     64 
     65 void RendererWebCookieJarImpl::deleteCookie(
     66     const WebURL& url, const WebString& cookie_name) {
     67   std::string cookie_name_utf8 = UTF16ToUTF8(cookie_name);
     68   sender_->Send(new ViewHostMsg_DeleteCookie(url, cookie_name_utf8));
     69 }
     70 
     71 bool RendererWebCookieJarImpl::cookiesEnabled(
     72     const WebURL& url, const WebURL& first_party_for_cookies) {
     73   bool cookies_enabled;
     74   // NOTE: This may pump events (see RenderThread::Send).
     75   sender_->Send(new ViewHostMsg_CookiesEnabled(
     76       url, first_party_for_cookies, &cookies_enabled));
     77   return cookies_enabled;
     78 }
     79 
     80 }  // namespace content
     81