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 GetLastCommittedEntryID(content::WebContents
* web_contents
) {
23 if (!web_contents
) return 0;
25 content::NavigationEntry
* entry
=
26 web_contents
->GetController().GetLastCommittedEntry();
27 return entry
? entry
->GetUniqueID() : 0;
32 PermissionRequestHandler::PermissionRequestHandler(
33 PermissionRequestHandlerClient
* client
,
34 content::WebContents
* web_contents
)
35 : content::WebContentsObserver(web_contents
),
37 contents_unique_id_(GetLastCommittedEntryID(web_contents
)) {
40 PermissionRequestHandler::~PermissionRequestHandler() {
44 void PermissionRequestHandler::SendRequest(
45 scoped_ptr
<AwPermissionRequestDelegate
> request
) {
46 if (Preauthorized(request
->GetOrigin(), request
->GetResources())) {
47 request
->NotifyRequestResult(true);
51 base::WeakPtr
<AwPermissionRequest
> weak_request
;
52 base::android::ScopedJavaLocalRef
<jobject
> java_peer
=
53 AwPermissionRequest::Create(request
.Pass(), &weak_request
);
54 requests_
.push_back(weak_request
);
55 client_
->OnPermissionRequest(java_peer
, weak_request
.get());
59 void PermissionRequestHandler::CancelRequest(const GURL
& origin
,
61 // The request list might have multiple requests with same origin and
63 RequestIterator i
= FindRequest(origin
, resources
);
64 while (i
!= requests_
.end()) {
65 CancelRequestInternal(i
);
67 i
= FindRequest(origin
, resources
);
71 void PermissionRequestHandler::PreauthorizePermission(const GURL
& origin
,
76 std::string key
= origin
.GetOrigin().spec();
78 LOG(ERROR
) << "The origin of preauthorization is empty, ignore it.";
82 preauthorized_permission_
[key
] |= resources
;
85 void PermissionRequestHandler::NavigationEntryCommitted(
86 const content::LoadCommittedDetails
& details
) {
87 const ui::PageTransition transition
= details
.entry
->GetTransitionType();
88 if (details
.is_navigation_to_different_page() ||
89 ui::PageTransitionStripQualifier(transition
) ==
90 ui::PAGE_TRANSITION_RELOAD
||
91 contents_unique_id_
!= details
.entry
->GetUniqueID()) {
93 contents_unique_id_
= details
.entry
->GetUniqueID();
97 PermissionRequestHandler::RequestIterator
98 PermissionRequestHandler::FindRequest(const GURL
& origin
,
101 for (i
= requests_
.begin(); i
!= requests_
.end(); ++i
) {
102 if (i
->get() && i
->get()->GetOrigin() == origin
&&
103 i
->get()->GetResources() == resources
) {
110 void PermissionRequestHandler::CancelRequestInternal(RequestIterator i
) {
111 AwPermissionRequest
* request
= i
->get();
113 client_
->OnPermissionRequestCanceled(request
);
114 request
->CancelAndDelete();
118 void PermissionRequestHandler::CancelAllRequests() {
119 for (RequestIterator i
= requests_
.begin(); i
!= requests_
.end(); ++i
)
120 CancelRequestInternal(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