geolocation_permission_context_android.cc revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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
3// found in the LICENSE file.
4
5#include "chrome/browser/geolocation/geolocation_permission_context_android.h"
6
7#include "base/prefs/pref_service.h"
8#include "chrome/browser/android/google_location_settings_helper.h"
9#include "chrome/browser/profiles/profile.h"
10#include "chrome/common/pref_names.h"
11#include "content/public/browser/browser_thread.h"
12#include "content/public/browser/web_contents.h"
13
14GeolocationPermissionContextAndroid::
15PermissionRequestInfo::PermissionRequestInfo()
16    : id(0, 0, 0, GURL()),
17      user_gesture(false) {}
18
19GeolocationPermissionContextAndroid::
20    GeolocationPermissionContextAndroid(Profile* profile)
21    : GeolocationPermissionContext(profile),
22      google_location_settings_helper_(
23          GoogleLocationSettingsHelper::Create()) {
24}
25
26GeolocationPermissionContextAndroid::~GeolocationPermissionContextAndroid() {
27}
28
29void GeolocationPermissionContextAndroid::ProceedDecidePermission(
30    content::WebContents* web_contents,
31    const PermissionRequestInfo& info,
32    const std::string& accept_button_label,
33    base::Callback<void(bool)> callback) {
34  // Super class implementation expects everything in UI thread instead.
35  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
36  GeolocationPermissionContext::DecidePermission(
37      web_contents, info.id, info.requesting_frame, info.user_gesture,
38      info.embedder, accept_button_label, callback);
39}
40
41void GeolocationPermissionContextAndroid::CheckMasterLocation(
42    content::WebContents* web_contents,
43    const PermissionRequestInfo& info,
44    base::Callback<void(bool)> callback) {
45  // Check to see if the feature in its entirety has been disabled.
46  // This must happen before other services (e.g. tabs, extensions)
47  // get an opportunity to allow the geolocation request.
48  bool enabled =
49      google_location_settings_helper_->IsMasterLocationSettingEnabled();
50
51  // The flow for geolocation permission on android is:
52  // - GeolocationPermissionContextAndroid::DecidePermission
53  // intercepts the flow in the UI thread, and posts task
54  // to the blocking pool to CheckMasterLocation (in order to
55  // avoid strict-mode violation).
56  // - At this point the master location permission is either:
57  // -- enabled, in which we case it proceeds the normal flow
58  // via GeolocationPermissionContext (which may create infobars, etc.).
59  // -- disabled, in which case the permission is already decided.
60  //
61  // In either case, GeolocationPermissionContext expects these
62  // in the UI thread.
63  base::Closure ui_closure;
64  if (enabled) {
65    bool allow_label = google_location_settings_helper_->IsAllowLabel();
66    std::string accept_button_label =
67        google_location_settings_helper_->GetAcceptButtonLabel(allow_label);
68    ui_closure = base::Bind(
69        &GeolocationPermissionContextAndroid::ProceedDecidePermission,
70        this, web_contents, info, accept_button_label, callback);
71  } else {
72    ui_closure = base::Bind(
73        &GeolocationPermissionContextAndroid::PermissionDecided,
74        this, info.id, info.requesting_frame, info.embedder, callback, false);
75  }
76
77  // This method is executed from the BlockingPool, post the result
78  // back to the UI thread.
79  content::BrowserThread::PostTask(
80      content::BrowserThread::UI, FROM_HERE, ui_closure);
81}
82
83void GeolocationPermissionContextAndroid::DecidePermission(
84    content::WebContents* web_contents,
85    const PermissionRequestID& id,
86    const GURL& requesting_frame,
87    bool user_gesture,
88    const GURL& embedder,
89    const std::string& accept_button_label,
90    base::Callback<void(bool)> callback) {
91
92  PermissionRequestInfo info;
93  info.id = id;
94  info.requesting_frame = requesting_frame;
95  info.user_gesture = user_gesture;
96  info.embedder = embedder;
97
98  // Called on the UI thread. However, do the work on a separate thread
99  // to avoid strict mode violation.
100  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
101  content::BrowserThread::PostBlockingPoolTask(FROM_HERE,
102      base::Bind(
103          &GeolocationPermissionContextAndroid::CheckMasterLocation,
104          this, web_contents, info, callback));
105}
106
107void GeolocationPermissionContextAndroid::PermissionDecided(
108    const PermissionRequestID& id,
109    const GURL& requesting_frame,
110    const GURL& embedder,
111    base::Callback<void(bool)> callback,
112    bool allowed) {
113  // If Google Apps Location setting is turned off then we ignore
114  // the 'allow' website setting for this site and instead show
115  // the infobar to go back to the 'settings' to turn it back on.
116  if (allowed &&
117      !google_location_settings_helper_->IsGoogleAppsLocationSettingEnabled()) {
118    QueueController()->CreateInfoBarRequest(
119        id, requesting_frame, embedder, "", callback);
120    return;
121  }
122
123  GeolocationPermissionContext::PermissionDecided(
124      id, requesting_frame, embedder, callback, allowed);
125}
126