Home | History | Annotate | Download | only in feedback_private
      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 "chrome/browser/ui/webui/screenshot_source.h"
     12 #include "content/public/browser/browser_thread.h"
     13 
     14 using content::BrowserThread;
     15 
     16 namespace {
     17 
     18 ScreenshotDataPtr ConvertStringToScreenshotPtr(scoped_ptr<std::string> image) {
     19   ScreenshotDataPtr screenshot(new ScreenshotData);
     20   std::copy(image->begin(), image->end(), screenshot->begin());
     21   return screenshot;
     22 }
     23 
     24 }
     25 
     26 namespace extensions {
     27 
     28 FeedbackService::FeedbackService() {
     29 }
     30 
     31 FeedbackService::~FeedbackService() {
     32 }
     33 
     34 void FeedbackService::SendFeedback(
     35     Profile* profile,
     36     scoped_refptr<FeedbackData> feedback_data,
     37     const SendFeedbackCallback& callback) {
     38   send_feedback_callback_ = callback;
     39   feedback_data_ = feedback_data;
     40 
     41   if (feedback_data_->attached_file_url().is_valid()) {
     42     attached_file_reader_ = new BlobReader(
     43         profile, feedback_data_->attached_file_url(),
     44         base::Bind(&FeedbackService::AttachedFileCallback,
     45                    GetWeakPtr()));
     46     attached_file_reader_->Start();
     47   }
     48 
     49   if (feedback_data_->screenshot_url().is_valid()) {
     50     attached_file_reader_ = new BlobReader(
     51         profile, feedback_data_->screenshot_url(),
     52         base::Bind(&FeedbackService::ScreenshotCallback,
     53                    GetWeakPtr()));
     54     screenshot_reader_->Start();
     55   }
     56 
     57   CompleteSendFeedback();
     58 }
     59 
     60 void FeedbackService::AttachedFileCallback(scoped_ptr<std::string> data) {
     61   if (!data.get())
     62     feedback_data_->set_attached_file_url(GURL());
     63   else
     64     feedback_data_->set_attached_filedata(data.Pass());
     65 
     66   CompleteSendFeedback();
     67 }
     68 
     69 void FeedbackService::ScreenshotCallback(scoped_ptr<std::string> data) {
     70   if (!data.get())
     71     feedback_data_->set_screenshot_url(GURL());
     72   else
     73     feedback_data_->set_image(ConvertStringToScreenshotPtr(data.Pass()));
     74 
     75   CompleteSendFeedback();
     76 }
     77 
     78 void FeedbackService::CompleteSendFeedback() {
     79   // If either the blob URL is invalid (we never needed to read it), or if the
     80   // data exists in the feedback object (the read is completed).
     81   bool attached_file_completed =
     82       !feedback_data_->attached_file_url().is_valid() ||
     83       feedback_data_->attached_filedata();
     84   bool screenshot_completed =
     85       !feedback_data_->screenshot_url().is_valid() ||
     86       !feedback_data_->image().get();
     87 
     88   if (screenshot_completed && attached_file_completed) {
     89     // Signal the feedback object that the data from the feedback page has been
     90     // filled - the object will manage sending of the actual report.
     91     feedback_data_->FeedbackPageDataComplete();
     92     // TODO(rkc): Change this once we have FeedbackData/Util refactored to
     93     // report the status of the report being sent.
     94     send_feedback_callback_.Run(true);
     95   }
     96 }
     97 
     98 }  // namespace extensions
     99