summaryrefslogtreecommitdiffstats
path: root/components/cloud_devices/common/cloud_devices_urls.cc
blob: 5a9a15e46b12990af2fb24c3320b7fd546bc34b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/cloud_devices/common/cloud_devices_urls.h"

#include "base/command_line.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "components/cloud_devices/common/cloud_devices_switches.h"
#include "google_apis/gaia/gaia_urls.h"
#include "net/base/url_util.h"

namespace cloud_devices {

const char kCloudPrintAuthScope[] =
    "https://www.googleapis.com/auth/cloudprint";

const char kCloudDevicesAuthScope[] =
    "https://www.googleapis.com/auth/clouddevices";

const char kCloudPrintLearnMoreURL[] =
    "https://www.google.com/support/cloudprint";

const char kCloudPrintTestPageURL[] =
    "http://www.google.com/landing/cloudprint/enable.html?print=true";

namespace {

// Url must not be matched by "urls" section of
// cloud_print_app/manifest.json. If it's matched, print driver dialog will
// open sign-in page in separate window.
const char kCloudPrintURL[] = "https://www.google.com/cloudprint";

const char kCloudDevicesUrl[] = "https://www.googleapis.com/clouddevices/v1";

}

// Returns the root service URL for the cloud print service.  The default is to
// point at the Google Cloud Print service.  This can be overridden by the
// command line or by the user preferences.
GURL GetCloudPrintURL() {
  const base::CommandLine* command_line =
      base::CommandLine::ForCurrentProcess();
  GURL cloud_print_url(
      command_line->GetSwitchValueASCII(switches::kCloudPrintURL));
  if (cloud_print_url.is_empty())
    cloud_print_url = GURL(kCloudPrintURL);
  return cloud_print_url;
}

GURL GetCloudPrintRelativeURL(const std::string& relative_path) {
  GURL url = GetCloudPrintURL();
  std::string path;
  static const char kURLPathSeparator[] = "/";
  base::TrimString(url.path(), kURLPathSeparator, &path);
  std::string trimmed_path;
  base::TrimString(relative_path, kURLPathSeparator, &trimmed_path);
  path += kURLPathSeparator;
  path += trimmed_path;
  GURL::Replacements replacements;
  replacements.SetPathStr(path);
  return url.ReplaceComponents(replacements);
}

GURL GetCloudPrintSigninURL() {
  GURL url(GaiaUrls::GetInstance()->service_login_url());
  url = net::AppendQueryParameter(url, "service", "cloudprint");
  url = net::AppendQueryParameter(url, "sarp", "1");
  std::string continue_str = GetCloudPrintURL().spec();
  url = net::AppendQueryParameter(url, "continue", continue_str);
  return url;
}

GURL GetCloudPrintAddAccountURL() {
  GURL url(GaiaUrls::GetInstance()->add_account_url());
  url = net::AppendQueryParameter(url, "service", "cloudprint");
  url = net::AppendQueryParameter(url, "sarp", "1");
  std::string continue_str = GetCloudPrintURL().spec();
  url = net::AppendQueryParameter(url, "continue", continue_str);
  return url;
}

bool IsCloudPrintURL(const GURL& url) {
  GURL cloud_print_url = GetCloudPrintURL();
  return url.host() == cloud_print_url.host() &&
         url.scheme() == cloud_print_url.scheme() &&
         base::StartsWith(url.path(), cloud_print_url.path(),
                          base::CompareCase::SENSITIVE);
}

GURL GetCloudPrintEnableURL(const std::string& proxy_id) {
  GURL url = GetCloudPrintRelativeURL("enable_chrome_connector/enable.html");
  url = net::AppendQueryParameter(url, "proxy", proxy_id);
  return url;
}

GURL GetCloudPrintEnableWithSigninURL(const std::string& proxy_id) {
  GURL url(GaiaUrls::GetInstance()->service_login_url());
  url = net::AppendQueryParameter(url, "service", "cloudprint");
  url = net::AppendQueryParameter(url, "sarp", "1");
  std::string continue_str = GetCloudPrintEnableURL(proxy_id).spec();
  return net::AppendQueryParameter(url, "continue", continue_str);
}

GURL GetCloudPrintManageDeviceURL(const std::string& device_id) {
  std::string ref = "printers/" + device_id;
  GURL::Replacements replacements;
  replacements.SetRefStr(ref);
  return GetCloudPrintURL().ReplaceComponents(replacements);
}

GURL GetCloudDevicesURL() {
  const base::CommandLine* command_line =
      base::CommandLine::ForCurrentProcess();
  GURL cloud_print_url(
      command_line->GetSwitchValueASCII(switches::kCloudDevicesURL));
  if (cloud_print_url.is_empty())
    cloud_print_url = GURL(kCloudDevicesUrl);
  return cloud_print_url;
}

GURL GetCloudDevicesRelativeURL(const std::string& relative_path) {
  GURL url = GetCloudDevicesURL();
  std::string path;
  const char kURLPathSeparator[] = "/";
  base::TrimString(url.path(), kURLPathSeparator, &path);
  std::string trimmed_path;
  base::TrimString(relative_path, kURLPathSeparator, &trimmed_path);
  path += kURLPathSeparator;
  path += trimmed_path;
  GURL::Replacements replacements;
  replacements.SetPathStr(path);
  return url.ReplaceComponents(replacements);
}

}  // namespace cloud_devices