1 // Copyright 2014 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 "android_webview/native/permission/permission_request_handler.h"
7 #include "android_webview/native/permission/aw_permission_request.h"
8 #include "android_webview/native/permission/aw_permission_request_delegate.h"
9 #include "android_webview/native/permission/permission_request_handler_client.h"
10 #include "base/android/scoped_java_ref.h"
11 #include "base/bind.h"
12 #include "content/public/browser/navigation_details.h"
13 #include "content/public/browser/navigation_entry.h"
14 #include "content/public/browser/web_contents.h"
16 using base::android::ScopedJavaLocalRef
;
18 namespace android_webview
{
22 int GetActiveEntryID(content::WebContents
* web_contents
) {
23 if (!web_contents
) return 0;
25 content::NavigationEntry
* active_entry
=
26 web_contents
->GetController().GetActiveEntry();
27 return active_entry
? active_entry
->GetUniqueID() : 0;
32 PermissionRequestHandler::PermissionRequestHandler(
33 PermissionRequestHandlerClient
* client
, content::WebContents
* web_contents
)
34 : content::WebContentsObserver(web_contents
),
36 contents_unique_id_(GetActiveEntryID(web_contents
)) {
39 PermissionRequestHandler::~PermissionRequestHandler() {
43 void PermissionRequestHandler::SendRequest(
44 scoped_ptr
<AwPermissionRequestDelegate
> request
) {
45 if (Preauthorized(request
->GetOrigin(), request
->GetResources())) {
46 request
->NotifyRequestResult(true);
50 AwPermissionRequest
* aw_request
= new AwPermissionRequest(request
.Pass());
52 base::WeakPtr
<AwPermissionRequest
>(aw_request
->GetWeakPtr()));
53 client_
->OnPermissionRequest(aw_request
);
57 void PermissionRequestHandler::CancelRequest(const GURL
& origin
,
59 // The request list might have multiple requests with same origin and
61 RequestIterator i
= FindRequest(origin
, resources
);
62 while (i
!= requests_
.end()) {
65 i
= FindRequest(origin
, resources
);
69 void PermissionRequestHandler::PreauthorizePermission(const GURL
& origin
,
74 std::string key
= origin
.GetOrigin().spec();
76 LOG(ERROR
) << "The origin of preauthorization is empty, ignore it.";
80 preauthorized_permission_
[key
] |= resources
;
83 void PermissionRequestHandler::NavigationEntryCommitted(
84 const content::LoadCommittedDetails
& details
) {
85 const ui::PageTransition transition
= details
.entry
->GetTransitionType();
86 if (details
.is_navigation_to_different_page() ||
87 ui::PageTransitionStripQualifier(transition
) ==
88 ui::PAGE_TRANSITION_RELOAD
||
89 contents_unique_id_
!= details
.entry
->GetUniqueID()) {
91 contents_unique_id_
= details
.entry
->GetUniqueID();
95 PermissionRequestHandler::RequestIterator
96 PermissionRequestHandler::FindRequest(const GURL
& origin
,
99 for (i
= requests_
.begin(); i
!= requests_
.end(); ++i
) {
100 if (i
->get() && i
->get()->GetOrigin() == origin
&&
101 i
->get()->GetResources() == resources
) {
108 void PermissionRequestHandler::CancelRequest(RequestIterator i
) {
110 client_
->OnPermissionRequestCanceled(i
->get());
111 // The request's grant()/deny() could be called upon
112 // OnPermissionRequestCanceled. Delete AwPermissionRequest if it still
118 void PermissionRequestHandler::CancelAllRequests() {
119 for (RequestIterator i
= requests_
.begin(); i
!= requests_
.end(); ++i
)
123 void PermissionRequestHandler::PruneRequests() {
124 for (RequestIterator i
= requests_
.begin(); i
!= requests_
.end();) {
126 i
= requests_
.erase(i
);
132 bool PermissionRequestHandler::Preauthorized(const GURL
& origin
,
134 std::map
<std::string
, int64
>::iterator i
=
135 preauthorized_permission_
.find(origin
.GetOrigin().spec());
137 return i
!= preauthorized_permission_
.end() &&
138 (resources
& i
->second
) == resources
;
141 } // namespace android_webivew