15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_DISPATCHER_HOST_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_DISPATCHER_HOST_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include <map>
9f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include <vector>
1046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
11f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "base/callback_forward.h"
12f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "base/memory/weak_ptr.h"
13010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "content/browser/geolocation/geolocation_provider_impl.h"
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/browser/web_contents_observer.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)class GURL;
17010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// GeolocationDispatcherHost is an observer for Geolocation messages.
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// It's the complement of GeolocationDispatcher (owned by RenderView).
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class GeolocationDispatcherHost : public WebContentsObserver {
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  explicit GeolocationDispatcherHost(WebContents* web_contents);
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual ~GeolocationDispatcherHost();
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Pause or resumes geolocation. Resuming when nothing is paused is a no-op.
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // If the web contents is paused while not currently using geolocation but
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // then goes on to do so before being resumed, then it will not get
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // geolocation updates until it is resumed.
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void PauseOrResume(bool should_pause);
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
33010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) private:
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // WebContentsObserver
3546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual void RenderFrameDeleted(RenderFrameHost* render_frame_host) OVERRIDE;
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void RenderViewHostChanged(RenderViewHost* old_host,
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                     RenderViewHost* new_host) OVERRIDE;
3846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual bool OnMessageReceived(
3946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      const IPC::Message& msg, RenderFrameHost* render_frame_host) OVERRIDE;
40010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
41010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Message handlers:
4246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void OnRequestPermission(RenderFrameHost* render_frame_host,
4346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                           int bridge_id,
44010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                           const GURL& requesting_frame,
45010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                           bool user_gesture);
4646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void OnCancelPermissionRequest(RenderFrameHost* render_frame_host,
4746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                 int bridge_id,
48010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                 const GURL& requesting_frame);
4946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void OnStartUpdating(RenderFrameHost* render_frame_host,
5046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                       const GURL& requesting_frame,
51010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                       bool enable_high_accuracy);
5246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void OnStopUpdating(RenderFrameHost* render_frame_host);
53010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Updates the geolocation provider with the currently required update
55010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // options.
56010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  void RefreshGeolocationOptions();
57010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
58010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  void OnLocationUpdate(const Geoposition& position);
59010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
60f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void SendGeolocationPermissionResponse(int render_process_id,
61f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                         int render_frame_id,
62f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                         int bridge_id,
63f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                         bool allowed);
64010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
6546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // A map from the RenderFrameHosts that have requested geolocation updates to
6646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // the type of accuracy they requested (true = high accuracy).
6746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::map<RenderFrameHost*, bool> updating_frames_;
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool paused_;
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
70f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  struct PendingPermission {
71f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    PendingPermission(int render_frame_id,
72f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                      int render_process_id,
73f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                      int bridge_id);
74f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    ~PendingPermission();
75f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    int render_frame_id;
76f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    int render_process_id;
77f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    int bridge_id;
78f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    base::Closure cancel;
79f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  };
80f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::vector<PendingPermission> pending_permissions_;
81f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  scoped_ptr<GeolocationProvider::Subscription> geolocation_subscription_;
83010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
84f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  base::WeakPtrFactory<GeolocationDispatcherHost> weak_factory_;
85f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(GeolocationDispatcherHost);
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_DISPATCHER_HOST_H_
92