1// Copyright (c) 2012 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#ifndef CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_MINT_QUEUE_H_
6#define CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_MINT_QUEUE_H_
7
8#include <list>
9#include <map>
10#include <set>
11#include <string>
12
13#include "chrome/browser/extensions/api/identity/extension_token_key.h"
14
15namespace extensions {
16
17// getAuthToken requests are serialized to avoid excessive traffic to
18// GAIA and to consolidate UI pop-ups. IdentityMintRequestQueue
19// maitains a set of queues, one for each RequestKey.
20//
21// The queue calls StartMintToken on each Request when it reaches the
22// head of the line.
23//
24// The queue does not own Requests. Request pointers must be valid
25// until they are removed from the queue with RequestComplete or
26// RequestCancel.
27class IdentityMintRequestQueue {
28 public:
29  enum MintType {
30    MINT_TYPE_NONINTERACTIVE,
31    MINT_TYPE_INTERACTIVE
32  };
33
34  IdentityMintRequestQueue();
35  virtual ~IdentityMintRequestQueue();
36
37  class Request {
38   public:
39    virtual ~Request() {}
40    virtual void StartMintToken(IdentityMintRequestQueue::MintType type) = 0;
41  };
42
43  // Adds a request to the queue specified by the token key.
44  void RequestStart(IdentityMintRequestQueue::MintType type,
45                    const ExtensionTokenKey& key,
46                    IdentityMintRequestQueue::Request* request);
47  // Removes a request from the queue specified by the token key.
48  void RequestComplete(IdentityMintRequestQueue::MintType type,
49                       const ExtensionTokenKey& key,
50                       IdentityMintRequestQueue::Request* request);
51  // Cancels a request. OK to call if |request| is not queued.
52  // Does *not* start a new request, even if the canceled request is at
53  // the head of the queue.
54  void RequestCancel(const ExtensionTokenKey& key,
55                     IdentityMintRequestQueue::Request* request);
56  bool empty(IdentityMintRequestQueue::MintType type,
57             const ExtensionTokenKey& key);
58
59 private:
60  typedef std::list<IdentityMintRequestQueue::Request*> RequestQueue;
61  typedef std::map<const ExtensionTokenKey, RequestQueue> RequestQueueMap;
62
63  RequestQueueMap& GetRequestQueueMap(IdentityMintRequestQueue::MintType type);
64  void RunRequest(IdentityMintRequestQueue::MintType type,
65                  RequestQueue& request_queue);
66
67  RequestQueueMap interactive_request_queue_map_;
68  RequestQueueMap noninteractive_request_queue_map_;
69};
70
71}  // namespace extensions
72
73#endif  // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_MINT_QUEUE_H_
74