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/chromeos/drive/job_list.h" 6 7 #include "base/logging.h" 8 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/stringprintf.h" 10 11 namespace drive { 12 13 std::string JobTypeToString(JobType type) { 14 switch (type){ 15 case TYPE_GET_ABOUT_RESOURCE: 16 return "TYPE_GET_ABOUT_RESOURCE"; 17 case TYPE_GET_APP_LIST: 18 return "TYPE_GET_APP_LIST"; 19 case TYPE_GET_ALL_RESOURCE_LIST: 20 return "TYPE_GET_ALL_RESOURCE_LIST"; 21 case TYPE_GET_RESOURCE_LIST_IN_DIRECTORY: 22 return "TYPE_GET_RESOURCE_LIST_IN_DIRECTORY"; 23 case TYPE_SEARCH: 24 return "TYPE_SEARCH"; 25 case TYPE_GET_CHANGE_LIST: 26 return "TYPE_GET_CHANGE_LIST"; 27 case TYPE_GET_REMAINING_CHANGE_LIST: 28 return "TYPE_GET_REMAINING_CHANGE_LIST"; 29 case TYPE_GET_REMAINING_FILE_LIST: 30 return "TYPE_GET_REMAINING_FILE_LIST"; 31 case TYPE_GET_RESOURCE_ENTRY: 32 return "TYPE_GET_RESOURCE_ENTRY"; 33 case TYPE_GET_SHARE_URL: 34 return "TYPE_GET_SHARE_URL"; 35 case TYPE_TRASH_RESOURCE: 36 return "TYPE_TRASH_RESOURCE"; 37 case TYPE_COPY_RESOURCE: 38 return "TYPE_COPY_RESOURCE"; 39 case TYPE_UPDATE_RESOURCE: 40 return "TYPE_UPDATE_RESOURCE"; 41 case TYPE_RENAME_RESOURCE: 42 return "TYPE_RENAME_RESOURCE"; 43 case TYPE_ADD_RESOURCE_TO_DIRECTORY: 44 return "TYPE_ADD_RESOURCE_TO_DIRECTORY"; 45 case TYPE_REMOVE_RESOURCE_FROM_DIRECTORY: 46 return "TYPE_REMOVE_RESOURCE_FROM_DIRECTORY"; 47 case TYPE_ADD_NEW_DIRECTORY: 48 return "TYPE_ADD_NEW_DIRECTORY"; 49 case TYPE_DOWNLOAD_FILE: 50 return "TYPE_DOWNLOAD_FILE"; 51 case TYPE_UPLOAD_NEW_FILE: 52 return "TYPE_UPLOAD_NEW_FILE"; 53 case TYPE_UPLOAD_EXISTING_FILE: 54 return "TYPE_UPLOAD_EXISTING_FILE"; 55 case TYPE_CREATE_FILE: 56 return "TYPE_CREATE_FILE"; 57 case TYPE_GET_RESOURCE_LIST_IN_DIRECTORY_BY_WAPI: 58 return "TYPE_GET_RESOURCE_LIST_IN_DIRECTORY_BY_WAPI"; 59 case TYPE_GET_REMAINING_RESOURCE_LIST: 60 return "TYPE_GET_REMAINING_RESOURCE_LIST"; 61 } 62 NOTREACHED(); 63 return "(unknown job type)"; 64 } 65 66 std::string JobStateToString(JobState state) { 67 switch (state) { 68 case STATE_NONE: 69 return "STATE_NONE"; 70 case STATE_RUNNING: 71 return "STATE_RUNNING"; 72 case STATE_RETRY: 73 return "STATE_RETRY"; 74 } 75 NOTREACHED(); 76 return "(unknown job state)"; 77 } 78 79 JobInfo::JobInfo(JobType in_job_type) 80 : job_type(in_job_type), 81 job_id(-1), 82 state(STATE_NONE), 83 num_completed_bytes(0), 84 num_total_bytes(0) { 85 } 86 87 std::string JobInfo::ToString() const { 88 std::string output = base::StringPrintf( 89 "%s %s [%d]", 90 JobTypeToString(job_type).c_str(), 91 JobStateToString(state).c_str(), 92 job_id); 93 if (job_type == TYPE_DOWNLOAD_FILE || 94 job_type == TYPE_UPLOAD_NEW_FILE || 95 job_type == TYPE_UPLOAD_EXISTING_FILE) { 96 base::StringAppendF(&output, 97 " bytes: %s/%s", 98 base::Int64ToString(num_completed_bytes).c_str(), 99 base::Int64ToString(num_total_bytes).c_str()); 100 } 101 return output; 102 } 103 104 bool IsActiveFileTransferJobInfo(const JobInfo& job_info) { 105 // Using switch statement so that compiler can warn when new states or types 106 // are added. 107 switch (job_info.state) { 108 case STATE_NONE: 109 return false; 110 case STATE_RUNNING: 111 case STATE_RETRY: 112 break; 113 } 114 115 switch (job_info.job_type) { 116 case TYPE_GET_ABOUT_RESOURCE: 117 case TYPE_GET_APP_LIST: 118 case TYPE_GET_ALL_RESOURCE_LIST: 119 case TYPE_GET_RESOURCE_LIST_IN_DIRECTORY: 120 case TYPE_SEARCH: 121 case TYPE_GET_CHANGE_LIST: 122 case TYPE_GET_REMAINING_CHANGE_LIST: 123 case TYPE_GET_REMAINING_FILE_LIST: 124 case TYPE_GET_RESOURCE_ENTRY: 125 case TYPE_GET_SHARE_URL: 126 case TYPE_TRASH_RESOURCE: 127 case TYPE_COPY_RESOURCE: 128 case TYPE_UPDATE_RESOURCE: 129 case TYPE_RENAME_RESOURCE: 130 case TYPE_ADD_RESOURCE_TO_DIRECTORY: 131 case TYPE_REMOVE_RESOURCE_FROM_DIRECTORY: 132 case TYPE_ADD_NEW_DIRECTORY: 133 case TYPE_CREATE_FILE: 134 case TYPE_GET_RESOURCE_LIST_IN_DIRECTORY_BY_WAPI: 135 case TYPE_GET_REMAINING_RESOURCE_LIST: 136 return false; 137 case TYPE_DOWNLOAD_FILE: 138 case TYPE_UPLOAD_NEW_FILE: 139 case TYPE_UPLOAD_EXISTING_FILE: 140 break; 141 } 142 143 return true; 144 } 145 146 } // namespace drive 147