• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 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.
4 
5 #include "chrome/test/chromedriver/chrome/geolocation_override_manager.h"
6 
7 #include "base/values.h"
8 #include "chrome/test/chromedriver/chrome/devtools_client.h"
9 #include "chrome/test/chromedriver/chrome/geoposition.h"
10 #include "chrome/test/chromedriver/chrome/status.h"
11 
GeolocationOverrideManager(DevToolsClient * client)12 GeolocationOverrideManager::GeolocationOverrideManager(DevToolsClient* client)
13     : client_(client) {
14   client_->AddListener(this);
15 }
16 
~GeolocationOverrideManager()17 GeolocationOverrideManager::~GeolocationOverrideManager() {
18 }
19 
OverrideGeolocation(const Geoposition & geoposition)20 Status GeolocationOverrideManager::OverrideGeolocation(
21     const Geoposition& geoposition) {
22   overridden_geoposition_.reset(new Geoposition(geoposition));
23   return ApplyOverrideIfNeeded();
24 }
25 
OnConnected(DevToolsClient * client)26 Status GeolocationOverrideManager::OnConnected(DevToolsClient* client) {
27   return ApplyOverrideIfNeeded();
28 }
29 
OnEvent(DevToolsClient * client,const std::string & method,const base::DictionaryValue & params)30 Status GeolocationOverrideManager::OnEvent(
31     DevToolsClient* client,
32     const std::string& method,
33     const base::DictionaryValue& params) {
34   if (method == "Page.frameNavigated") {
35     const base::Value* unused_value;
36     if (!params.Get("frame.parentId", &unused_value))
37       return ApplyOverrideIfNeeded();
38   }
39   return Status(kOk);
40 }
41 
ApplyOverrideIfNeeded()42 Status GeolocationOverrideManager::ApplyOverrideIfNeeded() {
43   if (!overridden_geoposition_)
44     return Status(kOk);
45 
46   base::DictionaryValue params;
47   params.SetDouble("latitude", overridden_geoposition_->latitude);
48   params.SetDouble("longitude", overridden_geoposition_->longitude);
49   params.SetDouble("accuracy", overridden_geoposition_->accuracy);
50   return client_->SendCommand("Page.setGeolocationOverride", params);
51 }
52