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.
5 #include "content/browser/geofencing/geofencing_dispatcher_host.h"
7 #include "content/browser/geofencing/geofencing_manager.h"
8 #include "content/browser/service_worker/service_worker_context_core.h"
9 #include "content/browser/service_worker/service_worker_context_wrapper.h"
10 #include "content/browser/service_worker/service_worker_registration.h"
11 #include "content/common/geofencing_messages.h"
12 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h"
16 static const int kMaxRegionIdLength
= 200;
18 GeofencingDispatcherHost::GeofencingDispatcherHost(
19 GeofencingManager
* geofencing_manager
)
20 : BrowserMessageFilter(GeofencingMsgStart
),
21 manager_(geofencing_manager
),
25 GeofencingDispatcherHost::~GeofencingDispatcherHost() {
28 bool GeofencingDispatcherHost::OnMessageReceived(const IPC::Message
& message
) {
30 IPC_BEGIN_MESSAGE_MAP(GeofencingDispatcherHost
, message
)
31 IPC_MESSAGE_HANDLER(GeofencingHostMsg_RegisterRegion
, OnRegisterRegion
)
32 IPC_MESSAGE_HANDLER(GeofencingHostMsg_UnregisterRegion
, OnUnregisterRegion
)
33 IPC_MESSAGE_HANDLER(GeofencingHostMsg_GetRegisteredRegions
,
34 OnGetRegisteredRegions
)
35 IPC_MESSAGE_UNHANDLED(handled
= false)
40 void GeofencingDispatcherHost::OnRegisterRegion(
43 const std::string
& region_id
,
44 const blink::WebCircularGeofencingRegion
& region
,
45 int64 service_worker_registration_id
) {
46 // Sanity check on region_id
47 if (region_id
.length() > kMaxRegionIdLength
) {
48 Send(new GeofencingMsg_RegisterRegionComplete(
49 thread_id
, request_id
, GeofencingStatus::GEOFENCING_STATUS_ERROR
));
53 manager_
->RegisterRegion(
54 service_worker_registration_id
,
57 base::Bind(&GeofencingDispatcherHost::RegisterRegionCompleted
,
58 weak_factory_
.GetWeakPtr(),
63 void GeofencingDispatcherHost::OnUnregisterRegion(
66 const std::string
& region_id
,
67 int64 service_worker_registration_id
) {
68 // Sanity check on region_id
69 if (region_id
.length() > kMaxRegionIdLength
) {
70 Send(new GeofencingMsg_UnregisterRegionComplete(
71 thread_id
, request_id
, GeofencingStatus::GEOFENCING_STATUS_ERROR
));
75 manager_
->UnregisterRegion(
76 service_worker_registration_id
,
78 base::Bind(&GeofencingDispatcherHost::UnregisterRegionCompleted
,
79 weak_factory_
.GetWeakPtr(),
84 void GeofencingDispatcherHost::OnGetRegisteredRegions(
87 int64 service_worker_registration_id
) {
88 GeofencingRegistrations result
;
90 GeofencingStatus status
=
91 manager_
->GetRegisteredRegions(service_worker_registration_id
, &result
);
92 Send(new GeofencingMsg_GetRegisteredRegionsComplete(
93 thread_id
, request_id
, status
, result
));
96 void GeofencingDispatcherHost::RegisterRegionCompleted(
99 GeofencingStatus status
) {
100 Send(new GeofencingMsg_RegisterRegionComplete(thread_id
, request_id
, status
));
103 void GeofencingDispatcherHost::UnregisterRegionCompleted(
106 GeofencingStatus status
) {
107 Send(new GeofencingMsg_UnregisterRegionComplete(
108 thread_id
, request_id
, status
));
111 } // namespace content