summaryrefslogtreecommitdiffstats
path: root/chrome/browser/media/router/media_router_dialog_controller.cc
blob: b0f3208a764333afe2e1281f14e813acde3e9bba (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2015 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.

#include "chrome/browser/media/router/media_router_dialog_controller.h"

#include <utility>

#include "chrome/browser/media/router/media_router_metrics.h"
#include "chrome/common/features.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"

#if BUILDFLAG(ANDROID_JAVA_UI)
#include "chrome/browser/media/android/router/media_router_dialog_controller_android.h"
#else
#include "chrome/browser/ui/webui/media_router/media_router_dialog_controller_impl.h"
#endif

namespace media_router {

// static
MediaRouterDialogController*
MediaRouterDialogController::GetOrCreateForWebContents(
    content::WebContents* contents) {
#if BUILDFLAG(ANDROID_JAVA_UI)
  return MediaRouterDialogControllerAndroid::GetOrCreateForWebContents(
      contents);
#else
  return MediaRouterDialogControllerImpl::GetOrCreateForWebContents(contents);
#endif
}

class MediaRouterDialogController::InitiatorWebContentsObserver
    : public content::WebContentsObserver {
 public:
  InitiatorWebContentsObserver(
      content::WebContents* web_contents,
      MediaRouterDialogController* dialog_controller)
      : content::WebContentsObserver(web_contents),
        dialog_controller_(dialog_controller) {
    DCHECK(dialog_controller_);
  }

 private:
  void WebContentsDestroyed() override {
    // NOTE: |this| is deleted after CloseMediaRouterDialog() returns.
    dialog_controller_->CloseMediaRouterDialog();
  }

  void NavigationEntryCommitted(
      const content::LoadCommittedDetails& load_details) override {
    // NOTE: |this| is deleted after CloseMediaRouterDialog() returns.
    dialog_controller_->CloseMediaRouterDialog();
  }

  void RenderProcessGone(base::TerminationStatus status) override {
    // NOTE: |this| is deleted after CloseMediaRouterDialog() returns.
    dialog_controller_->CloseMediaRouterDialog();
  }

  MediaRouterDialogController* const dialog_controller_;
};

MediaRouterDialogController::MediaRouterDialogController(
    content::WebContents* initiator)
    : initiator_(initiator) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(initiator_);
}

MediaRouterDialogController::~MediaRouterDialogController() {
  DCHECK(thread_checker_.CalledOnValidThread());
}

bool MediaRouterDialogController::ShowMediaRouterDialogForPresentation(
    scoped_ptr<CreatePresentationConnectionRequest> request) {
  DCHECK(thread_checker_.CalledOnValidThread());

  // Check if the media router dialog exists for |initiator| and return if so.
  if (IsShowingMediaRouterDialog())
    return false;

  create_connection_request_ = std::move(request);
  initiator_observer_.reset(new InitiatorWebContentsObserver(initiator_, this));
  CreateMediaRouterDialog();

  // Show the initiator holding the existing media router dialog.
  ActivateInitiatorWebContents();

  media_router::MediaRouterMetrics::RecordMediaRouterDialogOrigin(
      MediaRouterDialogOpenOrigin::PAGE);

  return true;
}

bool MediaRouterDialogController::ShowMediaRouterDialog() {
  DCHECK(thread_checker_.CalledOnValidThread());

  // Don't create dialog if it already exists.
  bool dialog_needs_creation = !IsShowingMediaRouterDialog();
  if (dialog_needs_creation) {
    initiator_observer_.reset(
        new InitiatorWebContentsObserver(initiator_, this));
    CreateMediaRouterDialog();
  }

  ActivateInitiatorWebContents();
  return dialog_needs_creation;
}

void MediaRouterDialogController::HideMediaRouterDialog() {
  DCHECK(thread_checker_.CalledOnValidThread());
  CloseMediaRouterDialog();
  Reset();
}

void MediaRouterDialogController::ActivateInitiatorWebContents() {
  initiator_->GetDelegate()->ActivateContents(initiator_);
}

scoped_ptr<CreatePresentationConnectionRequest>
MediaRouterDialogController::TakeCreateConnectionRequest() {
  return std::move(create_connection_request_);
}

void MediaRouterDialogController::Reset() {
  initiator_observer_.reset();
  create_connection_request_.reset();
}

}  // namespace media_router