OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/extensions/api/desktop_capture/desktop_capture_api.h" | 5 #include "chrome/browser/extensions/api/desktop_capture/desktop_capture_api.h" |
6 | 6 |
7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
45 | 45 |
46 } // namespace | 46 } // namespace |
47 | 47 |
48 // static | 48 // static |
49 void DesktopCaptureChooseDesktopMediaFunction::SetPickerFactoryForTests( | 49 void DesktopCaptureChooseDesktopMediaFunction::SetPickerFactoryForTests( |
50 PickerFactory* factory) { | 50 PickerFactory* factory) { |
51 g_picker_factory = factory; | 51 g_picker_factory = factory; |
52 } | 52 } |
53 | 53 |
54 DesktopCaptureChooseDesktopMediaFunction:: | 54 DesktopCaptureChooseDesktopMediaFunction:: |
55 DesktopCaptureChooseDesktopMediaFunction() | 55 DesktopCaptureChooseDesktopMediaFunction() { |
56 : render_process_id_(0), | |
57 render_view_id_(0) { | |
58 } | 56 } |
59 | 57 |
60 DesktopCaptureChooseDesktopMediaFunction:: | 58 DesktopCaptureChooseDesktopMediaFunction:: |
61 ~DesktopCaptureChooseDesktopMediaFunction() { | 59 ~DesktopCaptureChooseDesktopMediaFunction() { |
62 // RenderViewHost may be already destroyed. | 60 // RenderViewHost may be already destroyed. |
63 if (render_view_host()) { | 61 if (render_view_host()) { |
ncarter (slow)
2014/07/10 01:17:51
As you are probably aware, these would ideally be
| |
64 DesktopCaptureRequestsRegistry::GetInstance()->RemoveRequest( | 62 DesktopCaptureRequestsRegistry::GetInstance()->RemoveRequest( |
65 render_view_host()->GetProcess()->GetID(), request_id_); | 63 render_view_host()->GetProcess()->GetID(), request_id_); |
66 } | 64 } |
67 } | 65 } |
68 | 66 |
69 void DesktopCaptureChooseDesktopMediaFunction::Cancel() { | 67 void DesktopCaptureChooseDesktopMediaFunction::Cancel() { |
70 // Keep reference to |this| to ensure the object doesn't get destroyed before | 68 // Keep reference to |this| to ensure the object doesn't get destroyed before |
71 // we return. | 69 // we return. |
72 scoped_refptr<DesktopCaptureChooseDesktopMediaFunction> self(this); | 70 scoped_refptr<DesktopCaptureChooseDesktopMediaFunction> self(this); |
73 if (picker_) { | 71 if (picker_) { |
74 picker_.reset(); | 72 picker_.reset(); |
75 SetResult(new base::StringValue(std::string())); | 73 SetResult(new base::StringValue(std::string())); |
76 SendResponse(true); | 74 SendResponse(true); |
77 } | 75 } |
78 } | 76 } |
79 | 77 |
80 bool DesktopCaptureChooseDesktopMediaFunction::RunAsync() { | 78 bool DesktopCaptureChooseDesktopMediaFunction::RunAsync() { |
81 EXTENSION_FUNCTION_VALIDATE(args_->GetSize() > 0); | 79 EXTENSION_FUNCTION_VALIDATE(args_->GetSize() > 0); |
82 | 80 |
83 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &request_id_)); | 81 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &request_id_)); |
84 args_->Remove(0, NULL); | 82 args_->Remove(0, NULL); |
85 | 83 |
86 scoped_ptr<api::desktop_capture::ChooseDesktopMedia::Params> params = | 84 scoped_ptr<api::desktop_capture::ChooseDesktopMedia::Params> params = |
87 api::desktop_capture::ChooseDesktopMedia::Params::Create(*args_); | 85 api::desktop_capture::ChooseDesktopMedia::Params::Create(*args_); |
88 EXTENSION_FUNCTION_VALIDATE(params.get()); | 86 EXTENSION_FUNCTION_VALIDATE(params.get()); |
89 | 87 |
90 DesktopCaptureRequestsRegistry::GetInstance()->AddRequest( | 88 DesktopCaptureRequestsRegistry::GetInstance()->AddRequest( |
91 render_view_host()->GetProcess()->GetID(), request_id_, this); | 89 render_view_host()->GetProcess()->GetID(), request_id_, this); |
92 | 90 |
91 // |web_contents_for_stream| is the WebContents for which the stream is | |
92 // created. On the other hand, |web_contents_for_picker| is used to determine | |
93 // where to show the picker's UI. | |
94 content::WebContents* web_contents_for_stream = NULL; | |
93 gfx::NativeWindow parent_window = NULL; | 95 gfx::NativeWindow parent_window = NULL; |
94 content::RenderViewHost* render_view = NULL; | 96 content::WebContents* web_contents_for_picker = NULL; |
Sergey Ulanov
2014/07/09 18:06:24
The same WebContents should be used for both picke
miu
2014/07/09 23:28:04
Done. However, I should point out I wasn't actual
| |
95 content::WebContents* web_contents = NULL; | |
96 base::string16 target_name; | 97 base::string16 target_name; |
97 if (params->target_tab) { | 98 if (params->target_tab) { |
98 if (!params->target_tab->url) { | 99 if (!params->target_tab->url) { |
99 error_ = kNoUrlError; | 100 error_ = kNoUrlError; |
100 return false; | 101 return false; |
101 } | 102 } |
102 origin_ = GURL(*(params->target_tab->url)).GetOrigin(); | 103 origin_ = GURL(*(params->target_tab->url)).GetOrigin(); |
103 | 104 |
104 if (!CommandLine::ForCurrentProcess()->HasSwitch( | 105 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
105 switches::kAllowHttpScreenCapture) && | 106 switches::kAllowHttpScreenCapture) && |
106 !origin_.SchemeIsSecure()) { | 107 !origin_.SchemeIsSecure()) { |
107 error_ = kTabUrlNotSecure; | 108 error_ = kTabUrlNotSecure; |
108 return false; | 109 return false; |
109 } | 110 } |
110 target_name = base::UTF8ToUTF16(origin_.SchemeIsSecure() ? | 111 target_name = base::UTF8ToUTF16(origin_.SchemeIsSecure() ? |
111 net::GetHostAndOptionalPort(origin_) : origin_.spec()); | 112 net::GetHostAndOptionalPort(origin_) : origin_.spec()); |
112 | 113 |
113 if (!params->target_tab->id) { | 114 if (!params->target_tab->id) { |
114 error_ = kNoTabIdError; | 115 error_ = kNoTabIdError; |
115 return false; | 116 return false; |
116 } | 117 } |
117 | 118 |
118 if (!ExtensionTabUtil::GetTabById(*(params->target_tab->id), GetProfile(), | 119 if (!ExtensionTabUtil::GetTabById( |
119 true, NULL, NULL, &web_contents, NULL)) { | 120 *(params->target_tab->id), GetProfile(), true, NULL, NULL, |
121 &web_contents_for_stream, NULL)) { | |
120 error_ = kInvalidTabIdError; | 122 error_ = kInvalidTabIdError; |
121 return false; | 123 return false; |
122 } | 124 } |
125 DCHECK(web_contents_for_stream); | |
123 | 126 |
124 GURL current_origin_ = | 127 GURL current_origin_ = |
125 web_contents->GetLastCommittedURL().GetOrigin(); | 128 web_contents_for_stream->GetLastCommittedURL().GetOrigin(); |
126 if (current_origin_ != origin_) { | 129 if (current_origin_ != origin_) { |
127 error_ = kTabUrlChangedError; | 130 error_ = kTabUrlChangedError; |
128 return false; | 131 return false; |
129 } | 132 } |
130 | 133 |
131 // Register to be notified when the tab is closed. | 134 web_contents_for_picker = web_contents_for_stream; |
132 Observe(web_contents); | 135 parent_window = web_contents_for_picker->GetTopLevelNativeWindow(); |
133 | |
134 render_view = web_contents->GetRenderViewHost(); | |
135 parent_window = web_contents->GetTopLevelNativeWindow(); | |
136 } else { | 136 } else { |
137 origin_ = GetExtension()->url(); | 137 origin_ = GetExtension()->url(); |
138 target_name = base::UTF8ToUTF16(GetExtension()->name()); | 138 target_name = base::UTF8ToUTF16(GetExtension()->name()); |
139 render_view = render_view_host(); | 139 web_contents_for_stream = |
140 | 140 content::WebContents::FromRenderViewHost(render_view_host()); |
141 web_contents = GetAssociatedWebContents(); | 141 DCHECK(web_contents_for_stream); |
142 if (web_contents) { | 142 web_contents_for_picker = GetAssociatedWebContents(); |
Sergey Ulanov
2014/07/09 18:06:24
I think this will always return the same WebConten
miu
2014/07/09 23:28:04
It doesn't. The DesktopCaptureApiTest.ChooseDeskt
| |
143 parent_window = web_contents->GetTopLevelNativeWindow(); | 143 if (web_contents_for_picker) { |
144 parent_window = web_contents_for_picker->GetTopLevelNativeWindow(); | |
144 } else { | 145 } else { |
145 #if defined(USE_ASH) | 146 #if defined(USE_ASH) |
146 if (chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_ASH) | 147 if (chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_ASH) |
147 parent_window = ash::Shell::GetPrimaryRootWindow(); | 148 parent_window = ash::Shell::GetPrimaryRootWindow(); |
148 #endif | 149 #endif |
149 } | 150 } |
150 } | 151 } |
151 | 152 |
152 render_process_id_ = render_view->GetProcess()->GetID(); | 153 // Register to be notified when the tab is closed. |
153 render_view_id_ = render_view->GetRoutingID(); | 154 Observe(web_contents_for_stream); |
154 | 155 |
155 bool show_screens = false; | 156 bool show_screens = false; |
156 bool show_windows = false; | 157 bool show_windows = false; |
157 | 158 |
158 for (std::vector<api::desktop_capture::DesktopCaptureSourceType>::iterator | 159 for (std::vector<api::desktop_capture::DesktopCaptureSourceType>::iterator |
159 it = params->sources.begin(); it != params->sources.end(); ++it) { | 160 it = params->sources.begin(); it != params->sources.end(); ++it) { |
160 switch (*it) { | 161 switch (*it) { |
161 case api::desktop_capture::DESKTOP_CAPTURE_SOURCE_TYPE_NONE: | 162 case api::desktop_capture::DESKTOP_CAPTURE_SOURCE_TYPE_NONE: |
162 error_ = kInvalidSourceNameError; | 163 error_ = kInvalidSourceNameError; |
163 return false; | 164 return false; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 #if defined(TOOLKIT_VIEWS) || defined(OS_MACOSX) | 213 #if defined(TOOLKIT_VIEWS) || defined(OS_MACOSX) |
213 picker_ = DesktopMediaPicker::Create(); | 214 picker_ = DesktopMediaPicker::Create(); |
214 #else | 215 #else |
215 error_ = "Desktop Capture API is not yet implemented for this platform."; | 216 error_ = "Desktop Capture API is not yet implemented for this platform."; |
216 return false; | 217 return false; |
217 #endif | 218 #endif |
218 } | 219 } |
219 DesktopMediaPicker::DoneCallback callback = base::Bind( | 220 DesktopMediaPicker::DoneCallback callback = base::Bind( |
220 &DesktopCaptureChooseDesktopMediaFunction::OnPickerDialogResults, this); | 221 &DesktopCaptureChooseDesktopMediaFunction::OnPickerDialogResults, this); |
221 | 222 |
222 picker_->Show(web_contents, | 223 picker_->Show(web_contents_for_picker, |
223 parent_window, parent_window, | 224 parent_window, parent_window, |
224 base::UTF8ToUTF16(GetExtension()->name()), | 225 base::UTF8ToUTF16(GetExtension()->name()), |
225 target_name, | 226 target_name, |
226 media_list.Pass(), callback); | 227 media_list.Pass(), callback); |
227 return true; | 228 return true; |
228 } | 229 } |
229 | 230 |
230 void DesktopCaptureChooseDesktopMediaFunction::WebContentsDestroyed() { | 231 void DesktopCaptureChooseDesktopMediaFunction::WebContentsDestroyed() { |
231 Cancel(); | 232 Cancel(); |
232 } | 233 } |
233 | 234 |
234 void DesktopCaptureChooseDesktopMediaFunction::OnPickerDialogResults( | 235 void DesktopCaptureChooseDesktopMediaFunction::OnPickerDialogResults( |
235 content::DesktopMediaID source) { | 236 content::DesktopMediaID source) { |
236 std::string result; | 237 std::string result; |
237 if (source.type != content::DesktopMediaID::TYPE_NONE) { | 238 if (source.type != content::DesktopMediaID::TYPE_NONE && |
239 web_contents()) { | |
238 DesktopStreamsRegistry* registry = | 240 DesktopStreamsRegistry* registry = |
239 MediaCaptureDevicesDispatcher::GetInstance()-> | 241 MediaCaptureDevicesDispatcher::GetInstance()-> |
240 GetDesktopStreamsRegistry(); | 242 GetDesktopStreamsRegistry(); |
241 result = registry->RegisterStream( | 243 result = registry->RegisterStream( |
242 render_process_id_, | 244 web_contents(), |
243 render_view_id_, | |
244 origin_, | 245 origin_, |
245 source, | 246 source, |
246 GetExtension()->name()); | 247 GetExtension()->name()); |
247 } | 248 } |
248 | 249 |
249 SetResult(new base::StringValue(result)); | 250 SetResult(new base::StringValue(result)); |
250 SendResponse(true); | 251 SendResponse(true); |
251 } | 252 } |
252 | 253 |
253 DesktopCaptureRequestsRegistry::RequestId::RequestId(int process_id, | 254 DesktopCaptureRequestsRegistry::RequestId::RequestId(int process_id, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
303 | 304 |
304 void DesktopCaptureRequestsRegistry::CancelRequest(int process_id, | 305 void DesktopCaptureRequestsRegistry::CancelRequest(int process_id, |
305 int request_id) { | 306 int request_id) { |
306 RequestsMap::iterator it = requests_.find(RequestId(process_id, request_id)); | 307 RequestsMap::iterator it = requests_.find(RequestId(process_id, request_id)); |
307 if (it != requests_.end()) | 308 if (it != requests_.end()) |
308 it->second->Cancel(); | 309 it->second->Cancel(); |
309 } | 310 } |
310 | 311 |
311 | 312 |
312 } // namespace extensions | 313 } // namespace extensions |
OLD | NEW |