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