summaryrefslogtreecommitdiffstats
path: root/content/child/geofencing/geofencing_dispatcher.h
blob: 24fc89936d72371011fd282e97347be7915624c6 (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
// 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 CONTENT_CHILD_GEOFENCING_GEOFENCING_DISPATCHER_H_
#define CONTENT_CHILD_GEOFENCING_GEOFENCING_DISPATCHER_H_

#include "base/id_map.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "content/child/worker_task_runner.h"
#include "content/common/geofencing_status.h"
#include "third_party/WebKit/public/platform/WebGeofencingProvider.h"

namespace base {
class MessageLoop;
class TaskRunner;
}

namespace IPC {
class Message;
}

namespace content {
class ThreadSafeSender;

class GeofencingDispatcher : public WorkerTaskRunner::Observer {
 public:
  explicit GeofencingDispatcher(ThreadSafeSender* sender);
  virtual ~GeofencingDispatcher();

  bool Send(IPC::Message* msg);
  void OnMessageReceived(const IPC::Message& msg);

  // Corresponding to WebGeofencingProvider methods.
  void RegisterRegion(const blink::WebString& region_id,
                      const blink::WebCircularGeofencingRegion& region,
                      blink::WebGeofencingCallbacks* callbacks);
  void UnregisterRegion(const blink::WebString& region_id,
                        blink::WebGeofencingCallbacks* callbacks);
  void GetRegisteredRegions(blink::WebGeofencingRegionsCallbacks* callbacks);

  // |thread_safe_sender| needs to be passed in because if the call leads to
  // construction it will be needed.
  static GeofencingDispatcher* GetOrCreateThreadSpecificInstance(
      ThreadSafeSender* thread_safe_sender);

  // Unlike GetOrCreateThreadSpecificInstance() this doesn't create a new
  // instance if thread-local instance doesn't exist.
  static GeofencingDispatcher* GetThreadSpecificInstance();

 private:
  void OnRegisterRegionComplete(int thread_id,
                                int request_id,
                                GeofencingStatus status);
  void OnUnregisterRegionComplete(int thread_id,
                                  int request_id,
                                  GeofencingStatus status);
  void OnGetRegisteredRegionsComplete(
      int thread_id,
      int request_id,
      GeofencingStatus status,
      const std::map<std::string, blink::WebCircularGeofencingRegion>& regions);

  // WorkerTaskRunner::Observer implementation.
  virtual void OnWorkerRunLoopStopped() override;

  scoped_refptr<ThreadSafeSender> thread_safe_sender_;
  IDMap<blink::WebGeofencingCallbacks, IDMapOwnPointer>
      region_registration_requests_;
  IDMap<blink::WebGeofencingCallbacks, IDMapOwnPointer>
      region_unregistration_requests_;
  IDMap<blink::WebGeofencingRegionsCallbacks, IDMapOwnPointer>
      get_registered_regions_requests_;

  DISALLOW_COPY_AND_ASSIGN(GeofencingDispatcher);
};

}  // namespace content

#endif  // CONTENT_CHILD_GEOFENCING_GEOFENCING_DISPATCHER_H_