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
3 // found in the LICENSE file.
5 #include "components/cloud_devices/common/cloud_devices_urls.h"
7 #include "base/command_line.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "components/cloud_devices/common/cloud_devices_switches.h"
11 #include "google_apis/gaia/gaia_urls.h"
12 #include "net/base/url_util.h"
14 namespace cloud_devices
{
16 const char kCloudPrintAuthScope
[] =
17 "https://www.googleapis.com/auth/cloudprint";
19 const char kCloudDevicesAuthScope
[] =
20 "https://www.googleapis.com/auth/clouddevices";
22 const char kCloudPrintLearnMoreURL
[] =
23 "https://www.google.com/support/cloudprint";
25 const char kCloudPrintTestPageURL
[] =
26 "http://www.google.com/landing/cloudprint/enable.html?print=true";
30 // Url must not be matched by "urls" section of
31 // cloud_print_app/manifest.json. If it's matched, print driver dialog will
32 // open sign-in page in separate window.
33 const char kCloudPrintURL
[] = "https://www.google.com/cloudprint";
35 const char kCloudDevicesUrl
[] = "https://www.googleapis.com/clouddevices/v1";
39 // Returns the root service URL for the cloud print service. The default is to
40 // point at the Google Cloud Print service. This can be overridden by the
41 // command line or by the user preferences.
42 GURL
GetCloudPrintURL() {
43 const base::CommandLine
* command_line
=
44 base::CommandLine::ForCurrentProcess();
46 command_line
->GetSwitchValueASCII(switches::kCloudPrintURL
));
47 if (cloud_print_url
.is_empty())
48 cloud_print_url
= GURL(kCloudPrintURL
);
49 return cloud_print_url
;
52 GURL
GetCloudPrintRelativeURL(const std::string
& relative_path
) {
53 GURL url
= GetCloudPrintURL();
55 static const char kURLPathSeparator
[] = "/";
56 base::TrimString(url
.path(), kURLPathSeparator
, &path
);
57 std::string trimmed_path
;
58 base::TrimString(relative_path
, kURLPathSeparator
, &trimmed_path
);
59 path
+= kURLPathSeparator
;
61 GURL::Replacements replacements
;
62 replacements
.SetPathStr(path
);
63 return url
.ReplaceComponents(replacements
);
66 GURL
GetCloudPrintSigninURL() {
67 GURL
url(GaiaUrls::GetInstance()->service_login_url());
68 url
= net::AppendQueryParameter(url
, "service", "cloudprint");
69 url
= net::AppendQueryParameter(url
, "sarp", "1");
70 std::string continue_str
= GetCloudPrintURL().spec();
71 url
= net::AppendQueryParameter(url
, "continue", continue_str
);
75 GURL
GetCloudPrintAddAccountURL() {
76 GURL
url(GaiaUrls::GetInstance()->add_account_url());
77 url
= net::AppendQueryParameter(url
, "service", "cloudprint");
78 url
= net::AppendQueryParameter(url
, "sarp", "1");
79 std::string continue_str
= GetCloudPrintURL().spec();
80 url
= net::AppendQueryParameter(url
, "continue", continue_str
);
84 GURL
GetCloudPrintEnableURL(const std::string
& proxy_id
) {
85 GURL url
= GetCloudPrintRelativeURL("enable_chrome_connector/enable.html");
86 url
= net::AppendQueryParameter(url
, "proxy", proxy_id
);
90 GURL
GetCloudPrintEnableWithSigninURL(const std::string
& proxy_id
) {
91 GURL
url(GaiaUrls::GetInstance()->service_login_url());
92 url
= net::AppendQueryParameter(url
, "service", "cloudprint");
93 url
= net::AppendQueryParameter(url
, "sarp", "1");
94 std::string continue_str
= GetCloudPrintEnableURL(proxy_id
).spec();
95 return net::AppendQueryParameter(url
, "continue", continue_str
);
98 GURL
GetCloudPrintManageDeviceURL(const std::string
& device_id
) {
99 std::string ref
= "printers/" + device_id
;
100 GURL::Replacements replacements
;
101 replacements
.SetRefStr(ref
);
102 return GetCloudPrintURL().ReplaceComponents(replacements
);
105 GURL
GetCloudDevicesURL() {
106 const base::CommandLine
* command_line
=
107 base::CommandLine::ForCurrentProcess();
108 GURL
cloud_print_url(
109 command_line
->GetSwitchValueASCII(switches::kCloudDevicesURL
));
110 if (cloud_print_url
.is_empty())
111 cloud_print_url
= GURL(kCloudDevicesUrl
);
112 return cloud_print_url
;
115 GURL
GetCloudDevicesRelativeURL(const std::string
& relative_path
) {
116 GURL url
= GetCloudDevicesURL();
118 const char kURLPathSeparator
[] = "/";
119 base::TrimString(url
.path(), kURLPathSeparator
, &path
);
120 std::string trimmed_path
;
121 base::TrimString(relative_path
, kURLPathSeparator
, &trimmed_path
);
122 path
+= kURLPathSeparator
;
123 path
+= trimmed_path
;
124 GURL::Replacements replacements
;
125 replacements
.SetPathStr(path
);
126 return url
.ReplaceComponents(replacements
);
129 } // namespace cloud_devices