summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/external_cookie_handler.cc
blob: 3af6b04181b55f2db182a1fe16b60fe00149482d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/external_cookie_handler.h"

#include "base/command_line.h"
#include "base/file_util.h"
#include "chrome/browser/chromeos/pipe_reader.h"
#include "chrome/browser/profile.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/net/url_request_context_getter.h"
#include "googleurl/src/gurl.h"
#include "net/base/cookie_store.h"
#include "net/url_request/url_request_context.h"

namespace chromeos {

// static
const char ExternalCookieHandler::kGoogleAccountsUrl[] =
    "https://www.google.com/a/google.com/acs";

const int kChunkSize = 256;

// Reads up to a newline, or the end of the data, in increments of |chunk|
std::string ExternalCookieHandler::ReadLine(int chunk) {
  std::string cookie_line = reader_->Read(chunk);

  // As long as it's not an empty line...
  if (!cookie_line.empty()) {
    // and there's no newline at the end...
    while ('\n' != cookie_line[cookie_line.length() - 1]) {
      // try to pull more data...
      std::string piece = reader_->Read(chunk);
      if (piece.empty())  // only stop if there's none left.
        break;
      else
        cookie_line.append(piece);  // otherwise, append and keep going.
    }
  }

  return cookie_line;
}

bool ExternalCookieHandler::HandleCookies(net::CookieStore *cookie_store) {
  DCHECK(cookie_store);
  if (NULL != reader_.get()) {
    GURL url(ExternalCookieHandler::kGoogleAccountsUrl);
    net::CookieOptions options;
    options.set_include_httponly();

    // Each line we get is a cookie.  Grab up to a newline, then put
    // it in to the cookie jar.
    std::string cookie_line = ReadLine(kChunkSize);
    while (!cookie_line.empty()) {
      if (!cookie_store->SetCookieWithOptions(url, cookie_line, options))
        return false;
      cookie_line = ReadLine(kChunkSize);
    }
    return true;
  }
  return false;
}

}  // namespace chromeos