Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/feedback_private/feedback_service.h" | 5 #include "chrome/browser/extensions/api/feedback_private/feedback_service.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/stl_util.h" | |
| 11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 12 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/chrome_content_client.h" | 15 #include "chrome/common/chrome_content_client.h" |
| 15 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 16 | 17 |
| 17 using content::BrowserThread; | 18 using content::BrowserThread; |
| 18 using feedback::FeedbackData; | 19 using feedback::FeedbackData; |
| 19 | 20 |
| 21 namespace extensions { | |
| 22 | |
| 20 namespace { | 23 namespace { |
| 21 | 24 |
| 22 void PopulateSystemInfo( | 25 // Give unique IDs to each system information request, and send report request. |
| 23 extensions::SystemInformationList* sys_info_list, | 26 FeedbackService::RequestId g_last_sys_info_request_Id = 0; |
| 27 FeedbackService::RequestId g_last_send_feedback_request_Id = 0; | |
| 28 | |
| 29 void PopulateSystemInfo(SystemInformationList* sys_info_list, | |
| 24 const std::string& key, | 30 const std::string& key, |
| 25 const std::string& value) { | 31 const std::string& value) { |
| 26 base::DictionaryValue sys_info_value; | 32 base::DictionaryValue sys_info_value; |
| 27 sys_info_value.Set("key", new base::StringValue(key)); | 33 sys_info_value.Set("key", new base::StringValue(key)); |
| 28 sys_info_value.Set("value", new base::StringValue(value)); | 34 sys_info_value.Set("value", new base::StringValue(value)); |
| 29 | 35 |
| 30 linked_ptr<SystemInformation> sys_info(new SystemInformation()); | 36 linked_ptr<SystemInformation> sys_info(new SystemInformation()); |
| 31 SystemInformation::Populate(sys_info_value, sys_info.get()); | 37 SystemInformation::Populate(sys_info_value, sys_info.get()); |
| 32 | 38 |
| 33 sys_info_list->push_back(sys_info); | 39 sys_info_list->push_back(sys_info); |
| 34 } | 40 } |
| 35 | 41 |
| 36 } // namespace | 42 } // namespace |
| 37 | 43 |
| 38 namespace extensions { | 44 // Represents a request to send a feedback report. It is used to be able to |
| 45 // handle multiple send requests. | |
| 46 struct FeedbackService::SendRequest { | |
| 47 RequestId request_id; | |
| 48 scoped_refptr<feedback::FeedbackData> feedback_data; | |
| 49 SendFeedbackCallback on_sent_callback; | |
| 50 }; | |
| 39 | 51 |
| 40 FeedbackService::FeedbackService() { | 52 FeedbackService::FeedbackService() { |
| 41 } | 53 } |
| 42 | 54 |
| 43 FeedbackService::~FeedbackService() { | 55 FeedbackService::~FeedbackService() { |
| 44 } | 56 } |
| 45 | 57 |
| 46 void FeedbackService::SendFeedback( | 58 void FeedbackService::SendFeedback( |
| 47 Profile* profile, | 59 Profile* profile, |
| 48 scoped_refptr<FeedbackData> feedback_data, | 60 scoped_refptr<FeedbackData> feedback_data, |
| 49 const SendFeedbackCallback& callback) { | 61 const SendFeedbackCallback& callback) { |
| 50 send_feedback_callback_ = callback; | 62 // Give every send request a unique Id. |
| 51 feedback_data_ = feedback_data; | 63 const RequestId id = ++g_last_send_feedback_request_Id; |
| 52 feedback_data_->set_locale(g_browser_process->GetApplicationLocale()); | 64 SendRequest& request = send_feedback_requests_[id]; |
| 53 feedback_data_->set_user_agent(GetUserAgent()); | |
| 54 | 65 |
| 55 if (!feedback_data_->attached_file_uuid().empty()) { | 66 request.request_id = id; |
| 67 request.on_sent_callback = callback; | |
| 68 request.feedback_data = feedback_data; | |
| 69 request.feedback_data->set_locale(g_browser_process->GetApplicationLocale()); | |
| 70 request.feedback_data->set_user_agent(GetUserAgent()); | |
| 71 | |
| 72 if (!request.feedback_data->attached_file_uuid().empty()) { | |
| 56 // Self-deleting object. | 73 // Self-deleting object. |
| 57 BlobReader* attached_file_reader = new BlobReader( | 74 BlobReader* attached_file_reader = new BlobReader( |
| 58 profile, feedback_data_->attached_file_uuid(), | 75 profile, request.feedback_data->attached_file_uuid(), |
| 59 base::Bind(&FeedbackService::AttachedFileCallback, AsWeakPtr())); | 76 base::Bind(&FeedbackService::AttachedFileCallback, AsWeakPtr(), id)); |
|
xiyuan
2016/03/14 23:47:28
It seems to me that we can bind the needed context
afakhry
2016/03/15 19:25:38
I agree. Great idea. But I think we can only do th
xiyuan
2016/03/15 19:54:06
feedback_data is a ref-counted object. It would be
afakhry
2016/03/16 00:45:34
Right, right. Sorry I forgot that FeedbackData is
xiyuan
2016/03/16 16:39:28
Up to you. I am okay to do it now or in a later CL
afakhry
2016/03/16 17:51:21
Let's keep the scope of this CL small and do it in
| |
| 60 attached_file_reader->Start(); | 77 attached_file_reader->Start(); |
| 61 } | 78 } |
| 62 | 79 |
| 63 if (!feedback_data_->screenshot_uuid().empty()) { | 80 if (!request.feedback_data->screenshot_uuid().empty()) { |
| 64 // Self-deleting object. | 81 // Self-deleting object. |
| 65 BlobReader* screenshot_reader = new BlobReader( | 82 BlobReader* screenshot_reader = new BlobReader( |
| 66 profile, feedback_data_->screenshot_uuid(), | 83 profile, request.feedback_data->screenshot_uuid(), |
| 67 base::Bind(&FeedbackService::ScreenshotCallback, AsWeakPtr())); | 84 base::Bind(&FeedbackService::ScreenshotCallback, AsWeakPtr(), id)); |
| 68 screenshot_reader->Start(); | 85 screenshot_reader->Start(); |
| 69 } | 86 } |
| 70 | 87 |
| 71 CompleteSendFeedback(); | 88 CompleteSendFeedback(id); |
| 72 } | 89 } |
| 73 | 90 |
| 74 void FeedbackService::AttachedFileCallback(scoped_ptr<std::string> data, | 91 void FeedbackService::AttachedFileCallback(RequestId request_id, |
| 92 scoped_ptr<std::string> data, | |
| 75 int64_t /* total_blob_length */) { | 93 int64_t /* total_blob_length */) { |
| 76 feedback_data_->set_attached_file_uuid(std::string()); | 94 DCHECK(ContainsKey(send_feedback_requests_, request_id)); |
| 95 | |
| 96 SendRequest& request = send_feedback_requests_[request_id]; | |
| 97 request.feedback_data->set_attached_file_uuid(std::string()); | |
| 77 if (data) | 98 if (data) |
| 78 feedback_data_->AttachAndCompressFileData(std::move(data)); | 99 request.feedback_data->AttachAndCompressFileData(std::move(data)); |
| 79 | 100 |
| 80 CompleteSendFeedback(); | 101 CompleteSendFeedback(request_id); |
| 81 } | 102 } |
| 82 | 103 |
| 83 void FeedbackService::ScreenshotCallback(scoped_ptr<std::string> data, | 104 void FeedbackService::ScreenshotCallback(RequestId request_id, |
| 105 scoped_ptr<std::string> data, | |
| 84 int64_t /* total_blob_length */) { | 106 int64_t /* total_blob_length */) { |
| 85 feedback_data_->set_screenshot_uuid(std::string()); | 107 DCHECK(ContainsKey(send_feedback_requests_, request_id)); |
| 108 | |
| 109 SendRequest& request = send_feedback_requests_[request_id]; | |
| 110 request.feedback_data->set_screenshot_uuid(std::string()); | |
| 86 if (data) | 111 if (data) |
| 87 feedback_data_->set_image(std::move(data)); | 112 request.feedback_data->set_image(std::move(data)); |
| 88 | 113 |
| 89 CompleteSendFeedback(); | 114 CompleteSendFeedback(request_id); |
| 90 } | 115 } |
| 91 | 116 |
| 92 void FeedbackService::GetSystemInformation( | 117 void FeedbackService::GetSystemInformation( |
| 93 const GetSystemInformationCallback& callback) { | 118 const GetSystemInformationCallback& callback) { |
| 94 system_information_callback_ = callback; | 119 // Give every system information request a unique ID. |
| 120 const RequestId id = ++g_last_sys_info_request_Id; | |
| 121 | |
| 122 system_information_callbacks_[id] = callback; | |
| 95 | 123 |
| 96 system_logs::ScrubbedSystemLogsFetcher* fetcher = | 124 system_logs::ScrubbedSystemLogsFetcher* fetcher = |
| 97 new system_logs::ScrubbedSystemLogsFetcher(); | 125 new system_logs::ScrubbedSystemLogsFetcher(); |
| 98 fetcher->Fetch( | 126 fetcher->Fetch( |
| 99 base::Bind(&FeedbackService::OnSystemLogsFetchComplete, AsWeakPtr())); | 127 base::Bind(&FeedbackService::OnSystemLogsFetchComplete, AsWeakPtr(), id)); |
| 100 } | 128 } |
| 101 | 129 |
| 102 | 130 |
| 103 void FeedbackService::OnSystemLogsFetchComplete( | 131 void FeedbackService::OnSystemLogsFetchComplete( |
| 132 RequestId request_id, | |
| 104 scoped_ptr<system_logs::SystemLogsResponse> sys_info_map) { | 133 scoped_ptr<system_logs::SystemLogsResponse> sys_info_map) { |
| 134 DCHECK(ContainsKey(system_information_callbacks_, request_id)); | |
| 135 | |
| 136 auto& callback = system_information_callbacks_[request_id]; | |
| 137 | |
| 105 SystemInformationList sys_info_list; | 138 SystemInformationList sys_info_list; |
| 106 if (!sys_info_map.get()) { | 139 if (sys_info_map.get()) { |
| 107 system_information_callback_.Run(sys_info_list); | 140 for (const auto& itr : *sys_info_map) |
| 108 return; | 141 PopulateSystemInfo(&sys_info_list, itr.first, itr.second); |
| 109 } | 142 } |
| 110 | 143 |
| 111 for (system_logs::SystemLogsResponse::iterator it = sys_info_map->begin(); | 144 callback.Run(sys_info_list); |
| 112 it != sys_info_map->end(); ++it) | |
| 113 PopulateSystemInfo(&sys_info_list, it->first, it->second); | |
| 114 | 145 |
| 115 system_information_callback_.Run(sys_info_list); | 146 // We're done with this request. |
| 147 system_information_callbacks_.erase(request_id); | |
| 116 } | 148 } |
| 117 | 149 |
| 118 void FeedbackService::CompleteSendFeedback() { | 150 void FeedbackService::CompleteSendFeedback(RequestId request_id) { |
| 119 // A particular data collection is considered completed if, | 151 // A particular data collection is considered completed if, |
| 120 // a.) The blob URL is invalid - this will either happen because we never had | 152 // a.) The blob URL is invalid - this will either happen because we never had |
| 121 // a URL and never needed to read this data, or that the data read failed | 153 // a URL and never needed to read this data, or that the data read failed |
| 122 // and we set it to invalid in the data read callback. | 154 // and we set it to invalid in the data read callback. |
| 123 // b.) The associated data object exists, meaning that the data has been read | 155 // b.) The associated data object exists, meaning that the data has been read |
| 124 // and the read callback has updated the associated data on the feedback | 156 // and the read callback has updated the associated data on the feedback |
| 125 // object. | 157 // object. |
| 126 bool attached_file_completed = feedback_data_->attached_file_uuid().empty(); | 158 DCHECK(ContainsKey(send_feedback_requests_, request_id)); |
| 127 bool screenshot_completed = feedback_data_->screenshot_uuid().empty(); | 159 |
| 160 SendRequest& request = send_feedback_requests_[request_id]; | |
| 161 const bool attached_file_completed = | |
| 162 request.feedback_data->attached_file_uuid().empty(); | |
| 163 const bool screenshot_completed = | |
| 164 request.feedback_data->screenshot_uuid().empty(); | |
| 128 | 165 |
| 129 if (screenshot_completed && attached_file_completed) { | 166 if (screenshot_completed && attached_file_completed) { |
| 130 // Signal the feedback object that the data from the feedback page has been | 167 // Signal the feedback object that the data from the feedback page has been |
| 131 // filled - the object will manage sending of the actual report. | 168 // filled - the object will manage sending of the actual report. |
| 132 feedback_data_->OnFeedbackPageDataComplete(); | 169 request.feedback_data->OnFeedbackPageDataComplete(); |
| 170 | |
| 133 // TODO(rkc): Change this once we have FeedbackData/Util refactored to | 171 // TODO(rkc): Change this once we have FeedbackData/Util refactored to |
| 134 // report the status of the report being sent. | 172 // report the status of the report being sent. |
| 135 send_feedback_callback_.Run(true); | 173 request.on_sent_callback.Run(true); |
| 174 | |
| 175 // We're done with this request. | |
| 176 send_feedback_requests_.erase(request_id); | |
| 136 } | 177 } |
| 137 } | 178 } |
| 138 | 179 |
| 139 } // namespace extensions | 180 } // namespace extensions |
| OLD | NEW |