1 // Copyright 2020 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_ACCESSIBILITY_CAPTION_HOST_IMPL_H_
6 #define CHROME_BROWSER_ACCESSIBILITY_CAPTION_HOST_IMPL_H_
7 
8 #include <string>
9 
10 #include "chrome/common/caption.mojom.h"
11 #include "content/public/browser/web_contents_observer.h"
12 #include "mojo/public/cpp/bindings/pending_receiver.h"
13 
14 namespace content {
15 class RenderFrameHost;
16 }
17 
18 namespace captions {
19 
20 class CaptionController;
21 
22 ///////////////////////////////////////////////////////////////////////////////
23 // Caption Host Impl
24 //
25 //  A class that implements the Mojo interface CaptionHost. There exists one
26 //  CaptionHostImpl per render frame.
27 //
28 class CaptionHostImpl : public chrome::mojom::CaptionHost,
29                         public content::WebContentsObserver {
30  public:
31   explicit CaptionHostImpl(content::RenderFrameHost* frame_host);
32   CaptionHostImpl(const CaptionHostImpl&) = delete;
33   CaptionHostImpl& operator=(const CaptionHostImpl&) = delete;
34   ~CaptionHostImpl() override;
35 
36   // static
37   static void Create(
38       content::RenderFrameHost* frame_host,
39       mojo::PendingReceiver<chrome::mojom::CaptionHost> receiver);
40 
41   // chrome::mojom::CaptionHost:
42   void OnTranscription(
43       chrome::mojom::TranscriptionResultPtr transcription_result,
44       OnTranscriptionCallback reply) override;
45   void OnError() override;
46 
47   // content::WebContentsObserver:
48   void RenderFrameDeleted(content::RenderFrameHost* frame_host) override;
49 
50  private:
51   // Returns the WebContents if it exists. If it does not exist, sets the
52   // RenderFrameHost reference to nullptr and returns nullptr.
53   content::WebContents* GetWebContents();
54 
55   // Returns the CaptionController for this WebContents. Returns nullptr if
56   // it does not exist.
57   CaptionController* GetCaptionController(content::WebContents*);
58 
59   content::RenderFrameHost* frame_host_;
60 };
61 
62 }  // namespace captions
63 
64 #endif  // CHROME_BROWSER_ACCESSIBILITY_CAPTION_HOST_IMPL_H_
65