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#ifndef CHROME_BROWSER_MEDIA_MIDI_PERMISSION_CONTEXT_H_
6#define CHROME_BROWSER_MEDIA_MIDI_PERMISSION_CONTEXT_H_
7
8#include "base/containers/scoped_ptr_hash_map.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/memory/weak_ptr.h"
11#include "components/keyed_service/core/keyed_service.h"
12
13namespace content {
14class WebContents;
15}
16
17class GURL;
18class MidiPermissionRequest;
19class PermissionQueueController;
20class PermissionRequestID;
21class Profile;
22
23// This class manages MIDI permissions flow. Used on the UI thread.
24class MidiPermissionContext : public KeyedService {
25 public:
26  explicit MidiPermissionContext(Profile* profile);
27  virtual ~MidiPermissionContext();
28
29  // KeyedService methods:
30  virtual void Shutdown() OVERRIDE;
31
32  // Request to ask users permission about MIDI.
33  void RequestMidiSysExPermission(
34      content::WebContents* web_contents,
35      int bridge_id,
36      const GURL& requesting_frame,
37      bool user_gesture,
38      const base::Callback<void(bool)>& result_callback,
39      base::Closure* cancel_callback);
40
41  // Called when the permission decision is made. If a permissions prompt is
42  // shown to the user it will be called when the user selects an option
43  // from that prompt.
44  void NotifyPermissionSet(
45      const PermissionRequestID& id,
46      const GURL& requesting_frame,
47      const base::Callback<void(bool)>& callback,
48      bool allowed);
49
50 private:
51  friend class MidiPermissionRequest;
52
53  // Cancel a pending MIDI permission request.
54  void CancelMidiSysExPermissionRequest(int render_process_id,
55                                        int render_view_id,
56                                        int bridge_id);
57
58  // Decide whether the permission should be granted.
59  // Calls PermissionDecided if permission can be decided non-interactively,
60  // or NotifyPermissionSet if permission decided by presenting an infobar.
61  void DecidePermission(
62      content::WebContents* web_contents,
63      const PermissionRequestID& id,
64      const GURL& requesting_frame,
65      const GURL& embedder,
66      bool user_gesture,
67      const base::Callback<void(bool)>& callback);
68
69  // Called when permission is granted without interactively asking the user.
70  void PermissionDecided(
71      const PermissionRequestID& id,
72      const GURL& requesting_frame,
73      const GURL& embedder,
74      const base::Callback<void(bool)>& callback,
75      bool allowed);
76
77  // Return an instance of the infobar queue controller, creating it if needed.
78  PermissionQueueController* GetQueueController();
79
80  // Removes any pending InfoBar request.
81  void CancelPendingInfobarRequest(const PermissionRequestID& id);
82
83  // Notify the context that a particular request object is no longer needed.
84  void RequestFinished(MidiPermissionRequest* request);
85
86  Profile* const profile_;
87  bool shutting_down_;
88  scoped_ptr<PermissionQueueController> permission_queue_controller_;
89
90  base::ScopedPtrHashMap<std::string, MidiPermissionRequest> pending_requests_;
91
92  base::WeakPtrFactory<MidiPermissionContext> weak_factory_;
93
94  DISALLOW_COPY_AND_ASSIGN(MidiPermissionContext);
95};
96
97#endif  // CHROME_BROWSER_MEDIA_MIDI_PERMISSION_CONTEXT_H_
98