summaryrefslogtreecommitdiffstats
path: root/chromecast/browser/android/cast_window_android.cc
blob: 1fe64a879aee6361a395e4a9964935957fe00c90 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2014 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 "chromecast/browser/android/cast_window_android.h"

#include "base/message_loop/message_loop_proxy.h"
#include "chromecast/browser/android/cast_window_manager.h"
#include "chromecast/browser/cast_content_window.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/common/renderer_preferences.h"
#include "jni/CastWindowAndroid_jni.h"
#include "ui/gfx/skia_util.h"

namespace chromecast {
namespace shell {

namespace {

// The time (in milliseconds) we wait for after a page is closed (i.e.
// after an app is stopped) before we delete the corresponding WebContents.
const int kWebContentsDestructionDelayInMs = 50;

}  // namespace

// static
bool CastWindowAndroid::RegisterJni(JNIEnv* env) {
  return RegisterNativesImpl(env);
}

// static
CastWindowAndroid* CastWindowAndroid::CreateNewWindow(
    content::BrowserContext* browser_context,
    const GURL& url) {
  CastWindowAndroid* window_android = new CastWindowAndroid(browser_context);
  window_android->Initialize();

  if (!url.is_empty())
    window_android->LoadURL(url);

  content::RenderWidgetHostView* rwhv =
      window_android->web_contents_->GetRenderWidgetHostView();
  if (rwhv) {
    rwhv->SetBackgroundColor(SK_ColorBLACK);
  }

  return window_android;
}

CastWindowAndroid::CastWindowAndroid(content::BrowserContext* browser_context)
    : browser_context_(browser_context),
      content_window_(new CastContentWindow),
      weak_factory_(this) {
}

void CastWindowAndroid::Initialize() {
  web_contents_ =
      content_window_->CreateWebContents(gfx::Size(), browser_context_);
  web_contents_->SetDelegate(this);
  content::WebContentsObserver::Observe(web_contents_.get());

  JNIEnv* env = base::android::AttachCurrentThread();
  window_java_.Reset(CreateCastWindowView(this));

  Java_CastWindowAndroid_initFromNativeWebContents(
      env, window_java_.obj(), web_contents_->GetJavaWebContents().obj(),
      web_contents_->GetRenderProcessHost()->GetID());

  // Enabling hole-punching also requires runtime renderer preference
  content::RendererPreferences* prefs =
      web_contents_->GetMutableRendererPrefs();
  prefs->use_video_overlay_for_embedded_encrypted_video = true;
  prefs->use_view_overlay_for_all_video = true;
  web_contents_->GetRenderViewHost()->SyncRendererPrefs();
}

CastWindowAndroid::~CastWindowAndroid() {
}

void CastWindowAndroid::Close() {
  // Close page first, which fires the window.unload event. The WebContents
  // itself will be destroyed after browser-process has received renderer
  // notification that the page is closed.
  web_contents_->GetRenderViewHost()->ClosePage();
}

void CastWindowAndroid::Destroy() {
  // Note: if multiple windows becomes supported, this may close other devtools
  // sessions.
  content::DevToolsAgentHost::DetachAllClients();
  CloseCastWindowView(window_java_.obj());
  delete this;
}

void CastWindowAndroid::LoadURL(const GURL& url) {
  content::NavigationController::LoadURLParams params(url);
  params.transition_type = ui::PageTransitionFromInt(
      ui::PAGE_TRANSITION_TYPED |
      ui::PAGE_TRANSITION_FROM_ADDRESS_BAR);
  web_contents_->GetController().LoadURLWithParams(params);
  web_contents_->Focus();
}

void CastWindowAndroid::AddNewContents(content::WebContents* source,
                                       content::WebContents* new_contents,
                                       WindowOpenDisposition disposition,
                                       const gfx::Rect& initial_rect,
                                       bool user_gesture,
                                       bool* was_blocked) {
  NOTIMPLEMENTED();
  if (was_blocked) {
    *was_blocked = true;
  }
}

void CastWindowAndroid::CloseContents(content::WebContents* source) {
  DCHECK_EQ(source, web_contents_.get());

  // We need to delay the deletion of web_contents_ (currently for 50ms) to
  // give (and guarantee) the renderer enough time to finish 'onunload'
  // handler (but we don't want to wait any longer than that to delay the
  // starting of next app).
  base::MessageLoopProxy::current()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&CastWindowAndroid::Destroy, weak_factory_.GetWeakPtr()),
      base::TimeDelta::FromMilliseconds(kWebContentsDestructionDelayInMs));
}

bool CastWindowAndroid::CanOverscrollContent() const {
  return false;
}

bool CastWindowAndroid::AddMessageToConsole(content::WebContents* source,
                                            int32 level,
                                            const base::string16& message,
                                            int32 line_no,
                                            const base::string16& source_id) {
  return false;
}

void CastWindowAndroid::ActivateContents(content::WebContents* contents) {
  DCHECK_EQ(contents, web_contents_.get());
  contents->GetRenderViewHost()->Focus();
}

void CastWindowAndroid::DeactivateContents(content::WebContents* contents) {
  DCHECK_EQ(contents, web_contents_.get());
  contents->GetRenderViewHost()->Blur();
}

void CastWindowAndroid::RenderProcessGone(base::TerminationStatus status) {
  LOG(ERROR) << "Render process gone: status=" << status;
  Destroy();
}

}  // namespace shell
}  // namespace chromecast