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/service/cast_service_simple.h" | 5 #include "chromecast/browser/service/cast_service_simple.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/files/file_path.h" | 8 #include "chromecast/browser/cast_content_window.h" |
9 #include "base/macros.h" | |
10 #include "content/public/browser/render_view_host.h" | 9 #include "content/public/browser/render_view_host.h" |
11 #include "content/public/browser/web_contents.h" | 10 #include "content/public/browser/web_contents.h" |
12 #include "net/base/filename_util.h" | 11 #include "net/base/filename_util.h" |
13 #include "net/url_request/url_request_context_getter.h" | 12 #include "net/url_request/url_request_context_getter.h" |
14 #include "ui/aura/env.h" | |
15 #include "ui/aura/layout_manager.h" | |
16 #include "ui/aura/test/test_screen.h" | |
17 #include "ui/aura/window.h" | |
18 #include "ui/aura/window_tree_host.h" | |
19 #include "ui/gfx/size.h" | |
20 #include "url/gurl.h" | 13 #include "url/gurl.h" |
21 | 14 |
22 namespace chromecast { | 15 namespace chromecast { |
23 | 16 |
24 namespace { | 17 namespace { |
25 | 18 |
26 GURL GetStartupURL() { | 19 GURL GetStartupURL() { |
27 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 20 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
28 const base::CommandLine::StringVector& args = command_line->GetArgs(); | 21 const base::CommandLine::StringVector& args = command_line->GetArgs(); |
29 | 22 |
30 if (args.empty()) | 23 if (args.empty()) |
31 return GURL("http://www.google.com/"); | 24 return GURL("http://www.google.com/"); |
32 | 25 |
33 GURL url(args[0]); | 26 GURL url(args[0]); |
34 if (url.is_valid() && url.has_scheme()) | 27 if (url.is_valid() && url.has_scheme()) |
35 return url; | 28 return url; |
36 | 29 |
37 return net::FilePathToFileURL(base::FilePath(args[0])); | 30 return net::FilePathToFileURL(base::FilePath(args[0])); |
38 } | 31 } |
39 | 32 |
40 class FillLayout : public aura::LayoutManager { | |
41 public: | |
42 explicit FillLayout(aura::Window* root) : root_(root) {} | |
43 virtual ~FillLayout() {} | |
44 | |
45 private: | |
46 // aura::LayoutManager: | |
47 virtual void OnWindowResized() override {} | |
48 | |
49 virtual void OnWindowAddedToLayout(aura::Window* child) override { | |
50 child->SetBounds(root_->bounds()); | |
51 } | |
52 | |
53 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) override {} | |
54 | |
55 virtual void OnWindowRemovedFromLayout(aura::Window* child) override {} | |
56 | |
57 virtual void OnChildWindowVisibilityChanged(aura::Window* child, | |
58 bool visible) override {} | |
59 | |
60 virtual void SetChildBounds(aura::Window* child, | |
61 const gfx::Rect& requested_bounds) override { | |
62 SetChildBoundsDirect(child, requested_bounds); | |
63 } | |
64 | |
65 aura::Window* root_; | |
66 | |
67 DISALLOW_COPY_AND_ASSIGN(FillLayout); | |
68 }; | |
69 | |
70 } // namespace | 33 } // namespace |
71 | 34 |
72 // static | 35 // static |
73 CastService* CastService::Create( | 36 CastService* CastService::Create( |
74 content::BrowserContext* browser_context, | 37 content::BrowserContext* browser_context, |
75 net::URLRequestContextGetter* request_context_getter, | 38 net::URLRequestContextGetter* request_context_getter, |
76 const OptInStatsChangedCallback& opt_in_stats_callback) { | 39 const OptInStatsChangedCallback& opt_in_stats_callback) { |
77 return new CastServiceSimple(browser_context, opt_in_stats_callback); | 40 return new CastServiceSimple(browser_context, opt_in_stats_callback); |
78 } | 41 } |
79 | 42 |
80 CastServiceSimple::CastServiceSimple( | 43 CastServiceSimple::CastServiceSimple( |
81 content::BrowserContext* browser_context, | 44 content::BrowserContext* browser_context, |
82 const OptInStatsChangedCallback& opt_in_stats_callback) | 45 const OptInStatsChangedCallback& opt_in_stats_callback) |
83 : CastService(browser_context, opt_in_stats_callback) { | 46 : CastService(browser_context, opt_in_stats_callback) { |
84 } | 47 } |
85 | 48 |
86 CastServiceSimple::~CastServiceSimple() { | 49 CastServiceSimple::~CastServiceSimple() { |
87 } | 50 } |
88 | 51 |
89 void CastServiceSimple::Initialize() { | 52 void CastServiceSimple::Initialize() { |
90 } | 53 } |
91 | 54 |
92 void CastServiceSimple::StartInternal() { | 55 void CastServiceSimple::StartInternal() { |
93 // Aura initialization | 56 // This is the simple version that hard-codes the size. |
94 gfx::Size initial_size = gfx::Size(1280, 720); | 57 gfx::Size initial_size(1280, 720); |
95 // TODO(lcwu): http://crbug.com/391074. Chromecast only needs a minimal | |
96 // implementation of gfx::screen and aura's TestScreen will do for now. | |
97 // Change the code to use ozone's screen implementation when it is ready. | |
98 aura::TestScreen* screen = aura::TestScreen::Create(initial_size); | |
99 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen); | |
100 CHECK(aura::Env::GetInstance()); | |
101 window_tree_host_.reset( | |
102 aura::WindowTreeHost::Create(gfx::Rect(initial_size))); | |
103 window_tree_host_->InitHost(); | |
104 window_tree_host_->window()->SetLayoutManager( | |
105 new FillLayout(window_tree_host_->window())); | |
106 window_tree_host_->Show(); | |
107 | 58 |
108 // Create a WebContents | 59 window_.reset(new CastContentWindow); |
109 content::WebContents::CreateParams create_params(browser_context(), NULL); | 60 web_contents_ = window_->Create(initial_size, browser_context()); |
110 create_params.routing_id = MSG_ROUTING_NONE; | |
111 create_params.initial_size = initial_size; | |
112 web_contents_.reset(content::WebContents::Create(create_params)); | |
113 | |
114 // Add and show content's view/window | |
115 aura::Window* content_window = web_contents_->GetNativeView(); | |
116 aura::Window* parent = window_tree_host_->window(); | |
117 if (!parent->Contains(content_window)) { | |
118 parent->AddChild(content_window); | |
119 } | |
120 content_window->Show(); | |
121 | 61 |
122 web_contents_->GetController().LoadURL(GetStartupURL(), | 62 web_contents_->GetController().LoadURL(GetStartupURL(), |
123 content::Referrer(), | 63 content::Referrer(), |
124 ui::PAGE_TRANSITION_TYPED, | 64 ui::PAGE_TRANSITION_TYPED, |
125 std::string()); | 65 std::string()); |
126 } | 66 } |
127 | 67 |
128 void CastServiceSimple::StopInternal() { | 68 void CastServiceSimple::StopInternal() { |
129 web_contents_->GetRenderViewHost()->ClosePage(); | 69 web_contents_->GetRenderViewHost()->ClosePage(); |
130 window_tree_host_.reset(); | |
131 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL); | |
132 aura::Env::DeleteInstance(); | |
133 web_contents_.reset(); | 70 web_contents_.reset(); |
| 71 window_.reset(); |
134 } | 72 } |
135 | 73 |
136 } // namespace chromecast | 74 } // namespace chromecast |
OLD | NEW |