cloud_print_url.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2011 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 "chrome/browser/printing/cloud_print/cloud_print_url.h"
6
7#include "base/command_line.h"
8#include "base/logging.h"
9#include "base/stringprintf.h"
10#include "chrome/browser/google/google_util.h"
11#include "chrome/browser/prefs/pref_service.h"
12#include "chrome/browser/profiles/profile.h"
13#include "chrome/common/chrome_switches.h"
14#include "chrome/common/pref_names.h"
15#include "googleurl/src/gurl.h"
16
17const char kDefaultCloudPrintServiceURL[] = "https://www.google.com/cloudprint";
18const char kDefaultCloudPrintSigninURL[] =
19    "https://accounts.google.com/ServiceLogin?"
20    "service=cloudprint&continue=https%3A%2F%2Fwww.google.com%2Fcloudprint";
21
22const char kLearnMoreURL[] =
23    "https://www.google.com/support/cloudprint";
24const char kTestPageURL[] =
25    "http://www.google.com/landing/cloudprint/enable.html?print=true";
26
27void CloudPrintURL::RegisterPreferences() {
28  DCHECK(profile_);
29  PrefService* pref_service = profile_->GetPrefs();
30  if (!pref_service->FindPreference(prefs::kCloudPrintServiceURL)) {
31    pref_service->RegisterStringPref(prefs::kCloudPrintServiceURL,
32                                     kDefaultCloudPrintServiceURL,
33                                     PrefService::UNSYNCABLE_PREF);
34  }
35  if (!pref_service->FindPreference(prefs::kCloudPrintSigninURL)) {
36    pref_service->RegisterStringPref(prefs::kCloudPrintSigninURL,
37                                     kDefaultCloudPrintSigninURL,
38                                     PrefService::UNSYNCABLE_PREF);
39  }
40}
41
42// Returns the root service URL for the cloud print service.  The default is to
43// point at the Google Cloud Print service.  This can be overridden by the
44// command line or by the user preferences.
45GURL CloudPrintURL::GetCloudPrintServiceURL() {
46  DCHECK(profile_);
47  RegisterPreferences();
48
49  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
50  GURL cloud_print_service_url = GURL(command_line.GetSwitchValueASCII(
51      switches::kCloudPrintServiceURL));
52  if (cloud_print_service_url.is_empty()) {
53    cloud_print_service_url = GURL(
54        profile_->GetPrefs()->GetString(prefs::kCloudPrintServiceURL));
55  }
56  return cloud_print_service_url;
57}
58
59GURL CloudPrintURL::GetCloudPrintSigninURL() {
60  DCHECK(profile_);
61  RegisterPreferences();
62
63  GURL cloud_print_signin_url = GURL(
64      profile_->GetPrefs()->GetString(prefs::kCloudPrintSigninURL));
65  return google_util::AppendGoogleLocaleParam(cloud_print_signin_url);
66}
67
68GURL CloudPrintURL::GetCloudPrintServiceDialogURL() {
69  GURL cloud_print_service_url = GetCloudPrintServiceURL();
70  std::string path(cloud_print_service_url.path() + "/client/dialog.html");
71  GURL::Replacements replacements;
72  replacements.SetPathStr(path);
73  GURL cloud_print_dialog_url = cloud_print_service_url.ReplaceComponents(
74      replacements);
75  return google_util::AppendGoogleLocaleParam(cloud_print_dialog_url);
76}
77
78GURL CloudPrintURL::GetCloudPrintServiceManageURL() {
79  GURL cloud_print_service_url = GetCloudPrintServiceURL();
80  std::string path(cloud_print_service_url.path() + "/manage.html");
81  GURL::Replacements replacements;
82  replacements.SetPathStr(path);
83  GURL cloud_print_manage_url = cloud_print_service_url.ReplaceComponents(
84      replacements);
85  return cloud_print_manage_url;
86}
87
88GURL CloudPrintURL::GetCloudPrintServiceEnableURL(
89    const std::string& proxy_id) {
90  GURL cloud_print_service_url = GetCloudPrintServiceURL();
91  std::string path(cloud_print_service_url.path() +
92      "/enable_chrome_connector/enable.html");
93  GURL::Replacements replacements;
94  replacements.SetPathStr(path);
95  std::string query = StringPrintf("proxy=%s", proxy_id.c_str());
96  replacements.SetQueryStr(query);
97  GURL cloud_print_enable_url = cloud_print_service_url.ReplaceComponents(
98      replacements);
99  return cloud_print_enable_url;
100}
101
102GURL CloudPrintURL::GetCloudPrintLearnMoreURL() {
103  GURL cloud_print_learn_more_url(kLearnMoreURL);
104  return cloud_print_learn_more_url;
105}
106
107GURL CloudPrintURL::GetCloudPrintTestPageURL() {
108  GURL cloud_print_learn_more_url(kTestPageURL);
109  return cloud_print_learn_more_url;
110}
111