xref: /haiku/src/kits/package/Request.cpp (revision 21258e2674226d6aa732321b6f8494841895af5f)
1 /*
2  * Copyright 2011-2015, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Oliver Tappe <zooey@hirschkaefer.de>
7  */
8 
9 
10 #include <package/Request.h>
11 
12 #include <new>
13 
14 #include <JobQueue.h>
15 
16 #include <package/Context.h>
17 
18 
19 namespace BPackageKit {
20 
21 
22 using BSupportKit::BPrivate::JobQueue;
23 
24 
25 BRequest::BRequest(const BContext& context)
26 	:
27 	fContext(context),
28 	fJobQueue(new (std::nothrow) JobQueue())
29 {
30 	fInitStatus = fJobQueue == NULL ? B_NO_MEMORY : B_OK;
31 }
32 
33 
34 BRequest::~BRequest()
35 {
36 }
37 
38 
39 status_t
40 BRequest::InitCheck() const
41 {
42 	return fInitStatus;
43 }
44 
45 
46 BSupportKit::BJob*
47 BRequest::PopRunnableJob()
48 {
49 	if (fJobQueue == NULL)
50 		return NULL;
51 
52 	return fJobQueue->Pop();
53 }
54 
55 
56 status_t
57 BRequest::Process(bool failIfCanceledOnly)
58 {
59 	status_t error = InitCheck();
60 	if (error != B_OK)
61 		return error;
62 
63 	error = CreateInitialJobs();
64 	if (error != B_OK)
65 		return error;
66 
67 	while (BSupportKit::BJob* job = PopRunnableJob()) {
68 		error = job->Run();
69 		delete job;
70 		if (error != B_OK) {
71 			if (!failIfCanceledOnly || error == B_CANCELED)
72 				return error;
73 		}
74 	}
75 
76 	return B_OK;
77 }
78 
79 
80 status_t
81 BRequest::QueueJob(BSupportKit::BJob* job)
82 {
83 	if (fJobQueue == NULL)
84 		return B_NO_INIT;
85 
86 	job->AddStateListener(this);
87 	job->AddStateListener(&fContext.JobStateListener());
88 
89 	return fJobQueue->AddJob(job);
90 }
91 
92 
93 }	// namespace BPackageKit
94