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