Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(840)

Side by Side Diff: content/browser/geolocation/geolocation.cc

Issue 10344004: Add content API for requesting the current geolocation (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Rebased. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/public/browser/geolocation.h" 5 #include "content/public/browser/geolocation.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/logging.h"
10 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop_proxy.h" 13 #include "base/message_loop_proxy.h"
12 #include "content/browser/geolocation/geolocation_provider.h" 14 #include "content/browser/geolocation/geolocation_provider.h"
13 #include "content/public/browser/browser_thread.h" 15 #include "content/public/browser/browser_thread.h"
14 #include "content/public/common/geoposition.h" 16 #include "content/public/common/geoposition.h"
15 17
16 namespace content { 18 namespace content {
17 19
18 namespace { 20 namespace {
19 21
22 scoped_ptr<Geoposition> callback_override_position;
23
20 void OverrideLocationForTestingOnIOThread( 24 void OverrideLocationForTestingOnIOThread(
21 const Geoposition& position, 25 const Geoposition& position,
22 const base::Closure& completion_callback, 26 const base::Closure& completion_callback,
23 scoped_refptr<base::MessageLoopProxy> callback_loop) { 27 scoped_refptr<base::MessageLoopProxy> callback_loop) {
28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
24 GeolocationProvider::GetInstance()->OverrideLocationForTesting(position); 29 GeolocationProvider::GetInstance()->OverrideLocationForTesting(position);
25 callback_loop->PostTask(FROM_HERE, completion_callback); 30 callback_loop->PostTask(FROM_HERE, completion_callback);
26 } 31 }
27 32
33 void GeolocationUpdateCallbackOnIOThread(
34 const GeolocationUpdateCallback& client_callback,
35 scoped_refptr<base::MessageLoopProxy> client_loop,
36 const Geoposition& position) {
37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
38 if (base::MessageLoopProxy::current() != client_loop)
39 client_loop->PostTask(FROM_HERE, base::Bind(client_callback, position));
40 else
41 client_callback.Run(position);
42 }
43 void RequestLocationUpdateOnIOThread(
44 const GeolocationUpdateCallback& client_callback,
45 scoped_refptr<base::MessageLoopProxy> client_loop) {
46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
47 GeolocationUpdateCallback io_thread_callback = base::Bind(
48 &GeolocationUpdateCallbackOnIOThread,
49 client_callback,
50 client_loop);
51 GeolocationProvider::GetInstance()->RequestCallback(io_thread_callback);
52 }
53
28 } // namespace 54 } // namespace
29 55
30 void OverrideLocationForTesting( 56 void OverrideLocationForTesting(const Geoposition& position,
31 const Geoposition& position, 57 const base::Closure& completion_callback) {
32 const base::Closure& completion_callback) {
33 base::Closure closure = base::Bind(&OverrideLocationForTestingOnIOThread, 58 base::Closure closure = base::Bind(&OverrideLocationForTestingOnIOThread,
34 position, 59 position,
35 completion_callback, 60 completion_callback,
36 base::MessageLoopProxy::current()); 61 base::MessageLoopProxy::current());
37 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { 62 if (!BrowserThread::CurrentlyOn(BrowserThread::IO))
38 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, closure); 63 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, closure);
39 } else { 64 else
40 closure.Run(); 65 closure.Run();
66 }
67 void OverrideCallbackLocationForTesting(Geoposition* position) {
68 callback_override_position.reset(position);
69 }
70
71 void RequestLocationUpdate(const GeolocationUpdateCallback& callback) {
72 // Only used for testing.
73 if (callback_override_position.get()) {
74 callback.Run(*callback_override_position.get());
75 return;
41 } 76 }
77
78 base::Closure closure = base::Bind(&RequestLocationUpdateOnIOThread,
79 callback,
80 base::MessageLoopProxy::current());
81 if (!BrowserThread::CurrentlyOn(BrowserThread::IO))
82 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, closure);
83 else
84 closure.Run();
42 } 85 }
43 86
44 } // namespace content 87 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/browser/geolocation/geolocation_observer.h » ('j') | content/public/browser/geolocation.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698