1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file.
4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "components/feedback/feedback_uploader_chrome.h"
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/callback.h"
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/command_line.h"
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/files/file_path.h"
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/task_runner_util.h"
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/threading/sequenced_worker_pool.h"
12010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "components/feedback/feedback_report.h"
13010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "components/feedback/feedback_switches.h"
14010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "components/feedback/feedback_uploader_delegate.h"
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/browser/browser_context.h"
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/base/load_flags.h"
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/url_request/url_fetcher.h"
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "url/gurl.h"
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)using content::BrowserThread;
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace feedback {
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace {
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)const char kProtoBufMimeType[] = "application/x-protobuf";
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)FeedbackUploaderChrome::FeedbackUploaderChrome(
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::BrowserContext* context)
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    : FeedbackUploader(context ? context->GetPath() : base::FilePath(),
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                       BrowserThread::GetBlockingPool()),
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      context_(context) {
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  CHECK(context_);
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kFeedbackServer))
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    url_ = CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        switches::kFeedbackServer);
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void FeedbackUploaderChrome::DispatchReport(const std::string& data) {
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GURL post_url(url_);
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  net::URLFetcher* fetcher = net::URLFetcher::Create(
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      post_url, net::URLFetcher::POST,
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      new FeedbackUploaderDelegate(
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          data,
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          base::Bind(&FeedbackUploaderChrome::UpdateUploadTimer, AsWeakPtr()),
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          base::Bind(&FeedbackUploaderChrome::RetryReport, AsWeakPtr())));
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  fetcher->SetUploadData(std::string(kProtoBufMimeType), data);
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  fetcher->SetRequestContext(context_->GetRequestContext());
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES |
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                        net::LOAD_DO_NOT_SEND_COOKIES);
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  fetcher->Start();
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace feedback
59