Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / media / router / media_router_dialog_controller.cc
blob9efe8b98f0c2fb49839eb762bfc925d6a82833c0
1 // Copyright 2015 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.
5 #include "chrome/browser/media/router/media_router_dialog_controller.h"
7 #include "content/public/browser/browser_thread.h"
8 #include "content/public/browser/web_contents.h"
9 #include "content/public/browser/web_contents_delegate.h"
11 #if defined(OS_ANDROID)
12 #include "chrome/browser/media/android/router/media_router_dialog_controller_android.h"
13 #else
14 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller_impl.h"
15 #endif
17 namespace media_router {
19 // static
20 MediaRouterDialogController*
21 MediaRouterDialogController::GetOrCreateForWebContents(
22 content::WebContents* contents) {
23 #if defined(OS_ANDROID)
24 return MediaRouterDialogControllerAndroid::GetOrCreateForWebContents(
25 contents);
26 #else
27 return MediaRouterDialogControllerImpl::GetOrCreateForWebContents(contents);
28 #endif
31 class MediaRouterDialogController::InitiatorWebContentsObserver
32 : public content::WebContentsObserver {
33 public:
34 InitiatorWebContentsObserver(
35 content::WebContents* web_contents,
36 MediaRouterDialogController* dialog_controller)
37 : content::WebContentsObserver(web_contents),
38 dialog_controller_(dialog_controller) {
39 DCHECK(dialog_controller_);
42 private:
43 void WebContentsDestroyed() override {
44 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns.
45 dialog_controller_->CloseMediaRouterDialog();
48 void NavigationEntryCommitted(
49 const content::LoadCommittedDetails& load_details) override {
50 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns.
51 dialog_controller_->CloseMediaRouterDialog();
54 void RenderProcessGone(base::TerminationStatus status) override {
55 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns.
56 dialog_controller_->CloseMediaRouterDialog();
59 MediaRouterDialogController* const dialog_controller_;
62 MediaRouterDialogController::MediaRouterDialogController(
63 content::WebContents* initiator)
64 : initiator_(initiator) {
65 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
66 DCHECK(initiator_);
67 initiator_observer_.reset(new InitiatorWebContentsObserver(initiator_, this));
70 MediaRouterDialogController::~MediaRouterDialogController() {
71 DCHECK(thread_checker_.CalledOnValidThread());
74 bool MediaRouterDialogController::ShowMediaRouterDialogForPresentation(
75 scoped_ptr<CreatePresentationSessionRequest> request) {
76 DCHECK(thread_checker_.CalledOnValidThread());
78 // Check if the media router dialog exists for |initiator| and return if so.
79 if (IsShowingMediaRouterDialog())
80 return false;
82 presentation_request_ = request.Pass();
83 CreateMediaRouterDialog();
85 // Show the initiator holding the existing media router dialog.
86 ActivateInitiatorWebContents();
88 return true;
91 bool MediaRouterDialogController::ShowMediaRouterDialog() {
92 DCHECK(thread_checker_.CalledOnValidThread());
94 // Don't create dialog if it already exists.
95 bool dialog_needs_creation = !IsShowingMediaRouterDialog();
96 if (dialog_needs_creation)
97 CreateMediaRouterDialog();
99 ActivateInitiatorWebContents();
100 return dialog_needs_creation;
103 void MediaRouterDialogController::HideMediaRouterDialog() {
104 DCHECK(thread_checker_.CalledOnValidThread());
105 CloseMediaRouterDialog();
106 Reset();
109 void MediaRouterDialogController::ActivateInitiatorWebContents() {
110 initiator_->GetDelegate()->ActivateContents(initiator_);
113 scoped_ptr<CreatePresentationSessionRequest>
114 MediaRouterDialogController::TakePresentationRequest() {
115 return presentation_request_.Pass();
118 void MediaRouterDialogController::Reset() {
119 initiator_observer_.reset();
120 presentation_request_.reset();
123 } // namespace media_router