feedback_service.cc revision 3551c9c881056c480085172ff9840cab31610854
1// Copyright 2013 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 "chrome/browser/extensions/api/feedback_private/feedback_service.h"
6
7#include "base/callback.h"
8#include "base/memory/weak_ptr.h"
9#include "base/strings/string_number_conversions.h"
10#include "chrome/browser/profiles/profile.h"
11#include "content/public/browser/browser_thread.h"
12
13using content::BrowserThread;
14
15namespace extensions {
16
17// static
18void FeedbackService::PopulateSystemInfo(
19    SystemInformationList* sys_info_list,
20    const std::string& key,
21    const std::string& value) {
22  base::DictionaryValue sys_info_value;
23  sys_info_value.Set("key", new base::StringValue(key));
24  sys_info_value.Set("value", new base::StringValue(value));
25
26  linked_ptr<SystemInformation> sys_info(new SystemInformation());
27  SystemInformation::Populate(sys_info_value, sys_info.get());
28
29  sys_info_list->push_back(sys_info);
30}
31
32FeedbackService::FeedbackService() {
33}
34
35FeedbackService::~FeedbackService() {
36}
37
38void FeedbackService::SendFeedback(
39    Profile* profile,
40    scoped_refptr<FeedbackData> feedback_data,
41    const SendFeedbackCallback& callback) {
42  send_feedback_callback_ = callback;
43  feedback_data_ = feedback_data;
44
45  if (feedback_data_->attached_file_url().is_valid()) {
46    // Self-deleting object.
47    BlobReader* attached_file_reader = new BlobReader(
48        profile, feedback_data_->attached_file_url(),
49        base::Bind(&FeedbackService::AttachedFileCallback,
50                   GetWeakPtr()));
51    attached_file_reader->Start();
52  }
53
54  if (feedback_data_->screenshot_url().is_valid()) {
55    // Self-deleting object.
56    BlobReader* screenshot_reader = new BlobReader(
57        profile, feedback_data_->screenshot_url(),
58        base::Bind(&FeedbackService::ScreenshotCallback,
59                   GetWeakPtr()));
60    screenshot_reader->Start();
61  }
62
63  CompleteSendFeedback();
64}
65
66void FeedbackService::AttachedFileCallback(scoped_ptr<std::string> data) {
67  if (!data.get())
68    feedback_data_->set_attached_file_url(GURL());
69  else
70    feedback_data_->set_attached_filedata(data.Pass());
71
72  CompleteSendFeedback();
73}
74
75void FeedbackService::ScreenshotCallback(scoped_ptr<std::string> data) {
76  if (!data.get())
77    feedback_data_->set_screenshot_url(GURL());
78  else
79    feedback_data_->set_image(data.Pass());
80
81  CompleteSendFeedback();
82}
83
84void FeedbackService::CompleteSendFeedback() {
85  // A particular data collection is considered completed if,
86  // a.) The blob URL is invalid - this will either happen because we never had
87  //     a URL and never needed to read this data, or that the data read failed
88  //     and we set it to invalid in the data read callback.
89  // b.) The associated data object exists, meaning that the data has been read
90  //     and the read callback has updated the associated data on the feedback
91  //     object.
92  bool attached_file_completed =
93      !feedback_data_->attached_file_url().is_valid() ||
94      feedback_data_->attached_filedata();
95  bool screenshot_completed =
96      !feedback_data_->screenshot_url().is_valid() ||
97      feedback_data_->image();
98
99  if (screenshot_completed && attached_file_completed) {
100    // Signal the feedback object that the data from the feedback page has been
101    // filled - the object will manage sending of the actual report.
102    feedback_data_->OnFeedbackPageDataComplete();
103    // TODO(rkc): Change this once we have FeedbackData/Util refactored to
104    // report the status of the report being sent.
105    send_feedback_callback_.Run(true);
106  }
107}
108
109}  // namespace extensions
110