| OLD | NEW |
| (Empty) | |
| 1 #include "config.h" |
| 2 |
| 3 #include "ChaosCoordinates.h" |
| 4 #include "ChaosGeolocation.h" |
| 5 #include "ChaosGeoposition.h" |
| 6 #include "ChromeClient.h" |
| 7 #include "DOMWindow.h" |
| 8 #include "Frame.h" |
| 9 #include "GeolocationPowerbox.h" |
| 10 #include "Page.h" |
| 11 #include "V8Proxy.h" |
| 12 |
| 13 #undef LOG |
| 14 #include "base/logging.h" |
| 15 //#include "views/controls/button/radio_button.h" |
| 16 //#include "views/view.h" |
| 17 //#include "views/window/dialog_delegate.h" |
| 18 |
| 19 #include "webkit/glue/webview_delegate.h" |
| 20 |
| 21 #include <math.h> |
| 22 |
| 23 namespace WebCore { |
| 24 |
| 25 class ChaosGeolocationExact : public ChaosGeolocationImpl { |
| 26 public: |
| 27 ChaosGeolocationExact() { |
| 28 } |
| 29 ChaosCoordinates *coords() const { |
| 30 return new ChaosCoordinates(1.234, 2.345, .00001); |
| 31 } |
| 32 std::string describe() const { |
| 33 return "exact geolocation"; |
| 34 } |
| 35 }; |
| 36 |
| 37 class ChaosGeolocationCity : public ChaosGeolocationImpl { |
| 38 ChaosGeolocationImpl *source_; |
| 39 static const double ACCURACY = .1; |
| 40 |
| 41 static double round(double x) { |
| 42 return x - fmod(x, ACCURACY); |
| 43 } |
| 44 public: |
| 45 ChaosGeolocationCity(ChaosGeolocationImpl *source) : source_(source) { |
| 46 } |
| 47 ChaosCoordinates *coords() const { |
| 48 ChaosCoordinates *sc = source_->coords(); |
| 49 ChaosCoordinates *mc = new ChaosCoordinates(round(sc->latitude()), |
| 50 round(sc->longitude()), |
| 51 ACCURACY); |
| 52 delete sc; |
| 53 return mc; |
| 54 } |
| 55 std::string describe() const { |
| 56 return "city-level geoloication using " + source_->describe(); |
| 57 } |
| 58 }; |
| 59 |
| 60 class ChooseGeolocationProvider : public ChooseGeolocationProviderCallback { |
| 61 public: |
| 62 ChooseGeolocationProvider(ChaosGeolocation *choosing_for) |
| 63 : choosing_for_(choosing_for) { |
| 64 // V8Proxy* proxy = V8Proxy::retrieve(); |
| 65 Frame *frame = V8Proxy::retrieveFrameForCurrentContext(); |
| 66 KURL url = frame->document()->url(); |
| 67 frame->page()->chrome()->client()->chooseGeolocationProvider(this, url); |
| 68 } |
| 69 void OnProviderChosen(GeolocationPowerbox::ProviderId provider_id) { |
| 70 ChaosGeolocationImpl *impl = NULL; |
| 71 |
| 72 /* |
| 73 String urlString = url_.string(); |
| 74 LOG(WARNING) << "Choice " << provider_id << " for domain " |
| 75 << url_.string().ascii().data(); |
| 76 */ |
| 77 |
| 78 switch(provider_id) { |
| 79 case GeolocationPowerbox::EXACT: |
| 80 impl = new ChaosGeolocationExact(); |
| 81 break; |
| 82 case GeolocationPowerbox::CITY: |
| 83 impl = new ChaosGeolocationCity(new ChaosGeolocationExact()); |
| 84 break; |
| 85 } |
| 86 ASSERT(impl); |
| 87 choosing_for_->OnProviderChosen(impl); |
| 88 // I believe this gets deleted by the caller now... |
| 89 } |
| 90 |
| 91 private: |
| 92 ChaosGeolocation *choosing_for_; |
| 93 }; |
| 94 |
| 95 static GeolocationPowerbox powerbox; |
| 96 GeolocationBrowserPowerbox GeolocationBrowserPowerbox::powerbox; |
| 97 |
| 98 ChooseGeolocationProvider *GeolocationPowerbox::choose( |
| 99 ChaosGeolocation *geolocation) { |
| 100 ChaosGeolocationImpl *choice; |
| 101 |
| 102 ChooseGeolocationProvider *chooser |
| 103 = new ChooseGeolocationProvider(geolocation); |
| 104 |
| 105 return chooser; |
| 106 } |
| 107 |
| 108 ChaosGeolocation::ChaosGeolocation() : impl_(0) { |
| 109 } |
| 110 |
| 111 void ChaosGeolocation::OnProviderChosen(ChaosGeolocationImpl *impl) { |
| 112 // FIXME(benl): refcount this |
| 113 impl_ = impl; |
| 114 LOG(WARNING) << "Wired up " << impl_->describe(); |
| 115 } |
| 116 |
| 117 ChaosGeoposition *ChaosGeolocation::lastPosition() { |
| 118 return NULL; |
| 119 } |
| 120 |
| 121 void ChaosGeolocation::getCurrentPosition(PassRefPtr<ChaosPositionCallback> cb)
{ |
| 122 oneShots_.add(GeoNotifier::create(this, cb)); |
| 123 } |
| 124 |
| 125 int ChaosGeolocation::watchPosition(PassRefPtr<ChaosPositionCallback> cb) { |
| 126 static int nextWatchId = 1; // Do not start at zero, can't use it |
| 127 // in a map since it is reserved to |
| 128 // mean "empty". |
| 129 |
| 130 LOG(WARNING) << "pid = " << getpid(); |
| 131 LOG(WARNING) << "wp2"; |
| 132 watchers_.set(nextWatchId, GeoNotifier::create(this, cb)); |
| 133 LOG(WARNING) << "wp3"; |
| 134 |
| 135 powerbox.choose(this); |
| 136 |
| 137 return nextWatchId++; |
| 138 } |
| 139 |
| 140 void ChaosGeolocation::clearWatch(long watchId) { |
| 141 watchers_.remove(watchId); |
| 142 } |
| 143 |
| 144 ChaosGeolocation::GeoNotifier::GeoNotifier(ChaosGeolocation *parent, |
| 145 PassRefPtr<ChaosPositionCallback> successCallback) |
| 146 : successCallback_(successCallback), |
| 147 timer_(this, &ChaosGeolocation::GeoNotifier::timerFired), |
| 148 parent_(parent) { |
| 149 startTimer(); |
| 150 } |
| 151 |
| 152 void ChaosGeolocation::GeoNotifier::startTimer() { |
| 153 timer_.startRepeating(1); |
| 154 } |
| 155 |
| 156 void ChaosGeolocation::GeoNotifier::timerFired(Timer<GeoNotifier> *timer) { |
| 157 if (!parent_->impl_) |
| 158 return; |
| 159 |
| 160 DLOG(WARNING) << "tock!"; |
| 161 ChaosGeoposition *pos = new ChaosGeoposition(parent_->impl_->coords()); |
| 162 successCallback_->handleEvent(pos); |
| 163 } |
| 164 |
| 165 } // namespace WebCore |
| OLD | NEW |