Avi Drissman | d878a501 | 2022-09-12 19:13:30 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
[email protected] | 791733d | 2014-07-17 17:56:29 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
gunsch | f44ffcb | 2014-10-13 23:01:45 | [diff] [blame] | 5 | #include "chromecast/browser/service/cast_service_simple.h" |
[email protected] | 791733d | 2014-07-17 17:56:29 | [diff] [blame] | 6 | |
derekjchow | 504caf2 | 2017-01-12 21:04:27 | [diff] [blame] | 7 | #include <string> |
8 | |||||
[email protected] | 791733d | 2014-07-17 17:56:29 | [diff] [blame] | 9 | #include "base/command_line.h" |
eugenebng | b9400dc | 2015-04-28 11:18:06 | [diff] [blame] | 10 | #include "base/files/file_util.h" |
Sean Topping | f3a1642f | 2017-08-31 00:22:51 | [diff] [blame] | 11 | #include "base/time/time.h" |
Zhiheng Vincent Li | 180905f5 | 2018-03-14 21:36:35 | [diff] [blame] | 12 | #include "chromecast/browser/cast_content_window.h" |
Sean Topping | eda675e | 2019-11-07 22:29:04 | [diff] [blame] | 13 | #include "chromecast/browser/cast_web_service.h" |
[email protected] | 791733d | 2014-07-17 17:56:29 | [diff] [blame] | 14 | #include "content/public/browser/web_contents.h" |
jlevasseur | 4681757a | 2017-02-08 22:09:03 | [diff] [blame] | 15 | #include "content/public/common/content_switches.h" |
[email protected] | 791733d | 2014-07-17 17:56:29 | [diff] [blame] | 16 | #include "net/base/filename_util.h" |
[email protected] | 791733d | 2014-07-17 17:56:29 | [diff] [blame] | 17 | |
18 | namespace chromecast { | ||||
gunsch | 68c183a | 2015-09-08 18:12:31 | [diff] [blame] | 19 | namespace shell { |
[email protected] | 791733d | 2014-07-17 17:56:29 | [diff] [blame] | 20 | |
21 | namespace { | ||||
22 | |||||
23 | GURL GetStartupURL() { | ||||
24 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | ||||
25 | const base::CommandLine::StringVector& args = command_line->GetArgs(); | ||||
26 | |||||
27 | if (args.empty()) | ||||
Sean Topping | cd917c01 | 2021-12-03 21:19:35 | [diff] [blame] | 28 | return GURL(); |
[email protected] | 791733d | 2014-07-17 17:56:29 | [diff] [blame] | 29 | |
30 | GURL url(args[0]); | ||||
31 | if (url.is_valid() && url.has_scheme()) | ||||
32 | return url; | ||||
33 | |||||
eugenebng | b9400dc | 2015-04-28 11:18:06 | [diff] [blame] | 34 | return net::FilePathToFileURL( |
35 | base::MakeAbsoluteFilePath(base::FilePath(args[0]))); | ||||
[email protected] | 791733d | 2014-07-17 17:56:29 | [diff] [blame] | 36 | } |
37 | |||||
[email protected] | 791733d | 2014-07-17 17:56:29 | [diff] [blame] | 38 | } // namespace |
39 | |||||
Sean Topping | d23e572 | 2021-11-22 20:48:49 | [diff] [blame] | 40 | CastServiceSimple::CastServiceSimple(CastWebService* web_service) |
41 | : web_service_(web_service) { | ||||
42 | DCHECK(web_service_); | ||||
[email protected] | 791733d | 2014-07-17 17:56:29 | [diff] [blame] | 43 | } |
44 | |||||
45 | CastServiceSimple::~CastServiceSimple() { | ||||
46 | } | ||||
47 | |||||
byungchul | 70f384aa | 2014-12-16 19:14:11 | [diff] [blame] | 48 | void CastServiceSimple::InitializeInternal() { |
halliwell | a9d2cb9 | 2015-01-23 17:44:02 | [diff] [blame] | 49 | startup_url_ = GetStartupURL(); |
byungchul | 70f384aa | 2014-12-16 19:14:11 | [diff] [blame] | 50 | } |
51 | |||||
52 | void CastServiceSimple::FinalizeInternal() { | ||||
[email protected] | 791733d | 2014-07-17 17:56:29 | [diff] [blame] | 53 | } |
54 | |||||
55 | void CastServiceSimple::StartInternal() { | ||||
jlevasseur | 4681757a | 2017-02-08 22:09:03 | [diff] [blame] | 56 | if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) { |
57 | return; | ||||
58 | } | ||||
59 | |||||
Sean Topping | cd917c01 | 2021-12-03 21:19:35 | [diff] [blame] | 60 | if (startup_url_.is_empty()) { |
61 | return; | ||||
62 | } | ||||
63 | |||||
Sean Topping | 92d0465b | 2021-07-16 01:11:33 | [diff] [blame] | 64 | ::chromecast::mojom::CastWebViewParamsPtr params = |
65 | ::chromecast::mojom::CastWebViewParams::New(); | ||||
66 | params->enabled_for_dev = true; | ||||
67 | |||||
Sean Topping | 077ba6d8 | 2021-11-02 22:48:42 | [diff] [blame] | 68 | cast_web_view_ = web_service_->CreateWebViewInternal(std::move(params)); |
Sean Topping | a0a0f5e | 2020-03-19 00:22:30 | [diff] [blame] | 69 | cast_web_view_->cast_web_contents()->LoadUrl(startup_url_); |
70 | cast_web_view_->window()->GrantScreenAccess(); | ||||
Sean Topping | 805f50e | 2021-05-21 00:55:47 | [diff] [blame] | 71 | cast_web_view_->window()->CreateWindow( |
Sean Topping | 49d1cf7f | 2019-10-23 18:59:53 | [diff] [blame] | 72 | ::chromecast::mojom::ZOrder::APP, |
73 | chromecast::VisibilityPriority::STICKY_ACTIVITY); | ||||
[email protected] | 791733d | 2014-07-17 17:56:29 | [diff] [blame] | 74 | } |
75 | |||||
76 | void CastServiceSimple::StopInternal() { | ||||
derekjchow | 7348f00 | 2017-02-22 16:46:11 | [diff] [blame] | 77 | if (cast_web_view_) { |
Sean Topping | a0a0f5e | 2020-03-19 00:22:30 | [diff] [blame] | 78 | cast_web_view_->cast_web_contents()->ClosePage(); |
jlevasseur | 4681757a | 2017-02-08 22:09:03 | [diff] [blame] | 79 | } |
derekjchow | 7348f00 | 2017-02-22 16:46:11 | [diff] [blame] | 80 | cast_web_view_.reset(); |
[email protected] | 791733d | 2014-07-17 17:56:29 | [diff] [blame] | 81 | } |
82 | |||||
gunsch | 68c183a | 2015-09-08 18:12:31 | [diff] [blame] | 83 | } // namespace shell |
[email protected] | 791733d | 2014-07-17 17:56:29 | [diff] [blame] | 84 | } // namespace chromecast |