Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 <utility> | |
| 6 | |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #include "base/time/time.h" | |
| 9 #include "components/download/internal/proto_conversions.h" | |
| 10 #include "net/http/http_request_headers.h" | |
| 11 | |
| 12 namespace download { | |
| 13 | |
| 14 protodb::Entry_State ProtoConversions::RequestStateToProto(Entry::State state) { | |
| 15 switch (state) { | |
| 16 case Entry::State::NEW: | |
| 17 return protodb::Entry_State_NEW; | |
| 18 case Entry::State::AVAILABLE: | |
| 19 return protodb::Entry_State_AVAILABLE; | |
| 20 case Entry::State::ACTIVE: | |
| 21 return protodb::Entry_State_ACTIVE; | |
| 22 case Entry::State::PAUSED: | |
| 23 return protodb::Entry_State_PAUSED; | |
| 24 case Entry::State::COMPLETE: | |
| 25 return protodb::Entry_State_COMPLETE; | |
| 26 case Entry::State::WATCHDOG: | |
| 27 return protodb::Entry_State_WATCHDOG; | |
| 28 } | |
| 29 } | |
| 30 | |
| 31 Entry::State ProtoConversions::RequestStateFromProto( | |
| 32 protodb::Entry_State state) { | |
| 33 switch (state) { | |
| 34 case protodb::Entry_State_NEW: | |
| 35 return Entry::State::NEW; | |
| 36 case protodb::Entry_State_AVAILABLE: | |
| 37 return Entry::State::AVAILABLE; | |
| 38 case protodb::Entry_State_ACTIVE: | |
| 39 return Entry::State::ACTIVE; | |
| 40 case protodb::Entry_State_PAUSED: | |
| 41 return Entry::State::PAUSED; | |
| 42 case protodb::Entry_State_COMPLETE: | |
| 43 return Entry::State::COMPLETE; | |
| 44 case protodb::Entry_State_WATCHDOG: | |
| 45 return Entry::State::WATCHDOG; | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 protodb::DownloadClient ProtoConversions::DownloadClientToProto( | |
| 50 DownloadClient client) { | |
| 51 switch (client) { | |
| 52 case DownloadClient::INVALID: | |
| 53 return protodb::DownloadClient::INVALID; | |
| 54 case DownloadClient::TEST: | |
| 55 return protodb::DownloadClient::TEST; | |
| 56 case DownloadClient::OFFLINE_PAGE_PREFETCH: | |
| 57 return protodb::DownloadClient::OFFLINE_PAGE_PREFETCH; | |
| 58 case DownloadClient::BOUNDARY: | |
| 59 return protodb::DownloadClient::BOUNDARY; | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 DownloadClient ProtoConversions::DownloadClientFromProto( | |
| 64 protodb::DownloadClient client) { | |
| 65 switch (client) { | |
| 66 case protodb::DownloadClient::INVALID: | |
| 67 return DownloadClient::INVALID; | |
| 68 case protodb::DownloadClient::TEST: | |
| 69 return DownloadClient::TEST; | |
| 70 case protodb::DownloadClient::OFFLINE_PAGE_PREFETCH: | |
| 71 return DownloadClient::OFFLINE_PAGE_PREFETCH; | |
| 72 case protodb::DownloadClient::BOUNDARY: | |
| 73 return DownloadClient::BOUNDARY; | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 SchedulingParams::NetworkRequirements | |
| 78 ProtoConversions::NetworkRequirementsFromProto( | |
| 79 protodb::SchedulingParams_NetworkRequirements network_requirements) { | |
| 80 switch (network_requirements) { | |
| 81 case protodb::SchedulingParams_NetworkRequirements_NONE: | |
| 82 return SchedulingParams::NetworkRequirements::NONE; | |
| 83 case protodb::SchedulingParams_NetworkRequirements_OPTIMISTIC: | |
| 84 return SchedulingParams::NetworkRequirements::OPTIMISTIC; | |
| 85 case protodb::SchedulingParams_NetworkRequirements_UNMETERED: | |
| 86 return SchedulingParams::NetworkRequirements::UNMETERED; | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 protodb::SchedulingParams_NetworkRequirements | |
| 91 ProtoConversions::NetworkRequirementsToProto( | |
| 92 SchedulingParams::NetworkRequirements network_requirements) { | |
| 93 switch (network_requirements) { | |
| 94 case SchedulingParams::NetworkRequirements::NONE: | |
| 95 return protodb::SchedulingParams_NetworkRequirements_NONE; | |
| 96 case SchedulingParams::NetworkRequirements::OPTIMISTIC: | |
| 97 return protodb::SchedulingParams_NetworkRequirements_OPTIMISTIC; | |
| 98 case SchedulingParams::NetworkRequirements::UNMETERED: | |
| 99 return protodb::SchedulingParams_NetworkRequirements_UNMETERED; | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 SchedulingParams::BatteryRequirements | |
| 104 ProtoConversions::BatteryRequirementsFromProto( | |
| 105 protodb::SchedulingParams_BatteryRequirements battery_requirements) { | |
| 106 switch (battery_requirements) { | |
| 107 case protodb::SchedulingParams_BatteryRequirements_BATTERY_INSENSITIVE: | |
| 108 return SchedulingParams::BatteryRequirements::BATTERY_INSENSITIVE; | |
| 109 case protodb::SchedulingParams_BatteryRequirements_BATTERY_SENSITIVE: | |
| 110 return SchedulingParams::BatteryRequirements::BATTERY_SENSITIVE; | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 protodb::SchedulingParams_BatteryRequirements | |
| 115 ProtoConversions::BatteryRequirementsToProto( | |
| 116 SchedulingParams::BatteryRequirements battery_requirements) { | |
| 117 switch (battery_requirements) { | |
| 118 case SchedulingParams::BatteryRequirements::BATTERY_INSENSITIVE: | |
| 119 return protodb::SchedulingParams_BatteryRequirements_BATTERY_INSENSITIVE; | |
| 120 case SchedulingParams::BatteryRequirements::BATTERY_SENSITIVE: | |
| 121 return protodb::SchedulingParams_BatteryRequirements_BATTERY_SENSITIVE; | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 SchedulingParams::Priority ProtoConversions::SchedulingPriorityFromProto( | |
| 126 protodb::SchedulingParams_Priority priority) { | |
| 127 switch (priority) { | |
| 128 case protodb::SchedulingParams_Priority_LOW: | |
| 129 return SchedulingParams::Priority::LOW; | |
| 130 case protodb::SchedulingParams_Priority_NORMAL: | |
| 131 return SchedulingParams::Priority::NORMAL; | |
| 132 case protodb::SchedulingParams_Priority_HIGH: | |
| 133 return SchedulingParams::Priority::HIGH; | |
| 134 case protodb::SchedulingParams_Priority_UI: | |
| 135 return SchedulingParams::Priority::UI; | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 protodb::SchedulingParams_Priority ProtoConversions::SchedulingPriorityToProto( | |
| 140 SchedulingParams::Priority priority) { | |
| 141 switch (priority) { | |
| 142 case SchedulingParams::Priority::LOW: | |
| 143 return protodb::SchedulingParams_Priority_LOW; | |
| 144 case SchedulingParams::Priority::NORMAL: | |
| 145 return protodb::SchedulingParams_Priority_NORMAL; | |
| 146 case SchedulingParams::Priority::HIGH: | |
| 147 return protodb::SchedulingParams_Priority_HIGH; | |
| 148 case SchedulingParams::Priority::UI: | |
| 149 return protodb::SchedulingParams_Priority_UI; | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 SchedulingParams ProtoConversions::SchedulingParamsFromProto( | |
| 154 const protodb::SchedulingParams& proto) { | |
| 155 SchedulingParams scheduling_params; | |
| 156 | |
| 157 scheduling_params.cancel_time = | |
| 158 base::Time::FromInternalValue(proto.cancel_time_utc()); | |
|
David Trainor- moved to gerrit
2017/05/19 19:07:06
Hmm if this is how we store it there and we're goi
shaktisahu
2017/05/20 03:35:58
Done.
| |
| 159 scheduling_params.priority = SchedulingPriorityFromProto(proto.priority()); | |
| 160 scheduling_params.network_requirements = | |
| 161 NetworkRequirementsFromProto(proto.network_requirements()); | |
| 162 scheduling_params.battery_requirements = | |
| 163 BatteryRequirementsFromProto(proto.battery_requirements()); | |
| 164 | |
| 165 return scheduling_params; | |
| 166 } | |
| 167 | |
| 168 void ProtoConversions::SchedulingParamsToProto( | |
| 169 const SchedulingParams& scheduling_params, | |
| 170 protodb::SchedulingParams* proto) { | |
| 171 proto->set_cancel_time_utc(scheduling_params.cancel_time.ToInternalValue()); | |
| 172 proto->set_priority(SchedulingPriorityToProto(scheduling_params.priority)); | |
| 173 proto->set_network_requirements( | |
| 174 NetworkRequirementsToProto(scheduling_params.network_requirements)); | |
| 175 proto->set_battery_requirements( | |
| 176 BatteryRequirementsToProto(scheduling_params.battery_requirements)); | |
| 177 } | |
| 178 | |
| 179 RequestParams ProtoConversions::RequestParamsFromProto( | |
| 180 const protodb::RequestParams& proto) { | |
| 181 RequestParams request_params; | |
| 182 request_params.url = GURL(proto.url()); | |
| 183 request_params.method = proto.method(); | |
| 184 | |
| 185 for (int i = 0; i < proto.headers_size(); i++) { | |
| 186 protodb::RequestHeader header = proto.headers(i); | |
| 187 request_params.request_headers.SetHeader(header.key(), header.value()); | |
| 188 } | |
| 189 | |
| 190 return request_params; | |
| 191 } | |
| 192 | |
| 193 void ProtoConversions::RequestParamsToProto(const RequestParams& request_params, | |
| 194 protodb::RequestParams* proto) { | |
| 195 proto->set_url(request_params.url.spec()); | |
| 196 proto->set_method(request_params.method); | |
| 197 | |
| 198 int i = 0; | |
| 199 net::HttpRequestHeaders::Iterator iter(request_params.request_headers); | |
| 200 while (iter.GetNext()) { | |
| 201 protodb::RequestHeader* header = proto->add_headers(); | |
| 202 header->set_key(iter.name()); | |
| 203 header->set_value(iter.value()); | |
| 204 i++; | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 Entry ProtoConversions::EntryFromProto(const protodb::Entry& proto) { | |
| 209 Entry entry; | |
| 210 | |
| 211 entry.guid = proto.guid(); | |
| 212 entry.client = DownloadClientFromProto(proto.name_space()); | |
| 213 entry.scheduling_params = | |
| 214 SchedulingParamsFromProto(proto.scheduling_params()); | |
| 215 entry.request_params = RequestParamsFromProto(proto.request_params()); | |
| 216 entry.state = RequestStateFromProto(proto.state()); | |
| 217 | |
| 218 return entry; | |
| 219 } | |
| 220 | |
| 221 protodb::Entry ProtoConversions::EntryToProto(const Entry& entry) { | |
| 222 protodb::Entry proto; | |
| 223 | |
| 224 proto.set_guid(entry.guid); | |
| 225 proto.set_name_space(DownloadClientToProto(entry.client)); | |
| 226 SchedulingParamsToProto(entry.scheduling_params, | |
| 227 proto.mutable_scheduling_params()); | |
| 228 RequestParamsToProto(entry.request_params, proto.mutable_request_params()); | |
| 229 proto.set_state(RequestStateToProto(entry.state)); | |
| 230 | |
| 231 return proto; | |
| 232 } | |
| 233 | |
| 234 std::unique_ptr<std::vector<Entry>> ProtoConversions::EntryVectorFromProto( | |
| 235 std::unique_ptr<std::vector<protodb::Entry>> protos) { | |
| 236 auto entries = base::MakeUnique<std::vector<Entry>>(); | |
| 237 for (auto& proto : *protos) { | |
| 238 entries->push_back(EntryFromProto(proto)); | |
| 239 } | |
| 240 | |
| 241 return entries; | |
| 242 } | |
| 243 | |
| 244 std::unique_ptr<std::vector<protodb::Entry>> | |
| 245 ProtoConversions::EntryVectorToProto( | |
| 246 std::unique_ptr<std::vector<Entry>> entries) { | |
| 247 auto protos = base::MakeUnique<std::vector<protodb::Entry>>(); | |
| 248 for (auto& entry : *entries) { | |
| 249 protos->push_back(EntryToProto(entry)); | |
| 250 } | |
| 251 | |
| 252 return protos; | |
| 253 } | |
| 254 | |
| 255 } // namespace download | |
| OLD | NEW |