summaryrefslogtreecommitdiffstats
path: root/chrome/browser/importer/toolbar_importer_utils.cc
blob: 69931c216480b9869822aa2f3a6ae0cb5c0e74b8 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) 2012 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/importer/toolbar_importer_utils.h"

#include <string>
#include <vector>

#include "base/bind.h"
#include "base/string_split.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_thread.h"
#include "googleurl/src/gurl.h"
#include "net/cookies/cookie_store.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"

using content::BrowserThread;

namespace {
const char kGoogleDomainUrl[] = "http://.google.com/";
const char kGoogleDomainSecureCookieId[] = "SID=";
const char kSplitStringToken = ';';
}

namespace toolbar_importer_utils {

void OnGetCookies(const base::Callback<void(bool)>& callback,
                  const std::string& cookies) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  std::vector<std::string> cookie_list;
  base::SplitString(cookies, kSplitStringToken, &cookie_list);
  for (std::vector<std::string>::iterator current = cookie_list.begin();
       current != cookie_list.end();
       ++current) {
    size_t position = (*current).find(kGoogleDomainSecureCookieId);
    if (position == 0) {
      callback.Run(true);
      return;
    }
  }
  callback.Run(false);
}

void OnFetchComplete(const base::Callback<void(bool)>& callback,
                     const std::string& cookies) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::Bind(&OnGetCookies, callback, cookies));
}

void FetchCookiesOnIOThread(
    const base::Callback<void(bool)>& callback,
    net::URLRequestContextGetter* context_getter) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  net::CookieStore* store = context_getter->
      GetURLRequestContext()->cookie_store();
  GURL url(kGoogleDomainUrl);
  net::CookieOptions options;
  options.set_include_httponly();  // The SID cookie might be httponly.
  store->GetCookiesWithOptionsAsync(
      url, options,
      base::Bind(&toolbar_importer_utils::OnFetchComplete, callback));
}

void IsGoogleGAIACookieInstalled(const base::Callback<void(bool)>& callback,
                                 Profile* profile) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  if (!callback.is_null()) {
    BrowserThread::PostTask(
        BrowserThread::IO, FROM_HERE,
        base::Bind(&FetchCookiesOnIOThread,
                   callback, base::Unretained(profile->GetRequestContext())));
  }
}

}  // namespace toolbar_importer_utils