| 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" |
| 11 #include "chrome/browser/extensions/extension_tab_util.h" | 11 #include "chrome/browser/extensions/extension_tab_util.h" |
| 12 #include "chrome/browser/media/desktop_media_list_ash.h" | 12 #include "chrome/browser/media/desktop_media_list_ash.h" |
| 13 #include "chrome/browser/media/desktop_streams_registry.h" | 13 #include "chrome/browser/media/desktop_streams_registry.h" |
| 14 #include "chrome/browser/media/media_capture_devices_dispatcher.h" | 14 #include "chrome/browser/media/media_capture_devices_dispatcher.h" |
| 15 #include "chrome/browser/media/native_desktop_media_list.h" | 15 #include "chrome/browser/media/native_desktop_media_list.h" |
| 16 #include "chrome/browser/ui/ash/ash_util.h" | 16 #include "chrome/browser/ui/ash/ash_util.h" |
| 17 #include "chrome/browser/ui/host_desktop.h" | 17 #include "chrome/browser/ui/host_desktop.h" |
| 18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 19 #include "chrome/common/extensions/api/tabs.h" | 19 #include "chrome/common/extensions/api/tabs.h" |
| 20 #include "content/public/browser/render_frame_host.h" |
| 20 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
| 21 #include "content/public/browser/render_view_host.h" | 22 #include "content/public/browser/render_view_host.h" |
| 22 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
| 23 #include "net/base/net_util.h" | 24 #include "net/base/net_util.h" |
| 24 #include "third_party/webrtc/modules/desktop_capture/desktop_capture_options.h" | 25 #include "third_party/webrtc/modules/desktop_capture/desktop_capture_options.h" |
| 25 #include "third_party/webrtc/modules/desktop_capture/screen_capturer.h" | 26 #include "third_party/webrtc/modules/desktop_capture/screen_capturer.h" |
| 26 #include "third_party/webrtc/modules/desktop_capture/window_capturer.h" | 27 #include "third_party/webrtc/modules/desktop_capture/window_capturer.h" |
| 27 | 28 |
| 28 namespace extensions { | 29 namespace extensions { |
| 29 | 30 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 45 | 46 |
| 46 } // namespace | 47 } // namespace |
| 47 | 48 |
| 48 // static | 49 // static |
| 49 void DesktopCaptureChooseDesktopMediaFunction::SetPickerFactoryForTests( | 50 void DesktopCaptureChooseDesktopMediaFunction::SetPickerFactoryForTests( |
| 50 PickerFactory* factory) { | 51 PickerFactory* factory) { |
| 51 g_picker_factory = factory; | 52 g_picker_factory = factory; |
| 52 } | 53 } |
| 53 | 54 |
| 54 DesktopCaptureChooseDesktopMediaFunction:: | 55 DesktopCaptureChooseDesktopMediaFunction:: |
| 55 DesktopCaptureChooseDesktopMediaFunction() | 56 DesktopCaptureChooseDesktopMediaFunction() { |
| 56 : render_process_id_(0), | |
| 57 render_view_id_(0) { | |
| 58 } | 57 } |
| 59 | 58 |
| 60 DesktopCaptureChooseDesktopMediaFunction:: | 59 DesktopCaptureChooseDesktopMediaFunction:: |
| 61 ~DesktopCaptureChooseDesktopMediaFunction() { | 60 ~DesktopCaptureChooseDesktopMediaFunction() { |
| 62 // RenderViewHost may be already destroyed. | 61 // RenderViewHost may be already destroyed. |
| 63 if (render_view_host()) { | 62 if (render_view_host()) { |
| 64 DesktopCaptureRequestsRegistry::GetInstance()->RemoveRequest( | 63 DesktopCaptureRequestsRegistry::GetInstance()->RemoveRequest( |
| 65 render_view_host()->GetProcess()->GetID(), request_id_); | 64 render_view_host()->GetProcess()->GetID(), request_id_); |
| 66 } | 65 } |
| 67 } | 66 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 83 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &request_id_)); | 82 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &request_id_)); |
| 84 args_->Remove(0, NULL); | 83 args_->Remove(0, NULL); |
| 85 | 84 |
| 86 scoped_ptr<api::desktop_capture::ChooseDesktopMedia::Params> params = | 85 scoped_ptr<api::desktop_capture::ChooseDesktopMedia::Params> params = |
| 87 api::desktop_capture::ChooseDesktopMedia::Params::Create(*args_); | 86 api::desktop_capture::ChooseDesktopMedia::Params::Create(*args_); |
| 88 EXTENSION_FUNCTION_VALIDATE(params.get()); | 87 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 89 | 88 |
| 90 DesktopCaptureRequestsRegistry::GetInstance()->AddRequest( | 89 DesktopCaptureRequestsRegistry::GetInstance()->AddRequest( |
| 91 render_view_host()->GetProcess()->GetID(), request_id_, this); | 90 render_view_host()->GetProcess()->GetID(), request_id_, this); |
| 92 | 91 |
| 93 gfx::NativeWindow parent_window = NULL; | 92 // |web_contents| is the WebContents for which the stream is created, and will |
| 94 content::RenderViewHost* render_view = NULL; | 93 // also be used to determine where to show the picker's UI. |
| 95 content::WebContents* web_contents = NULL; | 94 content::WebContents* web_contents = NULL; |
| 96 base::string16 target_name; | 95 base::string16 target_name; |
| 97 if (params->target_tab) { | 96 if (params->target_tab) { |
| 98 if (!params->target_tab->url) { | 97 if (!params->target_tab->url) { |
| 99 error_ = kNoUrlError; | 98 error_ = kNoUrlError; |
| 100 return false; | 99 return false; |
| 101 } | 100 } |
| 102 origin_ = GURL(*(params->target_tab->url)).GetOrigin(); | 101 origin_ = GURL(*(params->target_tab->url)).GetOrigin(); |
| 103 | 102 |
| 104 if (!CommandLine::ForCurrentProcess()->HasSwitch( | 103 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 105 switches::kAllowHttpScreenCapture) && | 104 switches::kAllowHttpScreenCapture) && |
| 106 !origin_.SchemeIsSecure()) { | 105 !origin_.SchemeIsSecure()) { |
| 107 error_ = kTabUrlNotSecure; | 106 error_ = kTabUrlNotSecure; |
| 108 return false; | 107 return false; |
| 109 } | 108 } |
| 110 target_name = base::UTF8ToUTF16(origin_.SchemeIsSecure() ? | 109 target_name = base::UTF8ToUTF16(origin_.SchemeIsSecure() ? |
| 111 net::GetHostAndOptionalPort(origin_) : origin_.spec()); | 110 net::GetHostAndOptionalPort(origin_) : origin_.spec()); |
| 112 | 111 |
| 113 if (!params->target_tab->id) { | 112 if (!params->target_tab->id) { |
| 114 error_ = kNoTabIdError; | 113 error_ = kNoTabIdError; |
| 115 return false; | 114 return false; |
| 116 } | 115 } |
| 117 | 116 |
| 118 if (!ExtensionTabUtil::GetTabById(*(params->target_tab->id), GetProfile(), | 117 if (!ExtensionTabUtil::GetTabById(*(params->target_tab->id), GetProfile(), |
| 119 true, NULL, NULL, &web_contents, NULL)) { | 118 true, NULL, NULL, &web_contents, NULL)) { |
| 120 error_ = kInvalidTabIdError; | 119 error_ = kInvalidTabIdError; |
| 121 return false; | 120 return false; |
| 122 } | 121 } |
| 122 DCHECK(web_contents); |
| 123 | 123 |
| 124 GURL current_origin_ = | 124 if (origin_ != web_contents->GetLastCommittedURL().GetOrigin()) { |
| 125 web_contents->GetLastCommittedURL().GetOrigin(); | |
| 126 if (current_origin_ != origin_) { | |
| 127 error_ = kTabUrlChangedError; | 125 error_ = kTabUrlChangedError; |
| 128 return false; | 126 return false; |
| 129 } | 127 } |
| 130 | |
| 131 // Register to be notified when the tab is closed. | |
| 132 Observe(web_contents); | |
| 133 | |
| 134 render_view = web_contents->GetRenderViewHost(); | |
| 135 parent_window = web_contents->GetTopLevelNativeWindow(); | |
| 136 } else { | 128 } else { |
| 137 origin_ = GetExtension()->url(); | 129 origin_ = GetExtension()->url(); |
| 138 target_name = base::UTF8ToUTF16(GetExtension()->name()); | 130 target_name = base::UTF8ToUTF16(GetExtension()->name()); |
| 139 render_view = render_view_host(); | 131 web_contents = content::WebContents::FromRenderViewHost(render_view_host()); |
| 140 | 132 DCHECK(web_contents); |
| 141 web_contents = GetAssociatedWebContents(); | |
| 142 if (web_contents) { | |
| 143 parent_window = web_contents->GetTopLevelNativeWindow(); | |
| 144 } else { | |
| 145 #if defined(USE_ASH) | |
| 146 if (chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_ASH) | |
| 147 parent_window = ash::Shell::GetPrimaryRootWindow(); | |
| 148 #endif | |
| 149 } | |
| 150 } | 133 } |
| 151 | 134 |
| 152 render_process_id_ = render_view->GetProcess()->GetID(); | 135 // Register to be notified when the tab is closed. |
| 153 render_view_id_ = render_view->GetRoutingID(); | 136 Observe(web_contents); |
| 154 | 137 |
| 155 bool show_screens = false; | 138 bool show_screens = false; |
| 156 bool show_windows = false; | 139 bool show_windows = false; |
| 157 | 140 |
| 158 for (std::vector<api::desktop_capture::DesktopCaptureSourceType>::iterator | 141 for (std::vector<api::desktop_capture::DesktopCaptureSourceType>::iterator |
| 159 it = params->sources.begin(); it != params->sources.end(); ++it) { | 142 it = params->sources.begin(); it != params->sources.end(); ++it) { |
| 160 switch (*it) { | 143 switch (*it) { |
| 161 case api::desktop_capture::DESKTOP_CAPTURE_SOURCE_TYPE_NONE: | 144 case api::desktop_capture::DESKTOP_CAPTURE_SOURCE_TYPE_NONE: |
| 162 error_ = kInvalidSourceNameError; | 145 error_ = kInvalidSourceNameError; |
| 163 return false; | 146 return false; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 174 error_ = kTabCaptureNotSupportedError; | 157 error_ = kTabCaptureNotSupportedError; |
| 175 return false; | 158 return false; |
| 176 } | 159 } |
| 177 } | 160 } |
| 178 | 161 |
| 179 if (!show_screens && !show_windows) { | 162 if (!show_screens && !show_windows) { |
| 180 error_ = kEmptySourcesListError; | 163 error_ = kEmptySourcesListError; |
| 181 return false; | 164 return false; |
| 182 } | 165 } |
| 183 | 166 |
| 167 const gfx::NativeWindow parent_window = |
| 168 web_contents->GetTopLevelNativeWindow(); |
| 184 scoped_ptr<DesktopMediaList> media_list; | 169 scoped_ptr<DesktopMediaList> media_list; |
| 185 if (g_picker_factory) { | 170 if (g_picker_factory) { |
| 186 media_list = g_picker_factory->CreateModel( | 171 media_list = g_picker_factory->CreateModel( |
| 187 show_screens, show_windows); | 172 show_screens, show_windows); |
| 188 picker_ = g_picker_factory->CreatePicker(); | 173 picker_ = g_picker_factory->CreatePicker(); |
| 189 } else { | 174 } else { |
| 190 #if defined(USE_ASH) | 175 #if defined(USE_ASH) |
| 191 if (chrome::IsNativeWindowInAsh(parent_window)) { | 176 if (chrome::IsNativeWindowInAsh(parent_window)) { |
| 192 media_list.reset(new DesktopMediaListAsh( | 177 media_list.reset(new DesktopMediaListAsh( |
| 193 (show_screens ? DesktopMediaListAsh::SCREENS : 0) | | 178 (show_screens ? DesktopMediaListAsh::SCREENS : 0) | |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 return true; | 212 return true; |
| 228 } | 213 } |
| 229 | 214 |
| 230 void DesktopCaptureChooseDesktopMediaFunction::WebContentsDestroyed() { | 215 void DesktopCaptureChooseDesktopMediaFunction::WebContentsDestroyed() { |
| 231 Cancel(); | 216 Cancel(); |
| 232 } | 217 } |
| 233 | 218 |
| 234 void DesktopCaptureChooseDesktopMediaFunction::OnPickerDialogResults( | 219 void DesktopCaptureChooseDesktopMediaFunction::OnPickerDialogResults( |
| 235 content::DesktopMediaID source) { | 220 content::DesktopMediaID source) { |
| 236 std::string result; | 221 std::string result; |
| 237 if (source.type != content::DesktopMediaID::TYPE_NONE) { | 222 if (source.type != content::DesktopMediaID::TYPE_NONE && |
| 223 web_contents()) { |
| 238 DesktopStreamsRegistry* registry = | 224 DesktopStreamsRegistry* registry = |
| 239 MediaCaptureDevicesDispatcher::GetInstance()-> | 225 MediaCaptureDevicesDispatcher::GetInstance()-> |
| 240 GetDesktopStreamsRegistry(); | 226 GetDesktopStreamsRegistry(); |
| 227 // TODO(miu): Once render_frame_host() is being set, we should register the |
| 228 // exact RenderFrame requesting the stream, not the main RenderFrame. With |
| 229 // that change, also update |
| 230 // MediaCaptureDevicesDispatcher::ProcessDesktopCaptureAccessRequest(). |
| 231 // http://crbug.com/304341 |
| 232 content::RenderFrameHost* const main_frame = web_contents()->GetMainFrame(); |
| 241 result = registry->RegisterStream( | 233 result = registry->RegisterStream( |
| 242 render_process_id_, | 234 main_frame->GetProcess()->GetID(), |
| 243 render_view_id_, | 235 main_frame->GetRoutingID(), |
| 244 origin_, | 236 origin_, |
| 245 source, | 237 source, |
| 246 GetExtension()->name()); | 238 GetExtension()->name()); |
| 247 } | 239 } |
| 248 | 240 |
| 249 SetResult(new base::StringValue(result)); | 241 SetResult(new base::StringValue(result)); |
| 250 SendResponse(true); | 242 SendResponse(true); |
| 251 } | 243 } |
| 252 | 244 |
| 253 DesktopCaptureRequestsRegistry::RequestId::RequestId(int process_id, | 245 DesktopCaptureRequestsRegistry::RequestId::RequestId(int process_id, |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 | 295 |
| 304 void DesktopCaptureRequestsRegistry::CancelRequest(int process_id, | 296 void DesktopCaptureRequestsRegistry::CancelRequest(int process_id, |
| 305 int request_id) { | 297 int request_id) { |
| 306 RequestsMap::iterator it = requests_.find(RequestId(process_id, request_id)); | 298 RequestsMap::iterator it = requests_.find(RequestId(process_id, request_id)); |
| 307 if (it != requests_.end()) | 299 if (it != requests_.end()) |
| 308 it->second->Cancel(); | 300 it->second->Cancel(); |
| 309 } | 301 } |
| 310 | 302 |
| 311 | 303 |
| 312 } // namespace extensions | 304 } // namespace extensions |
| OLD | NEW |