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.
4
5#include "components/cloud_devices/common/cloud_devices_urls.h"
6
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"
13
14namespace cloud_devices {
15
16const char kCloudPrintAuthScope[] =
17    "https://www.googleapis.com/auth/cloudprint";
18
19const char kCloudDevicesAuthScope[] =
20    "https://www.googleapis.com/auth/clouddevices";
21
22const char kCloudPrintLearnMoreURL[] =
23    "https://www.google.com/support/cloudprint";
24
25const char kCloudPrintTestPageURL[] =
26    "http://www.google.com/landing/cloudprint/enable.html?print=true";
27
28namespace {
29
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.
33const char kCloudPrintURL[] = "https://www.google.com/cloudprint";
34
35const char kCloudDevicesUrl[] = "https://www.googleapis.com/clouddevices/v1";
36
37}
38
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.
42GURL GetCloudPrintURL() {
43  const CommandLine* command_line = CommandLine::ForCurrentProcess();
44  GURL cloud_print_url(
45      command_line->GetSwitchValueASCII(switches::kCloudPrintURL));
46  if (cloud_print_url.is_empty())
47    cloud_print_url = GURL(kCloudPrintURL);
48  return cloud_print_url;
49}
50
51GURL GetCloudPrintRelativeURL(const std::string& relative_path) {
52  GURL url = GetCloudPrintURL();
53  std::string path;
54  static const char kURLPathSeparator[] = "/";
55  base::TrimString(url.path(), kURLPathSeparator, &path);
56  std::string trimmed_path;
57  base::TrimString(relative_path, kURLPathSeparator, &trimmed_path);
58  path += kURLPathSeparator;
59  path += trimmed_path;
60  GURL::Replacements replacements;
61  replacements.SetPathStr(path);
62  return url.ReplaceComponents(replacements);
63}
64
65GURL GetCloudPrintSigninURL() {
66  GURL url(GaiaUrls::GetInstance()->service_login_url());
67  url = net::AppendQueryParameter(url, "service", "cloudprint");
68  url = net::AppendQueryParameter(url, "sarp", "1");
69  std::string continue_str = GetCloudPrintURL().spec();
70  url = net::AppendQueryParameter(url, "continue", continue_str);
71  return url;
72}
73
74GURL GetCloudPrintAddAccountURL() {
75  GURL url(GaiaUrls::GetInstance()->add_account_url());
76  url = net::AppendQueryParameter(url, "service", "cloudprint");
77  url = net::AppendQueryParameter(url, "sarp", "1");
78  std::string continue_str = GetCloudPrintURL().spec();
79  url = net::AppendQueryParameter(url, "continue", continue_str);
80  return url;
81}
82
83GURL GetCloudPrintEnableURL(const std::string& proxy_id) {
84  GURL url = GetCloudPrintRelativeURL("enable_chrome_connector/enable.html");
85  url = net::AppendQueryParameter(url, "proxy", proxy_id);
86  return url;
87}
88
89GURL GetCloudPrintEnableWithSigninURL(const std::string& proxy_id) {
90  GURL url(GaiaUrls::GetInstance()->service_login_url());
91  url = net::AppendQueryParameter(url, "service", "cloudprint");
92  url = net::AppendQueryParameter(url, "sarp", "1");
93  std::string continue_str = GetCloudPrintEnableURL(proxy_id).spec();
94  return net::AppendQueryParameter(url, "continue", continue_str);
95}
96
97GURL GetCloudPrintManageDeviceURL(const std::string& device_id) {
98  std::string ref = "printers/" + device_id;
99  GURL::Replacements replacements;
100  replacements.SetRefStr(ref);
101  return GetCloudPrintURL().ReplaceComponents(replacements);
102}
103
104GURL GetCloudDevicesURL() {
105  const CommandLine* command_line = CommandLine::ForCurrentProcess();
106  GURL cloud_print_url(
107      command_line->GetSwitchValueASCII(switches::kCloudDevicesURL));
108  if (cloud_print_url.is_empty())
109    cloud_print_url = GURL(kCloudDevicesUrl);
110  return cloud_print_url;
111}
112
113GURL GetCloudDevicesRelativeURL(const std::string& relative_path) {
114  GURL url = GetCloudDevicesURL();
115  std::string path;
116  const char kURLPathSeparator[] = "/";
117  base::TrimString(url.path(), kURLPathSeparator, &path);
118  std::string trimmed_path;
119  base::TrimString(relative_path, kURLPathSeparator, &trimmed_path);
120  path += kURLPathSeparator;
121  path += trimmed_path;
122  GURL::Replacements replacements;
123  replacements.SetPathStr(path);
124  return url.ReplaceComponents(replacements);
125}
126
127}  // namespace cloud_devices
128