cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / android_webview / native / permission / permission_request_handler.cc
blob28c359e7f25880fde26d364fc33d0f74814c87e2
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 {
20 namespace {
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;
30 } // namespace
32 PermissionRequestHandler::PermissionRequestHandler(
33 PermissionRequestHandlerClient* client,
34 content::WebContents* web_contents)
35 : content::WebContentsObserver(web_contents),
36 client_(client),
37 contents_unique_id_(GetLastCommittedEntryID(web_contents)) {
40 PermissionRequestHandler::~PermissionRequestHandler() {
41 CancelAllRequests();
44 void PermissionRequestHandler::SendRequest(
45 scoped_ptr<AwPermissionRequestDelegate> request) {
46 if (Preauthorized(request->GetOrigin(), request->GetResources())) {
47 request->NotifyRequestResult(true);
48 return;
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());
56 PruneRequests();
59 void PermissionRequestHandler::CancelRequest(const GURL& origin,
60 int64 resources) {
61 // The request list might have multiple requests with same origin and
62 // resources.
63 RequestIterator i = FindRequest(origin, resources);
64 while (i != requests_.end()) {
65 CancelRequestInternal(i);
66 requests_.erase(i);
67 i = FindRequest(origin, resources);
71 void PermissionRequestHandler::PreauthorizePermission(const GURL& origin,
72 int64 resources) {
73 if (!resources)
74 return;
76 std::string key = origin.GetOrigin().spec();
77 if (key.empty()) {
78 LOG(ERROR) << "The origin of preauthorization is empty, ignore it.";
79 return;
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()) {
92 CancelAllRequests();
93 contents_unique_id_ = details.entry->GetUniqueID();
97 PermissionRequestHandler::RequestIterator
98 PermissionRequestHandler::FindRequest(const GURL& origin,
99 int64 resources) {
100 RequestIterator i;
101 for (i = requests_.begin(); i != requests_.end(); ++i) {
102 if (i->get() && i->get()->GetOrigin() == origin &&
103 i->get()->GetResources() == resources) {
104 break;
107 return i;
110 void PermissionRequestHandler::CancelRequestInternal(RequestIterator i) {
111 AwPermissionRequest* request = i->get();
112 if (request) {
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();) {
125 if (!i->get())
126 i = requests_.erase(i);
127 else
128 ++i;
132 bool PermissionRequestHandler::Preauthorized(const GURL& origin,
133 int64 resources) {
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