OLD | NEW |
| (Empty) |
1 // Copyright 2007-2010 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 #include "omaha/net/bits_utils.h" | |
17 | |
18 #include <windows.h> | |
19 #include <winhttp.h> | |
20 #include <cstring> | |
21 #include "omaha/base/debug.h" | |
22 #include "omaha/base/error.h" | |
23 #include "omaha/base/logging.h" | |
24 #include "omaha/base/utils.h" | |
25 #include "omaha/base/scoped_ptr_cotask.h" | |
26 | |
27 namespace omaha { | |
28 | |
29 // Gets the instance of BITS manager. | |
30 HRESULT GetBitsManager(IBackgroundCopyManager** bits_manager) { | |
31 ASSERT1(bits_manager); | |
32 if (*bits_manager) { | |
33 (*bits_manager)->Release(); | |
34 } | |
35 *bits_manager = NULL; | |
36 | |
37 CComPtr<IBackgroundCopyManager> object; | |
38 HRESULT hr = object.CoCreateInstance(__uuidof(BackgroundCopyManager)); | |
39 if (FAILED(hr)) { | |
40 OPT_LOG(LE, (_T("[failed to get BITS interface][0x%08x]"), hr)); | |
41 const HRESULT kServiceDisabled(HRESULT_FROM_WIN32(ERROR_SERVICE_DISABLED)); | |
42 return (hr == kServiceDisabled) ? CI_E_BITS_DISABLED : hr; | |
43 } | |
44 *bits_manager = object.Detach(); | |
45 return S_OK; | |
46 } | |
47 | |
48 bool JobLocalNameEqual::operator()(IBackgroundCopyJob* job, | |
49 const TCHAR* name) const { | |
50 ASSERT1(job); | |
51 ASSERT1(name); | |
52 CComPtr<IEnumBackgroundCopyFiles> files; | |
53 if (FAILED(job->EnumFiles(&files))) { | |
54 return false; | |
55 } | |
56 ULONG file_count = 0; | |
57 if (FAILED(files->GetCount(&file_count))) { | |
58 return false; | |
59 } | |
60 for (size_t i = 0; i != file_count; ++i) { | |
61 CComPtr<IBackgroundCopyFile> file; | |
62 if (files->Next(1, &file, NULL) == S_OK) { | |
63 scoped_ptr_cotask<TCHAR> local_name; | |
64 if (SUCCEEDED(file->GetLocalName(address(local_name)))) { | |
65 if (_tcscmp(local_name.get(), name) == 0) { | |
66 return true; | |
67 } | |
68 } | |
69 } | |
70 } | |
71 return false; | |
72 } | |
73 | |
74 bool JobDisplayNameEqual::operator()(IBackgroundCopyJob* job, | |
75 const TCHAR* name) const { | |
76 ASSERT1(job); | |
77 ASSERT1(name); | |
78 scoped_ptr_cotask<TCHAR> display_name; | |
79 HRESULT hr = job->GetDisplayName(address(display_name)); | |
80 if (SUCCEEDED(hr)) { | |
81 return _tcscmp(display_name.get(), name) == 0; | |
82 } | |
83 return false; | |
84 } | |
85 | |
86 HRESULT SetProxyAuthImplicitCredentials(IBackgroundCopyJob* job, | |
87 BG_AUTH_SCHEME auth_scheme) { | |
88 return SetProxyAuthCredentials(job, NULL, NULL, auth_scheme); | |
89 } | |
90 | |
91 HRESULT SetProxyAuthCredentials(IBackgroundCopyJob* job, | |
92 TCHAR* username, | |
93 TCHAR* password, | |
94 BG_AUTH_SCHEME auth_scheme) { | |
95 ASSERT1(job); | |
96 CComQIPtr<IBackgroundCopyJob2> job2(job); | |
97 if (!job2) { | |
98 return E_NOINTERFACE; | |
99 } | |
100 BG_AUTH_CREDENTIALS auth_cred; | |
101 SetZero(auth_cred); | |
102 auth_cred.Target = BG_AUTH_TARGET_PROXY; | |
103 auth_cred.Scheme = auth_scheme; | |
104 auth_cred.Credentials.Basic.UserName = username; | |
105 auth_cred.Credentials.Basic.Password = password; | |
106 return job2->SetCredentials(&auth_cred); | |
107 } | |
108 | |
109 int GetHttpStatusFromBitsError(HRESULT error) { | |
110 // Bits errors are defined in bitsmsg.h. Although not documented, it is | |
111 // clear that all errors corresponding to http status code have the high | |
112 // word equal to 0x8019. | |
113 bool is_valid = HIWORD(error) == 0x8019 && | |
114 LOWORD(error) >= HTTP_STATUS_FIRST && | |
115 LOWORD(error) <= HTTP_STATUS_LAST; | |
116 return is_valid ? LOWORD(error) : 0; | |
117 } | |
118 | |
119 HRESULT CancelBitsJob(IBackgroundCopyJob* job) { | |
120 if (job) { | |
121 BG_JOB_STATE job_state = BG_JOB_STATE_ERROR; | |
122 HRESULT hr = job->GetState(&job_state); | |
123 if (SUCCEEDED(hr) && | |
124 job_state != BG_JOB_STATE_CANCELLED && | |
125 job_state != BG_JOB_STATE_ACKNOWLEDGED) { | |
126 HRESULT hr = job->Cancel(); | |
127 if (FAILED(hr)) { | |
128 NET_LOG(LW, (_T("[CancelBitsJob failed][0x%08x]"), hr)); | |
129 } | |
130 return hr; | |
131 } | |
132 } | |
133 return S_OK; | |
134 } | |
135 | |
136 HRESULT PauseBitsJob(IBackgroundCopyJob* job) { | |
137 if (job) { | |
138 BG_JOB_STATE job_state = BG_JOB_STATE_ERROR; | |
139 HRESULT hr = job->GetState(&job_state); | |
140 if (SUCCEEDED(hr) && | |
141 job_state != BG_JOB_STATE_TRANSFERRED && | |
142 job_state != BG_JOB_STATE_ACKNOWLEDGED && | |
143 job_state != BG_JOB_STATE_CANCELLED) { | |
144 HRESULT hr = job->Suspend(); | |
145 if (FAILED(hr)) { | |
146 NET_LOG(LW, (_T("[PauseBitsJob failed][0x%08x]"), hr)); | |
147 } | |
148 return hr; | |
149 } | |
150 } | |
151 return S_OK; | |
152 } | |
153 | |
154 HRESULT ResumeBitsJob(IBackgroundCopyJob* job) { | |
155 if (job) { | |
156 BG_JOB_STATE job_state = BG_JOB_STATE_ERROR; | |
157 HRESULT hr = job->GetState(&job_state); | |
158 if (SUCCEEDED(hr) && job_state == BG_JOB_STATE_SUSPENDED) { | |
159 HRESULT hr = job->Suspend(); | |
160 if (FAILED(hr)) { | |
161 NET_LOG(LW, (_T("[ResumeBitsJob failed][0x%08x]"), hr)); | |
162 } | |
163 return hr; | |
164 } | |
165 } | |
166 return S_OK; | |
167 } | |
168 | |
169 #define RETURN_TSTR(x) case (x): return _T(#x) | |
170 CString JobStateToString(BG_JOB_STATE job_state) { | |
171 switch (job_state) { | |
172 RETURN_TSTR(BG_JOB_STATE_QUEUED); | |
173 RETURN_TSTR(BG_JOB_STATE_CONNECTING); | |
174 RETURN_TSTR(BG_JOB_STATE_TRANSFERRING); | |
175 RETURN_TSTR(BG_JOB_STATE_TRANSIENT_ERROR); | |
176 RETURN_TSTR(BG_JOB_STATE_ERROR); | |
177 RETURN_TSTR(BG_JOB_STATE_TRANSFERRED); | |
178 RETURN_TSTR(BG_JOB_STATE_SUSPENDED); | |
179 RETURN_TSTR(BG_JOB_STATE_ACKNOWLEDGED); | |
180 RETURN_TSTR(BG_JOB_STATE_CANCELLED); | |
181 } | |
182 return _T(""); | |
183 } | |
184 | |
185 CString BitsAuthSchemeToString(int auth_scheme) { | |
186 switch (auth_scheme) { | |
187 RETURN_TSTR(BG_AUTH_SCHEME_NEGOTIATE); | |
188 RETURN_TSTR(BG_AUTH_SCHEME_NTLM); | |
189 RETURN_TSTR(BG_AUTH_SCHEME_DIGEST); | |
190 RETURN_TSTR(BG_AUTH_SCHEME_BASIC); | |
191 RETURN_TSTR(0); | |
192 } | |
193 return _T(""); | |
194 } | |
195 | |
196 } // namespace omaha | |
197 | |
OLD | NEW |