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 send report request. |
23 extensions::SystemInformationList* sys_info_list, | 26 FeedbackService::RequestId g_last_send_feedback_request_Id = 0; |
| 27 |
| 28 void PopulateSystemInfo(SystemInformationList* sys_info_list, |
24 const std::string& key, | 29 const std::string& key, |
25 const std::string& value) { | 30 const std::string& value) { |
26 base::DictionaryValue sys_info_value; | 31 base::DictionaryValue sys_info_value; |
27 sys_info_value.Set("key", new base::StringValue(key)); | 32 sys_info_value.Set("key", new base::StringValue(key)); |
28 sys_info_value.Set("value", new base::StringValue(value)); | 33 sys_info_value.Set("value", new base::StringValue(value)); |
29 | 34 |
30 linked_ptr<SystemInformation> sys_info(new SystemInformation()); | 35 linked_ptr<SystemInformation> sys_info(new SystemInformation()); |
31 SystemInformation::Populate(sys_info_value, sys_info.get()); | 36 SystemInformation::Populate(sys_info_value, sys_info.get()); |
32 | 37 |
33 sys_info_list->push_back(sys_info); | 38 sys_info_list->push_back(sys_info); |
34 } | 39 } |
35 | 40 |
36 } // namespace | 41 } // namespace |
37 | 42 |
38 namespace extensions { | 43 // Represents a request to send a feedback report. It is used to be able to |
| 44 // handle multiple send requests. |
| 45 struct FeedbackService::SendRequest { |
| 46 RequestId request_id; |
| 47 scoped_refptr<feedback::FeedbackData> feedback_data; |
| 48 SendFeedbackCallback on_sent_callback; |
| 49 }; |
39 | 50 |
40 FeedbackService::FeedbackService() { | 51 FeedbackService::FeedbackService() { |
41 } | 52 } |
42 | 53 |
43 FeedbackService::~FeedbackService() { | 54 FeedbackService::~FeedbackService() { |
44 } | 55 } |
45 | 56 |
46 void FeedbackService::SendFeedback( | 57 void FeedbackService::SendFeedback( |
47 Profile* profile, | 58 Profile* profile, |
48 scoped_refptr<FeedbackData> feedback_data, | 59 scoped_refptr<FeedbackData> feedback_data, |
49 const SendFeedbackCallback& callback) { | 60 const SendFeedbackCallback& callback) { |
50 send_feedback_callback_ = callback; | 61 // Give every send request a unique Id. |
51 feedback_data_ = feedback_data; | 62 const RequestId id = ++g_last_send_feedback_request_Id; |
52 feedback_data_->set_locale(g_browser_process->GetApplicationLocale()); | 63 SendRequest& request = send_feedback_requests_[id]; |
53 feedback_data_->set_user_agent(GetUserAgent()); | |
54 | 64 |
55 if (!feedback_data_->attached_file_uuid().empty()) { | 65 request.request_id = id; |
| 66 request.on_sent_callback = callback; |
| 67 request.feedback_data = feedback_data; |
| 68 request.feedback_data->set_locale(g_browser_process->GetApplicationLocale()); |
| 69 request.feedback_data->set_user_agent(GetUserAgent()); |
| 70 |
| 71 if (!request.feedback_data->attached_file_uuid().empty()) { |
56 // Self-deleting object. | 72 // Self-deleting object. |
57 BlobReader* attached_file_reader = new BlobReader( | 73 BlobReader* attached_file_reader = new BlobReader( |
58 profile, feedback_data_->attached_file_uuid(), | 74 profile, request.feedback_data->attached_file_uuid(), |
59 base::Bind(&FeedbackService::AttachedFileCallback, AsWeakPtr())); | 75 base::Bind(&FeedbackService::AttachedFileCallback, AsWeakPtr(), id)); |
60 attached_file_reader->Start(); | 76 attached_file_reader->Start(); |
61 } | 77 } |
62 | 78 |
63 if (!feedback_data_->screenshot_uuid().empty()) { | 79 if (!request.feedback_data->screenshot_uuid().empty()) { |
64 // Self-deleting object. | 80 // Self-deleting object. |
65 BlobReader* screenshot_reader = new BlobReader( | 81 BlobReader* screenshot_reader = new BlobReader( |
66 profile, feedback_data_->screenshot_uuid(), | 82 profile, request.feedback_data->screenshot_uuid(), |
67 base::Bind(&FeedbackService::ScreenshotCallback, AsWeakPtr())); | 83 base::Bind(&FeedbackService::ScreenshotCallback, AsWeakPtr(), id)); |
68 screenshot_reader->Start(); | 84 screenshot_reader->Start(); |
69 } | 85 } |
70 | 86 |
71 CompleteSendFeedback(); | 87 CompleteSendFeedback(id); |
72 } | |
73 | |
74 void FeedbackService::AttachedFileCallback(scoped_ptr<std::string> data, | |
75 int64_t /* total_blob_length */) { | |
76 feedback_data_->set_attached_file_uuid(std::string()); | |
77 if (data) | |
78 feedback_data_->AttachAndCompressFileData(std::move(data)); | |
79 | |
80 CompleteSendFeedback(); | |
81 } | |
82 | |
83 void FeedbackService::ScreenshotCallback(scoped_ptr<std::string> data, | |
84 int64_t /* total_blob_length */) { | |
85 feedback_data_->set_screenshot_uuid(std::string()); | |
86 if (data) | |
87 feedback_data_->set_image(std::move(data)); | |
88 | |
89 CompleteSendFeedback(); | |
90 } | 88 } |
91 | 89 |
92 void FeedbackService::GetSystemInformation( | 90 void FeedbackService::GetSystemInformation( |
93 const GetSystemInformationCallback& callback) { | 91 const GetSystemInformationCallback& callback) { |
94 system_information_callback_ = callback; | |
95 | |
96 system_logs::ScrubbedSystemLogsFetcher* fetcher = | 92 system_logs::ScrubbedSystemLogsFetcher* fetcher = |
97 new system_logs::ScrubbedSystemLogsFetcher(); | 93 new system_logs::ScrubbedSystemLogsFetcher(); |
98 fetcher->Fetch( | 94 fetcher->Fetch(base::Bind(&FeedbackService::OnSystemLogsFetchComplete, |
99 base::Bind(&FeedbackService::OnSystemLogsFetchComplete, AsWeakPtr())); | 95 AsWeakPtr(), |
| 96 callback)); |
100 } | 97 } |
101 | 98 |
| 99 void FeedbackService::AttachedFileCallback(RequestId request_id, |
| 100 scoped_ptr<std::string> data, |
| 101 int64_t /* total_blob_length */) { |
| 102 DCHECK(ContainsKey(send_feedback_requests_, request_id)); |
| 103 |
| 104 SendRequest& request = send_feedback_requests_[request_id]; |
| 105 request.feedback_data->set_attached_file_uuid(std::string()); |
| 106 if (data) |
| 107 request.feedback_data->AttachAndCompressFileData(std::move(data)); |
| 108 |
| 109 CompleteSendFeedback(request_id); |
| 110 } |
| 111 |
| 112 void FeedbackService::ScreenshotCallback(RequestId request_id, |
| 113 scoped_ptr<std::string> data, |
| 114 int64_t /* total_blob_length */) { |
| 115 DCHECK(ContainsKey(send_feedback_requests_, request_id)); |
| 116 |
| 117 SendRequest& request = send_feedback_requests_[request_id]; |
| 118 request.feedback_data->set_screenshot_uuid(std::string()); |
| 119 if (data) |
| 120 request.feedback_data->set_image(std::move(data)); |
| 121 |
| 122 CompleteSendFeedback(request_id); |
| 123 } |
102 | 124 |
103 void FeedbackService::OnSystemLogsFetchComplete( | 125 void FeedbackService::OnSystemLogsFetchComplete( |
| 126 const GetSystemInformationCallback& callback, |
104 scoped_ptr<system_logs::SystemLogsResponse> sys_info_map) { | 127 scoped_ptr<system_logs::SystemLogsResponse> sys_info_map) { |
105 SystemInformationList sys_info_list; | 128 SystemInformationList sys_info_list; |
106 if (!sys_info_map.get()) { | 129 if (sys_info_map.get()) { |
107 system_information_callback_.Run(sys_info_list); | 130 for (const auto& itr : *sys_info_map) |
108 return; | 131 PopulateSystemInfo(&sys_info_list, itr.first, itr.second); |
109 } | 132 } |
110 | 133 |
111 for (system_logs::SystemLogsResponse::iterator it = sys_info_map->begin(); | 134 callback.Run(sys_info_list); |
112 it != sys_info_map->end(); ++it) | |
113 PopulateSystemInfo(&sys_info_list, it->first, it->second); | |
114 | |
115 system_information_callback_.Run(sys_info_list); | |
116 } | 135 } |
117 | 136 |
118 void FeedbackService::CompleteSendFeedback() { | 137 void FeedbackService::CompleteSendFeedback(RequestId request_id) { |
119 // A particular data collection is considered completed if, | 138 // A particular data collection is considered completed if, |
120 // a.) The blob URL is invalid - this will either happen because we never had | 139 // 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 | 140 // 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. | 141 // 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 | 142 // 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 | 143 // and the read callback has updated the associated data on the feedback |
125 // object. | 144 // object. |
126 bool attached_file_completed = feedback_data_->attached_file_uuid().empty(); | 145 DCHECK(ContainsKey(send_feedback_requests_, request_id)); |
127 bool screenshot_completed = feedback_data_->screenshot_uuid().empty(); | 146 |
| 147 SendRequest& request = send_feedback_requests_[request_id]; |
| 148 const bool attached_file_completed = |
| 149 request.feedback_data->attached_file_uuid().empty(); |
| 150 const bool screenshot_completed = |
| 151 request.feedback_data->screenshot_uuid().empty(); |
128 | 152 |
129 if (screenshot_completed && attached_file_completed) { | 153 if (screenshot_completed && attached_file_completed) { |
130 // Signal the feedback object that the data from the feedback page has been | 154 // Signal the feedback object that the data from the feedback page has been |
131 // filled - the object will manage sending of the actual report. | 155 // filled - the object will manage sending of the actual report. |
132 feedback_data_->OnFeedbackPageDataComplete(); | 156 request.feedback_data->OnFeedbackPageDataComplete(); |
| 157 |
133 // TODO(rkc): Change this once we have FeedbackData/Util refactored to | 158 // TODO(rkc): Change this once we have FeedbackData/Util refactored to |
134 // report the status of the report being sent. | 159 // report the status of the report being sent. |
135 send_feedback_callback_.Run(true); | 160 request.on_sent_callback.Run(true); |
| 161 |
| 162 // We're done with this request. |
| 163 send_feedback_requests_.erase(request_id); |
136 } | 164 } |
137 } | 165 } |
138 | 166 |
139 } // namespace extensions | 167 } // namespace extensions |
OLD | NEW |