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_FORWARD(GeofencingHostMsg_SetMockProvider
, manager_
.get(),
36 GeofencingManager::SetMockProvider
)
37 IPC_MESSAGE_FORWARD(GeofencingHostMsg_SetMockPosition
, manager_
.get(),
38 GeofencingManager::SetMockPosition
)
39 IPC_MESSAGE_UNHANDLED(handled
= false)
44 void GeofencingDispatcherHost::OnRegisterRegion(
47 const std::string
& region_id
,
48 const blink::WebCircularGeofencingRegion
& region
,
49 int64 service_worker_registration_id
) {
50 // Sanity check on region_id
51 if (region_id
.length() > kMaxRegionIdLength
) {
52 Send(new GeofencingMsg_RegisterRegionComplete(
53 thread_id
, request_id
, GeofencingStatus::GEOFENCING_STATUS_ERROR
));
57 manager_
->RegisterRegion(
58 service_worker_registration_id
,
61 base::Bind(&GeofencingDispatcherHost::RegisterRegionCompleted
,
62 weak_factory_
.GetWeakPtr(),
67 void GeofencingDispatcherHost::OnUnregisterRegion(
70 const std::string
& region_id
,
71 int64 service_worker_registration_id
) {
72 // Sanity check on region_id
73 if (region_id
.length() > kMaxRegionIdLength
) {
74 Send(new GeofencingMsg_UnregisterRegionComplete(
75 thread_id
, request_id
, GeofencingStatus::GEOFENCING_STATUS_ERROR
));
79 manager_
->UnregisterRegion(
80 service_worker_registration_id
,
82 base::Bind(&GeofencingDispatcherHost::UnregisterRegionCompleted
,
83 weak_factory_
.GetWeakPtr(),
88 void GeofencingDispatcherHost::OnGetRegisteredRegions(
91 int64 service_worker_registration_id
) {
92 GeofencingRegistrations result
;
94 GeofencingStatus status
=
95 manager_
->GetRegisteredRegions(service_worker_registration_id
, &result
);
96 Send(new GeofencingMsg_GetRegisteredRegionsComplete(
97 thread_id
, request_id
, status
, result
));
100 void GeofencingDispatcherHost::RegisterRegionCompleted(
103 GeofencingStatus status
) {
104 Send(new GeofencingMsg_RegisterRegionComplete(thread_id
, request_id
, status
));
107 void GeofencingDispatcherHost::UnregisterRegionCompleted(
110 GeofencingStatus status
) {
111 Send(new GeofencingMsg_UnregisterRegionComplete(
112 thread_id
, request_id
, status
));
115 } // namespace content