Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / desktop_capture / desktop_capture_api.cc
blobf5dfb0d28b73c5e0416ee6313cfd66055bb8012d
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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/desktop_capture/desktop_capture_api.h"
7 #include "base/command_line.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/extensions/extension_tab_util.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "content/public/browser/render_frame_host.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/common/origin_util.h"
16 #include "net/base/net_util.h"
18 namespace extensions {
20 namespace {
22 const char kNoTabIdError[] = "targetTab doesn't have id field set.";
23 const char kNoUrlError[] = "targetTab doesn't have URL field set.";
24 const char kInvalidOriginError[] = "targetTab.url is not a valid URL.";
25 const char kInvalidTabIdError[] = "Invalid tab specified.";
26 const char kTabUrlNotSecure[] =
27 "URL scheme for the specified tab is not secure.";
28 } // namespace
30 DesktopCaptureChooseDesktopMediaFunction::
31 DesktopCaptureChooseDesktopMediaFunction() {
34 DesktopCaptureChooseDesktopMediaFunction::
35 ~DesktopCaptureChooseDesktopMediaFunction() {
38 bool DesktopCaptureChooseDesktopMediaFunction::RunAsync() {
39 EXTENSION_FUNCTION_VALIDATE(args_->GetSize() > 0);
41 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &request_id_));
42 DesktopCaptureRequestsRegistry::GetInstance()->AddRequest(
43 render_frame_host()->GetProcess()->GetID(), request_id_, this);
45 args_->Remove(0, NULL);
47 scoped_ptr<api::desktop_capture::ChooseDesktopMedia::Params> params =
48 api::desktop_capture::ChooseDesktopMedia::Params::Create(*args_);
49 EXTENSION_FUNCTION_VALIDATE(params.get());
51 // |web_contents| is the WebContents for which the stream is created, and will
52 // also be used to determine where to show the picker's UI.
53 content::WebContents* web_contents = NULL;
54 base::string16 target_name;
55 GURL origin;
56 if (params->target_tab) {
57 if (!params->target_tab->url) {
58 error_ = kNoUrlError;
59 return false;
61 origin = GURL(*(params->target_tab->url)).GetOrigin();
63 if (!origin.is_valid()) {
64 error_ = kInvalidOriginError;
65 return false;
68 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
69 switches::kAllowHttpScreenCapture) &&
70 !content::IsOriginSecure(origin)) {
71 error_ = kTabUrlNotSecure;
72 return false;
74 target_name = base::UTF8ToUTF16(content::IsOriginSecure(origin) ?
75 net::GetHostAndOptionalPort(origin) : origin.spec());
77 if (!params->target_tab->id ||
78 *params->target_tab->id == api::tabs::TAB_ID_NONE) {
79 error_ = kNoTabIdError;
80 return false;
83 if (!ExtensionTabUtil::GetTabById(*(params->target_tab->id), GetProfile(),
84 true, NULL, NULL, &web_contents, NULL)) {
85 error_ = kInvalidTabIdError;
86 return false;
88 DCHECK(web_contents);
89 } else {
90 origin = extension()->url();
91 target_name = base::UTF8ToUTF16(extension()->name());
92 web_contents = GetSenderWebContents();
93 DCHECK(web_contents);
96 return Execute(params->sources, web_contents, origin, target_name);
99 DesktopCaptureCancelChooseDesktopMediaFunction::
100 DesktopCaptureCancelChooseDesktopMediaFunction() {}
102 DesktopCaptureCancelChooseDesktopMediaFunction::
103 ~DesktopCaptureCancelChooseDesktopMediaFunction() {}
105 } // namespace extensions