gservices_settings.h revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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#ifndef GOOGLE_APIS_GCM_ENGINE_GSERVICES_SETTINGS_H_
6#define GOOGLE_APIS_GCM_ENGINE_GSERVICES_SETTINGS_H_
7
8#include <map>
9#include <string>
10
11#include "base/memory/weak_ptr.h"
12#include "google_apis/gcm/base/gcm_export.h"
13#include "google_apis/gcm/engine/gcm_store.h"
14#include "google_apis/gcm/protocol/checkin.pb.h"
15
16namespace gcm {
17
18// Class responsible for handling G-services settings. It takes care of
19// extracting them from checkin response and storing in GCMStore.
20class GCM_EXPORT GServicesSettings {
21 public:
22  // Create an instance of GServicesSettings class. |gcm_store| is used to store
23  // the settings after they are extracted from checkin response.
24  explicit GServicesSettings(GCMStore* gcm_store);
25  ~GServicesSettings();
26
27  // Udpates the settings based on |checkin_response|.
28  void UpdateFromCheckinResponse(
29      const checkin_proto::AndroidCheckinResponse& checkin_response);
30
31  // Updates the settings based on |load_result|.
32  void UpdateFromLoadResult(const GCMStore::LoadResult& load_result);
33
34  const std::string& digest() const { return digest_; }
35
36  // TODO(fgorski): Consider returning TimeDelta.
37  int64 checkin_interval() const { return checkin_interval_; }
38
39  // TODO(fgorski): Consider returning GURL and use it for validation.
40  const std::string& checkin_url() const { return checkin_url_; }
41
42  // TODO(fgorski): Consider returning GURL and use it for validation.
43  const std::string& mcs_hostname() const { return mcs_hostname_; }
44
45  int mcs_secure_port() const { return mcs_secure_port_; }
46
47  // TODO(fgorski): Consider returning GURL and use it for validation.
48  const std::string& registration_url() const { return registration_url_; }
49
50 private:
51  // Parses the |settings| to fill in specific fields.
52  // TODO(fgorski): Change to a status variable that can be logged to UMA.
53  bool UpdateSettings(const std::map<std::string, std::string>& settings);
54
55  // Callback passed to GCMStore::SetGServicesSettings.
56  void SetGServicesSettingsCallback(bool success);
57
58  // GCM store to persist the settings. Not owned.
59  GCMStore* gcm_store_;
60
61  // Digest (hash) of the settings, used to check whether settings need update.
62  // It is meant to be sent with checkin request, instead of sending the whole
63  // settings table.
64  std::string digest_;
65
66  // Time in seconds between periodic checkins.
67  int64 checkin_interval_;
68
69  // URL that should be used for checkins.
70  std::string checkin_url_;
71
72  // Hostname of the MCS server.
73  std::string mcs_hostname_;
74
75  // Secure port to connect to on MCS sever.
76  int mcs_secure_port_;
77
78  // URL that should be used for regisrations and unregistrations.
79  std::string registration_url_;
80
81  // Factory for creating references in callbacks.
82  base::WeakPtrFactory<GServicesSettings> weak_ptr_factory_;
83
84  DISALLOW_COPY_AND_ASSIGN(GServicesSettings);
85};
86
87}  // namespace gcm
88
89#endif  // GOOGLE_APIS_GCM_ENGINE_GSERVICES_SETTINGS_H_
90