| OLD | NEW | 
|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "chromecast/browser/android/cast_window_android.h" | 5 #include "chromecast/browser/android/cast_window_android.h" | 
| 6 | 6 | 
| 7 #include "base/message_loop/message_loop_proxy.h" | 7 #include "base/message_loop/message_loop_proxy.h" | 
| 8 #include "chromecast/browser/android/cast_window_manager.h" | 8 #include "chromecast/browser/android/cast_window_manager.h" | 
|  | 9 #include "chromecast/browser/cast_content_window.h" | 
| 9 #include "content/public/browser/devtools_agent_host.h" | 10 #include "content/public/browser/devtools_agent_host.h" | 
| 10 #include "content/public/browser/navigation_controller.h" | 11 #include "content/public/browser/navigation_controller.h" | 
| 11 #include "content/public/browser/navigation_entry.h" | 12 #include "content/public/browser/navigation_entry.h" | 
| 12 #include "content/public/browser/render_process_host.h" | 13 #include "content/public/browser/render_process_host.h" | 
| 13 #include "content/public/browser/render_view_host.h" | 14 #include "content/public/browser/render_view_host.h" | 
| 14 #include "content/public/common/renderer_preferences.h" | 15 #include "content/public/common/renderer_preferences.h" | 
| 15 #include "jni/CastWindowAndroid_jni.h" | 16 #include "jni/CastWindowAndroid_jni.h" | 
| 16 | 17 | 
| 17 namespace chromecast { | 18 namespace chromecast { | 
| 18 namespace shell { | 19 namespace shell { | 
| 19 | 20 | 
| 20 namespace { | 21 namespace { | 
| 21 | 22 | 
| 22 // The time (in milliseconds) we wait for after a page is closed (i.e. | 23 // The time (in milliseconds) we wait for after a page is closed (i.e. | 
| 23 // after an app is stopped) before we delete the corresponding WebContents. | 24 // after an app is stopped) before we delete the corresponding WebContents. | 
| 24 const int kWebContentsDestructionDelayInMs = 50; | 25 const int kWebContentsDestructionDelayInMs = 50; | 
| 25 | 26 | 
| 26 }  // namespace | 27 }  // namespace | 
| 27 | 28 | 
| 28 // static | 29 // static | 
| 29 bool CastWindowAndroid::RegisterJni(JNIEnv* env) { | 30 bool CastWindowAndroid::RegisterJni(JNIEnv* env) { | 
| 30   return RegisterNativesImpl(env); | 31   return RegisterNativesImpl(env); | 
| 31 } | 32 } | 
| 32 | 33 | 
| 33 CastWindowAndroid::CastWindowAndroid(content::WebContents* web_contents) |  | 
| 34     : content::WebContentsObserver(web_contents), |  | 
| 35       weak_factory_(this) { |  | 
| 36 } |  | 
| 37 |  | 
| 38 CastWindowAndroid::~CastWindowAndroid() { |  | 
| 39 } |  | 
| 40 |  | 
| 41 // static | 34 // static | 
| 42 CastWindowAndroid* CastWindowAndroid::CreateNewWindow( | 35 CastWindowAndroid* CastWindowAndroid::CreateNewWindow( | 
| 43     content::BrowserContext* browser_context, | 36     content::BrowserContext* browser_context, | 
| 44     const GURL& url) { | 37     const GURL& url) { | 
| 45   content::WebContents::CreateParams create_params(browser_context); | 38   CastWindowAndroid* window_android = new CastWindowAndroid(browser_context); | 
| 46   create_params.routing_id = MSG_ROUTING_NONE; | 39   window_android->Initialize(); | 
| 47   content::WebContents* web_contents = | 40 | 
| 48       content::WebContents::Create(create_params); |  | 
| 49   CastWindowAndroid* shell = CreateCastWindowAndroid( |  | 
| 50       web_contents, |  | 
| 51       create_params.initial_size); |  | 
| 52   if (!url.is_empty()) | 41   if (!url.is_empty()) | 
| 53     shell->LoadURL(url); | 42     window_android->LoadURL(url); | 
| 54   return shell; | 43   return window_android; | 
| 55 } | 44 } | 
| 56 | 45 | 
| 57 // static | 46 CastWindowAndroid::CastWindowAndroid(content::BrowserContext* browser_context) | 
| 58 CastWindowAndroid* CastWindowAndroid::CreateCastWindowAndroid( | 47     : browser_context_(browser_context), | 
| 59     content::WebContents* web_contents, | 48       content_window_(new CastContentWindow), | 
| 60     const gfx::Size& initial_size) { | 49       weak_factory_(this) { | 
| 61   CastWindowAndroid* shell = new CastWindowAndroid(web_contents); | 50 } | 
|  | 51 | 
|  | 52 void CastWindowAndroid::Initialize() { | 
|  | 53   web_contents_ = | 
|  | 54       content_window_->CreateWebContents(gfx::Size(), browser_context_); | 
|  | 55   web_contents_->SetDelegate(this); | 
|  | 56   content::WebContentsObserver::Observe(web_contents_.get()); | 
| 62 | 57 | 
| 63   JNIEnv* env = base::android::AttachCurrentThread(); | 58   JNIEnv* env = base::android::AttachCurrentThread(); | 
| 64   base::android::ScopedJavaLocalRef<jobject> shell_android( | 59   window_java_.Reset(CreateCastWindowView(this)); | 
| 65       CreateCastWindowView(shell)); |  | 
| 66 |  | 
| 67   shell->java_object_.Reset(env, shell_android.Release()); |  | 
| 68   shell->web_contents_.reset(web_contents); |  | 
| 69   web_contents->SetDelegate(shell); |  | 
| 70 | 60 | 
| 71   Java_CastWindowAndroid_initFromNativeWebContents( | 61   Java_CastWindowAndroid_initFromNativeWebContents( | 
| 72       env, shell->java_object_.obj(), web_contents->GetJavaWebContents().obj(), | 62       env, window_java_.obj(), web_contents_->GetJavaWebContents().obj(), | 
| 73       web_contents->GetRenderProcessHost()->GetID()); | 63       web_contents_->GetRenderProcessHost()->GetID()); | 
| 74 | 64 | 
| 75   // Enabling hole-punching also requires runtime renderer preference | 65   // Enabling hole-punching also requires runtime renderer preference | 
| 76   web_contents->GetMutableRendererPrefs()-> | 66   web_contents_->GetMutableRendererPrefs()-> | 
| 77       use_video_overlay_for_embedded_encrypted_video = true; | 67       use_video_overlay_for_embedded_encrypted_video = true; | 
| 78   web_contents->GetRenderViewHost()->SyncRendererPrefs(); | 68   web_contents_->GetRenderViewHost()->SyncRendererPrefs(); | 
|  | 69 } | 
| 79 | 70 | 
| 80   return shell; | 71 CastWindowAndroid::~CastWindowAndroid() { | 
| 81 } | 72 } | 
| 82 | 73 | 
| 83 void CastWindowAndroid::Close() { | 74 void CastWindowAndroid::Close() { | 
| 84   // Close page first, which fires the window.unload event. The WebContents | 75   // Close page first, which fires the window.unload event. The WebContents | 
| 85   // itself will be destroyed after browser-process has received renderer | 76   // itself will be destroyed after browser-process has received renderer | 
| 86   // notification that the page is closed. | 77   // notification that the page is closed. | 
| 87   web_contents_->GetRenderViewHost()->ClosePage(); | 78   web_contents_->GetRenderViewHost()->ClosePage(); | 
| 88 } | 79 } | 
| 89 | 80 | 
| 90 void CastWindowAndroid::Destroy() { | 81 void CastWindowAndroid::Destroy() { | 
| 91   // Note: if multiple windows becomes supported, this may close other devtools | 82   // Note: if multiple windows becomes supported, this may close other devtools | 
| 92   // sessions. | 83   // sessions. | 
| 93   content::DevToolsAgentHost::DetachAllClients(); | 84   content::DevToolsAgentHost::DetachAllClients(); | 
| 94   CloseCastWindowView(java_object_.obj()); | 85   CloseCastWindowView(window_java_.obj()); | 
| 95   delete this; | 86   delete this; | 
| 96 } | 87 } | 
| 97 | 88 | 
| 98 void CastWindowAndroid::LoadURL(const GURL& url) { | 89 void CastWindowAndroid::LoadURL(const GURL& url) { | 
| 99   content::NavigationController::LoadURLParams params(url); | 90   content::NavigationController::LoadURLParams params(url); | 
| 100   params.transition_type = ui::PageTransitionFromInt( | 91   params.transition_type = ui::PageTransitionFromInt( | 
| 101       ui::PAGE_TRANSITION_TYPED | | 92       ui::PAGE_TRANSITION_TYPED | | 
| 102       ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); | 93       ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); | 
| 103   web_contents_->GetController().LoadURLWithParams(params); | 94   web_contents_->GetController().LoadURLWithParams(params); | 
| 104   web_contents_->Focus(); | 95   web_contents_->Focus(); | 
| (...skipping 11 matching lines...) Expand all  Loading... | 
| 116   } | 107   } | 
| 117 } | 108 } | 
| 118 | 109 | 
| 119 void CastWindowAndroid::CloseContents(content::WebContents* source) { | 110 void CastWindowAndroid::CloseContents(content::WebContents* source) { | 
| 120   DCHECK_EQ(source, web_contents_.get()); | 111   DCHECK_EQ(source, web_contents_.get()); | 
| 121 | 112 | 
| 122   // We need to delay the deletion of web_contents_ (currently for 50ms) to | 113   // We need to delay the deletion of web_contents_ (currently for 50ms) to | 
| 123   // give (and guarantee) the renderer enough time to finish 'onunload' | 114   // give (and guarantee) the renderer enough time to finish 'onunload' | 
| 124   // handler (but we don't want to wait any longer than that to delay the | 115   // handler (but we don't want to wait any longer than that to delay the | 
| 125   // starting of next app). | 116   // starting of next app). | 
| 126 |  | 
| 127   if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) { |  | 
| 128     // When shutting down in a test context, the last remaining WebContents |  | 
| 129     // is torn down at browser-thread shutdown time. Call Destroy directly to |  | 
| 130     // avoid losing the last posted task to delete this object. |  | 
| 131     // TODO(gunsch): This could probably be avoided by using a |  | 
| 132     // CompletionCallback in StopCurrentApp to wait until the app is completely |  | 
| 133     // stopped. This might require a separate message loop and might only be |  | 
| 134     // appropriate for test contexts or during shutdown, since it triggers a |  | 
| 135     // wait on the main thread. |  | 
| 136     Destroy(); |  | 
| 137     return; |  | 
| 138   } |  | 
| 139 |  | 
| 140   base::MessageLoopProxy::current()->PostDelayedTask( | 117   base::MessageLoopProxy::current()->PostDelayedTask( | 
| 141       FROM_HERE, | 118       FROM_HERE, | 
| 142       base::Bind(&CastWindowAndroid::Destroy, weak_factory_.GetWeakPtr()), | 119       base::Bind(&CastWindowAndroid::Destroy, weak_factory_.GetWeakPtr()), | 
| 143       base::TimeDelta::FromMilliseconds(kWebContentsDestructionDelayInMs)); | 120       base::TimeDelta::FromMilliseconds(kWebContentsDestructionDelayInMs)); | 
| 144 } | 121 } | 
| 145 | 122 | 
| 146 bool CastWindowAndroid::CanOverscrollContent() const { | 123 bool CastWindowAndroid::CanOverscrollContent() const { | 
| 147   return false; | 124   return false; | 
| 148 } | 125 } | 
| 149 | 126 | 
| (...skipping 15 matching lines...) Expand all  Loading... | 
| 165   contents->GetRenderViewHost()->Blur(); | 142   contents->GetRenderViewHost()->Blur(); | 
| 166 } | 143 } | 
| 167 | 144 | 
| 168 void CastWindowAndroid::RenderProcessGone(base::TerminationStatus status) { | 145 void CastWindowAndroid::RenderProcessGone(base::TerminationStatus status) { | 
| 169   LOG(ERROR) << "Render process gone: status=" << status; | 146   LOG(ERROR) << "Render process gone: status=" << status; | 
| 170   Destroy(); | 147   Destroy(); | 
| 171 } | 148 } | 
| 172 | 149 | 
| 173 }  // namespace shell | 150 }  // namespace shell | 
| 174 }  // namespace chromecast | 151 }  // namespace chromecast | 
| OLD | NEW | 
|---|