15f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Copyright 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)
5f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/geolocation/geolocation_permission_context_android.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/android/google_location_settings_helper.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/public/browser/web_contents.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)GeolocationPermissionContextAndroid::
15a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochPermissionRequestInfo::PermissionRequestInfo()
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    : id(0, 0, 0, GURL()),
17a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      user_gesture(false) {}
18a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
19f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)GeolocationPermissionContextAndroid::
20f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    GeolocationPermissionContextAndroid(Profile* profile)
21f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    : GeolocationPermissionContext(profile),
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      google_location_settings_helper_(
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          GoogleLocationSettingsHelper::Create()) {
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
26f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)GeolocationPermissionContextAndroid::~GeolocationPermissionContextAndroid() {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
29f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void GeolocationPermissionContextAndroid::ProceedDecidePermission(
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    content::WebContents* web_contents,
31a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    const PermissionRequestInfo& info,
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::Callback<void(bool)> callback) {
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Super class implementation expects everything in UI thread instead.
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
35f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  GeolocationPermissionContext::DecidePermission(
36a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      web_contents, info.id, info.requesting_frame, info.user_gesture,
375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      info.embedder, callback);
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void GeolocationPermissionContextAndroid::CheckSystemLocation(
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::WebContents* web_contents,
42a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    const PermissionRequestInfo& info,
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    base::Callback<void(bool)> callback) {
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check to see if the feature in its entirety has been disabled.
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This must happen before other services (e.g. tabs, extensions)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // get an opportunity to allow the geolocation request.
475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  bool enabled = google_location_settings_helper_->IsSystemLocationEnabled();
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::Closure ui_closure;
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (enabled) {
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ui_closure = base::Bind(
52f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        &GeolocationPermissionContextAndroid::ProceedDecidePermission,
535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        this, web_contents, info, callback);
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  } else {
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ui_closure = base::Bind(
56f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        &GeolocationPermissionContextAndroid::PermissionDecided,
57a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch        this, info.id, info.requesting_frame, info.embedder, callback, false);
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // This method is executed from the BlockingPool, post the result
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // back to the UI thread.
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  content::BrowserThread::PostTask(
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      content::BrowserThread::UI, FROM_HERE, ui_closure);
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
66f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void GeolocationPermissionContextAndroid::DecidePermission(
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::WebContents* web_contents,
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const PermissionRequestID& id,
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const GURL& requesting_frame,
70a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    bool user_gesture,
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const GURL& embedder,
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    base::Callback<void(bool)> callback) {
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
74a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  PermissionRequestInfo info;
75a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  info.id = id;
76a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  info.requesting_frame = requesting_frame;
77a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  info.user_gesture = user_gesture;
78a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  info.embedder = embedder;
79a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Called on the UI thread. However, do the work on a separate thread
81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // to avoid strict mode violation.
82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  content::BrowserThread::PostBlockingPoolTask(FROM_HERE,
84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::Bind(
855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          &GeolocationPermissionContextAndroid::CheckSystemLocation,
86a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch          this, web_contents, info, callback));
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
88