summaryrefslogtreecommitdiff
path: root/chromium/content/browser/renderer_host/media/web_contents_tracker.h
blob: 632ced28be7a2e532c4676c292c2702313efba67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Given a starting render_process_id and render_view_id, the WebContentsTracker
// tracks RenderViewHost instance swapping during the lifetime of a WebContents
// instance.  This is used when mirroring tab video and audio so that user
// navigations, crashes, etc., during a tab's lifetime allow the capturing code
// to remain active on the current/latest RenderView.
//
// Threading issues: Start(), Stop() and the ChangeCallback are invoked on the
// same thread.  This can be any thread, and the decision is locked-in by
// WebContentsTracker when Start() is called.

#ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_WEB_CONTENTS_TRACKER_H_
#define CONTENT_BROWSER_RENDERER_HOST_MEDIA_WEB_CONTENTS_TRACKER_H_

#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "content/common/content_export.h"
#include "content/public/browser/web_contents_observer.h"

namespace base {
class MessageLoopProxy;
}

namespace content {

class CONTENT_EXPORT WebContentsTracker
    : public base::RefCountedThreadSafe<WebContentsTracker>,
      public WebContentsObserver {
 public:
  WebContentsTracker();

  // Callback for whenever the target is swapped.  The callback is also invoked
  // with both arguments set to MSG_ROUTING_NONE to indicate tracking will not
  // continue (i.e., the WebContents instance was not found or has been
  // destroyed).
  typedef base::Callback<void(int render_process_id, int render_view_id)>
      ChangeCallback;

  // Start tracking.  The last-known |render_process_id| and |render_view_id|
  // are provided, and the given callback is invoked asynchronously one or more
  // times.  The callback will be invoked on the same thread calling Start().
  virtual void Start(int render_process_id, int render_view_id,
                     const ChangeCallback& callback);

  // Stop tracking.  Once this method returns, the callback is guaranteed not to
  // be invoked again.
  virtual void Stop();

 protected:
  friend class base::RefCountedThreadSafe<WebContentsTracker>;
  virtual ~WebContentsTracker();

 private:
  // Reads the render_process_id/render_view_id from the current WebContents
  // instance and then invokes the callback.
  void OnWebContentsChangeEvent();

  // Called on the thread that Start()/Stop() are called on, check whether the
  // callback is still valid and, if so, invoke it.
  void MaybeDoCallback(int render_process_id, int render_view_id);

  // Look-up the current WebContents instance associated with the given
  // |render_process_id| and |render_view_id| and begin observing it.
  void LookUpAndObserveWebContents(int render_process_id,
                                   int render_view_id);

  // WebContentsObserver overrides to react to events of interest.
  virtual void RenderViewReady() OVERRIDE;
  virtual void AboutToNavigateRenderView(RenderViewHost* render_view_host)
      OVERRIDE;
  virtual void DidNavigateMainFrame(const LoadCommittedDetails& details,
                                    const FrameNavigateParams& params) OVERRIDE;
  virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE;

  scoped_refptr<base::MessageLoopProxy> message_loop_;
  ChangeCallback callback_;

  DISALLOW_COPY_AND_ASSIGN(WebContentsTracker);
};

}  // namespace content

#endif  // CONTENT_BROWSER_RENDERER_HOST_MEDIA_WEB_CONTENTS_TRACKER_H_