1 // Copyright (c) 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.
5 #include "chrome/browser/extensions/api/identity/identity_mint_queue.h"
7 #include "base/logging.h"
8 #include "base/stl_util.h"
9 #include "base/trace_event/trace_event.h"
11 namespace extensions
{
13 IdentityMintRequestQueue::IdentityMintRequestQueue() {
16 IdentityMintRequestQueue::~IdentityMintRequestQueue() {
17 for (RequestQueueMap::const_iterator
18 it
= interactive_request_queue_map_
.begin();
19 it
!= interactive_request_queue_map_
.end();
21 DCHECK_EQ(it
->second
.size(), 0lu);
23 for (RequestQueueMap::const_iterator
24 it
= noninteractive_request_queue_map_
.begin();
25 it
!= noninteractive_request_queue_map_
.end();
27 DCHECK_EQ(it
->second
.size(), 0lu);
31 void IdentityMintRequestQueue::RequestStart(
32 IdentityMintRequestQueue::MintType type
,
33 const ExtensionTokenKey
& key
,
34 IdentityMintRequestQueue::Request
* request
) {
35 TRACE_EVENT_ASYNC_BEGIN1(
36 "identity", "IdentityMintRequestQueue", request
, "type", type
);
37 RequestQueue
& request_queue
= GetRequestQueueMap(type
)[key
];
38 request_queue
.push_back(request
);
39 // If this is the first request, start it now. RequestComplete will start
40 // all other requests.
41 if (request_queue
.size() == 1)
42 RunRequest(type
, request_queue
);
45 void IdentityMintRequestQueue::RequestComplete(
46 IdentityMintRequestQueue::MintType type
,
47 const ExtensionTokenKey
& key
,
48 IdentityMintRequestQueue::Request
* request
) {
49 TRACE_EVENT_ASYNC_END1("identity",
50 "IdentityMintRequestQueue",
54 RequestQueue
& request_queue
= GetRequestQueueMap(type
)[key
];
55 CHECK_EQ(request_queue
.front(), request
);
56 request_queue
.pop_front();
57 if (request_queue
.size() > 0)
58 RunRequest(type
, request_queue
);
61 void IdentityMintRequestQueue::RequestCancel(
62 const ExtensionTokenKey
& key
,
63 IdentityMintRequestQueue::Request
* request
) {
64 TRACE_EVENT_ASYNC_END1("identity",
65 "IdentityMintRequestQueue",
69 GetRequestQueueMap(MINT_TYPE_INTERACTIVE
)[key
].remove(request
);
70 GetRequestQueueMap(MINT_TYPE_NONINTERACTIVE
)[key
].remove(request
);
73 bool IdentityMintRequestQueue::empty(IdentityMintRequestQueue::MintType type
,
74 const ExtensionTokenKey
& key
) {
75 RequestQueueMap
& request_queue_map
= GetRequestQueueMap(type
);
76 return !ContainsKey(request_queue_map
, key
) ||
77 (request_queue_map
.find(key
))->second
.empty();
80 IdentityMintRequestQueue::RequestQueueMap
&
81 IdentityMintRequestQueue::GetRequestQueueMap(
82 IdentityMintRequestQueue::MintType type
) {
83 return (type
== MINT_TYPE_INTERACTIVE
) ? interactive_request_queue_map_
84 : noninteractive_request_queue_map_
;
87 void IdentityMintRequestQueue::RunRequest(
88 IdentityMintRequestQueue::MintType type
,
89 RequestQueue
& request_queue
) {
90 TRACE_EVENT_ASYNC_STEP_INTO0(
91 "identity", "IdentityMintRequestQueue", request_queue
.front(), "RUNNING");
92 request_queue
.front()->StartMintToken(type
);
95 } // namespace extensions