summaryrefslogtreecommitdiffstats
path: root/components/omaha_query_params
diff options
context:
space:
mode:
authoryoz@chromium.org <yoz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-06-26 00:01:31 +0000
committeryoz@chromium.org <yoz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-06-26 00:01:31 +0000
commit8f4b69e5058bad5559e07b33c258ec0e5adff1b1 (patch)
treeb7e7444fb54b28d562724517d3148291e2d4b048 /components/omaha_query_params
parent3a4bd5dc9d0f9c5258dfcf5399ee876e263ae3a3 (diff)
downloadchromium_src-8f4b69e5058bad5559e07b33c258ec0e5adff1b1.zip
chromium_src-8f4b69e5058bad5559e07b33c258ec0e5adff1b1.tar.gz
chromium_src-8f4b69e5058bad5559e07b33c258ec0e5adff1b1.tar.bz2
Move OmahaQueryParams to a component and add a delegate interface.
BUG=376554 Review URL: https://codereview.chromium.org/333353005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@279867 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components/omaha_query_params')
-rw-r--r--components/omaha_query_params/DEPS4
-rw-r--r--components/omaha_query_params/OWNERS2
-rw-r--r--components/omaha_query_params/omaha_query_params.cc134
-rw-r--r--components/omaha_query_params/omaha_query_params.h59
-rw-r--r--components/omaha_query_params/omaha_query_params_delegate.cc15
-rw-r--r--components/omaha_query_params/omaha_query_params_delegate.h32
-rw-r--r--components/omaha_query_params/omaha_query_params_unittest.cc55
7 files changed, 301 insertions, 0 deletions
diff --git a/components/omaha_query_params/DEPS b/components/omaha_query_params/DEPS
new file mode 100644
index 0000000..beabace
--- /dev/null
+++ b/components/omaha_query_params/DEPS
@@ -0,0 +1,4 @@
+include_rules = [
+ "+base",
+ "+testing",
+]
diff --git a/components/omaha_query_params/OWNERS b/components/omaha_query_params/OWNERS
new file mode 100644
index 0000000..c089b54
--- /dev/null
+++ b/components/omaha_query_params/OWNERS
@@ -0,0 +1,2 @@
+asargent@chromium.org
+cpu@chromium.org
diff --git a/components/omaha_query_params/omaha_query_params.cc b/components/omaha_query_params/omaha_query_params.cc
new file mode 100644
index 0000000..b04116db
--- /dev/null
+++ b/components/omaha_query_params/omaha_query_params.cc
@@ -0,0 +1,134 @@
+// 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/omaha_query_params/omaha_query_params.h"
+
+#include "base/compiler_specific.h"
+#include "base/logging.h"
+#include "base/strings/stringprintf.h"
+#include "base/win/windows_version.h"
+#include "components/omaha_query_params/omaha_query_params_delegate.h"
+
+namespace omaha_query_params {
+
+namespace {
+
+const char kUnknown[] = "unknown";
+
+// The request extra information is the OS and architecture, this helps
+// the server select the right package to be delivered.
+const char kOs[] =
+#if defined(OS_MACOSX)
+ "mac";
+#elif defined(OS_WIN)
+ "win";
+#elif defined(OS_ANDROID)
+ "android";
+#elif defined(OS_CHROMEOS)
+ "cros";
+#elif defined(OS_LINUX)
+ "linux";
+#elif defined(OS_OPENBSD)
+ "openbsd";
+#else
+#error "unknown os"
+#endif
+
+const char kArch[] =
+#if defined(__amd64__) || defined(_WIN64)
+ "x64";
+#elif defined(__i386__) || defined(_WIN32)
+ "x86";
+#elif defined(__arm__)
+ "arm";
+#elif defined(__aarch64__)
+ "arm64";
+#elif defined(__mips__)
+ "mipsel";
+#else
+#error "unknown arch"
+#endif
+
+const char kChrome[] = "chrome";
+
+#if defined(GOOGLE_CHROME_BUILD)
+const char kChromeCrx[] = "chromecrx";
+#else
+const char kChromiumCrx[] = "chromiumcrx";
+#endif // defined(GOOGLE_CHROME_BUILD)
+
+OmahaQueryParamsDelegate* g_delegate = NULL;
+
+} // namespace
+
+// static
+std::string OmahaQueryParams::Get(ProdId prod) {
+ return base::StringPrintf(
+ "os=%s&arch=%s&nacl_arch=%s&prod=%s%s",
+ kOs,
+ kArch,
+ GetNaclArch(),
+ GetProdIdString(prod),
+ g_delegate ? g_delegate->GetExtraParams().c_str() : "");
+}
+
+// static
+const char* OmahaQueryParams::GetProdIdString(OmahaQueryParams::ProdId prod) {
+ switch (prod) {
+ case OmahaQueryParams::CHROME:
+ return kChrome;
+ break;
+ case OmahaQueryParams::CRX:
+#if defined(GOOGLE_CHROME_BUILD)
+ return kChromeCrx;
+#else
+ return kChromiumCrx;
+#endif
+ break;
+ }
+ return kUnknown;
+}
+
+// static
+const char* OmahaQueryParams::GetOS() {
+ return kOs;
+}
+
+// static
+const char* OmahaQueryParams::GetArch() {
+ return kArch;
+}
+
+// static
+const char* OmahaQueryParams::GetNaclArch() {
+#if defined(ARCH_CPU_X86_FAMILY)
+#if defined(ARCH_CPU_X86_64)
+ return "x86-64";
+#elif defined(OS_WIN)
+ bool x86_64 = (base::win::OSInfo::GetInstance()->wow64_status() ==
+ base::win::OSInfo::WOW64_ENABLED);
+ return x86_64 ? "x86-64" : "x86-32";
+#else
+ return "x86-32";
+#endif
+#elif defined(ARCH_CPU_ARMEL)
+ return "arm";
+#elif defined(ARCH_CPU_ARM64)
+ return "arm64";
+#elif defined(ARCH_CPU_MIPSEL)
+ return "mips32";
+#else
+// NOTE: when adding new values here, please remember to update the
+// comment in the .h file about possible return values from this function.
+#error "You need to add support for your architecture here"
+#endif
+}
+
+// static
+void OmahaQueryParams::SetDelegate(OmahaQueryParamsDelegate* delegate) {
+ DCHECK(!g_delegate || !delegate);
+ g_delegate = delegate;
+}
+
+} // namespace omaha_query_params
diff --git a/components/omaha_query_params/omaha_query_params.h b/components/omaha_query_params/omaha_query_params.h
new file mode 100644
index 0000000..d92798a
--- /dev/null
+++ b/components/omaha_query_params/omaha_query_params.h
@@ -0,0 +1,59 @@
+// 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.
+
+#ifndef COMPONENTS_OMAHA_QUERY_PARAMS_OMAHA_QUERY_PARAMS_H_
+#define COMPONENTS_OMAHA_QUERY_PARAMS_OMAHA_QUERY_PARAMS_H_
+
+#include <string>
+
+#include "base/basictypes.h"
+
+namespace omaha_query_params {
+
+class OmahaQueryParamsDelegate;
+
+// Generates a string of URL query parameters to be used when getting
+// component and extension updates. These parameters generally remain
+// fixed for a particular build. Embedders can use the delegate to
+// define different implementations. This should be used only in the
+// browser process.
+class OmahaQueryParams {
+ public:
+ enum ProdId {
+ CHROME = 0,
+ CRX,
+ };
+
+ // Generates a string of URL query parameters for Omaha. Includes the
+ // following fields: os, arch, prod, prodchannel, prodversion, lang.
+ static std::string Get(ProdId prod);
+
+ // Returns the value we use for the "prod=" parameter. Possible return values
+ // include "chrome", "chromecrx", "chromiumcrx", and "unknown".
+ static const char* GetProdIdString(ProdId prod);
+
+ // Returns the value we use for the "os=" parameter. Possible return values
+ // include: "mac", "win", "android", "cros", "linux", and "openbsd".
+ static const char* GetOS();
+
+ // Returns the value we use for the "arch=" parameter. Possible return values
+ // include: "x86", "x64", and "arm".
+ static const char* GetArch();
+
+ // Returns the value we use for the "nacl_arch" parameter. Note that this may
+ // be different from the "arch" parameter above (e.g. one may be 32-bit and
+ // the other 64-bit). Possible return values include: "x86-32", "x86-64",
+ // "arm", and "mips32".
+ static const char* GetNaclArch();
+
+ // Use this delegate.
+ static void SetDelegate(OmahaQueryParamsDelegate* delegate);
+
+ private:
+ DISALLOW_IMPLICIT_CONSTRUCTORS(OmahaQueryParams);
+};
+
+} // namespace omaha_query_params
+
+#endif // COMPONENTS_OMAHA_QUERY_PARAMS_OMAHA_QUERY_PARAMS_H_
diff --git a/components/omaha_query_params/omaha_query_params_delegate.cc b/components/omaha_query_params/omaha_query_params_delegate.cc
new file mode 100644
index 0000000..9d79a94
--- /dev/null
+++ b/components/omaha_query_params/omaha_query_params_delegate.cc
@@ -0,0 +1,15 @@
+// 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/omaha_query_params/omaha_query_params_delegate.h"
+
+namespace omaha_query_params {
+
+OmahaQueryParamsDelegate::OmahaQueryParamsDelegate() {
+}
+
+OmahaQueryParamsDelegate::~OmahaQueryParamsDelegate() {
+}
+
+} // namespace omaha_query_params
diff --git a/components/omaha_query_params/omaha_query_params_delegate.h b/components/omaha_query_params/omaha_query_params_delegate.h
new file mode 100644
index 0000000..ebea760
--- /dev/null
+++ b/components/omaha_query_params/omaha_query_params_delegate.h
@@ -0,0 +1,32 @@
+// 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.
+
+#ifndef COMPONENTS_OMAHA_QUERY_PARAMS_OMAHA_QUERY_PARAMS_DELEGATE_H_
+#define COMPONENTS_OMAHA_QUERY_PARAMS_OMAHA_QUERY_PARAMS_DELEGATE_H_
+
+#include <string>
+
+#include "base/basictypes.h"
+
+namespace omaha_query_params {
+
+// Embedders can specify an OmahaQueryParamsDelegate to provide additional
+// custom parameters. If not specified (Set is never called), no additional
+// parameters are added.
+class OmahaQueryParamsDelegate {
+ public:
+ OmahaQueryParamsDelegate();
+ virtual ~OmahaQueryParamsDelegate();
+
+ // Returns additional parameters, if any. If there are any parameters, the
+ // string should begin with a & character.
+ virtual std::string GetExtraParams() = 0;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(OmahaQueryParamsDelegate);
+};
+
+} // namespace omaha_query_params
+
+#endif // COMPONENTS_OMAHA_QUERY_PARAMS_OMAHA_QUERY_PARAMS_DELEGATE_H_
diff --git a/components/omaha_query_params/omaha_query_params_unittest.cc b/components/omaha_query_params/omaha_query_params_unittest.cc
new file mode 100644
index 0000000..37a97e9
--- /dev/null
+++ b/components/omaha_query_params/omaha_query_params_unittest.cc
@@ -0,0 +1,55 @@
+// 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 "base/strings/stringprintf.h"
+#include "components/omaha_query_params/omaha_query_params.h"
+#include "components/omaha_query_params/omaha_query_params_delegate.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using base::StringPrintf;
+
+namespace omaha_query_params {
+
+namespace {
+
+bool Contains(const std::string& source, const std::string& target) {
+ return source.find(target) != std::string::npos;
+}
+
+class TestOmahaQueryParamsDelegate : public OmahaQueryParamsDelegate {
+ virtual std::string GetExtraParams() OVERRIDE { return "&cat=dog"; }
+};
+
+} // namespace
+
+void TestParams(OmahaQueryParams::ProdId prod_id, bool extra_params) {
+ std::string params = OmahaQueryParams::Get(prod_id);
+
+ // This doesn't so much test what the values are (since that would be an
+ // almost exact duplication of code with omaha_query_params.cc, and wouldn't
+ // really test anything) as it is a verification that all the params are
+ // present in the generated string.
+ EXPECT_TRUE(
+ Contains(params, StringPrintf("os=%s", OmahaQueryParams::GetOS())));
+ EXPECT_TRUE(
+ Contains(params, StringPrintf("arch=%s", OmahaQueryParams::GetArch())));
+ EXPECT_TRUE(Contains(
+ params,
+ StringPrintf("prod=%s", OmahaQueryParams::GetProdIdString(prod_id))));
+ if (extra_params)
+ EXPECT_TRUE(Contains(params, "cat=dog"));
+}
+
+TEST(OmahaQueryParamsTest, GetParams) {
+ TestParams(OmahaQueryParams::CRX, false);
+ TestParams(OmahaQueryParams::CHROME, false);
+
+ TestOmahaQueryParamsDelegate delegate;
+ OmahaQueryParams::SetDelegate(&delegate);
+
+ TestParams(OmahaQueryParams::CRX, true);
+ TestParams(OmahaQueryParams::CHROME, true);
+}
+
+} // namespace omaha_query_params