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/update_client/ping_manager.h"
6 
7 #include <stddef.h>
8 
9 #include <memory>
10 #include <string>
11 #include <utility>
12 #include <vector>
13 
14 #include "base/bind.h"
15 #include "base/check_op.h"
16 #include "base/location.h"
17 #include "base/macros.h"
18 #include "base/threading/thread_task_runner_handle.h"
19 #include "components/update_client/component.h"
20 #include "components/update_client/configurator.h"
21 #include "components/update_client/protocol_definition.h"
22 #include "components/update_client/protocol_handler.h"
23 #include "components/update_client/protocol_serializer.h"
24 #include "components/update_client/request_sender.h"
25 #include "components/update_client/utils.h"
26 #include "url/gurl.h"
27 
28 namespace update_client {
29 
30 namespace {
31 
32 const int kErrorNoEvents = -1;
33 const int kErrorNoUrl = -2;
34 
35 // An instance of this class can send only one ping.
36 class PingSender : public base::RefCountedThreadSafe<PingSender> {
37  public:
38   using Callback = PingManager::Callback;
39   explicit PingSender(scoped_refptr<Configurator> config);
40   void SendPing(const Component& component, Callback callback);
41 
42  protected:
43   virtual ~PingSender();
44 
45  private:
46   friend class base::RefCountedThreadSafe<PingSender>;
47   void SendPingComplete(int error,
48                         const std::string& response,
49                         int retry_after_sec);
50 
51   THREAD_CHECKER(thread_checker_);
52 
53   const scoped_refptr<Configurator> config_;
54   Callback callback_;
55   std::unique_ptr<RequestSender> request_sender_;
56 
57   DISALLOW_COPY_AND_ASSIGN(PingSender);
58 };
59 
PingSender(scoped_refptr<Configurator> config)60 PingSender::PingSender(scoped_refptr<Configurator> config) : config_(config) {}
61 
~PingSender()62 PingSender::~PingSender() {
63   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
64 }
65 
SendPing(const Component & component,Callback callback)66 void PingSender::SendPing(const Component& component, Callback callback) {
67   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
68 
69   if (component.events().empty()) {
70     base::ThreadTaskRunnerHandle::Get()->PostTask(
71         FROM_HERE, base::BindOnce(std::move(callback), kErrorNoEvents, ""));
72     return;
73   }
74 
75   DCHECK(component.crx_component());
76 
77   auto urls(config_->PingUrl());
78   if (component.crx_component()->requires_network_encryption)
79     RemoveUnsecureUrls(&urls);
80 
81   if (urls.empty()) {
82     base::ThreadTaskRunnerHandle::Get()->PostTask(
83         FROM_HERE, base::BindOnce(std::move(callback), kErrorNoUrl, ""));
84     return;
85   }
86 
87   callback_ = std::move(callback);
88 
89   std::vector<protocol_request::App> apps;
90   apps.push_back(MakeProtocolApp(component.id(),
91                                  component.crx_component()->version,
92                                  component.GetEvents()));
93   request_sender_ = std::make_unique<RequestSender>(config_);
94   request_sender_->Send(
95       urls, {},
96       config_->GetProtocolHandlerFactory()->CreateSerializer()->Serialize(
97           MakeProtocolRequest(
98               component.session_id(), config_->GetProdId(),
99               config_->GetBrowserVersion().GetString(), config_->GetLang(),
100               config_->GetChannel(), config_->GetOSLongName(),
101               config_->GetDownloadPreference(), config_->ExtraRequestParams(),
102               nullptr, std::move(apps))),
103       false, base::BindOnce(&PingSender::SendPingComplete, this));
104 }
105 
SendPingComplete(int error,const std::string & response,int retry_after_sec)106 void PingSender::SendPingComplete(int error,
107                                   const std::string& response,
108                                   int retry_after_sec) {
109   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
110   std::move(callback_).Run(error, response);
111 }
112 
113 }  // namespace
114 
PingManager(scoped_refptr<Configurator> config)115 PingManager::PingManager(scoped_refptr<Configurator> config)
116     : config_(config) {}
117 
~PingManager()118 PingManager::~PingManager() {
119   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
120 }
121 
SendPing(const Component & component,Callback callback)122 void PingManager::SendPing(const Component& component, Callback callback) {
123   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
124 
125   auto ping_sender = base::MakeRefCounted<PingSender>(config_);
126   ping_sender->SendPing(component, std::move(callback));
127 }
128 
129 }  // namespace update_client
130