Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 "content/child/permissions/permission_manager.h" | |
| 6 | |
| 7 #include "content/child/worker_task_runner.h" | |
| 8 #include "content/public/common/service_registry.h" | |
| 9 #include "third_party/WebKit/public/platform/WebURL.h" | |
| 10 | |
| 11 namespace content { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 PermissionName GetPermissionName(blink::WebPermissionType type) { | |
| 16 switch (type) { | |
| 17 case blink::WebPermissionTypeGeolocation: | |
| 18 return PERMISSION_NAME_GEOLOCATION; | |
| 19 case blink::WebPermissionTypeNotifications: | |
| 20 return PERMISSION_NAME_NOTIFICATIONS; | |
| 21 case blink::WebPermissionTypePushNotifications: | |
| 22 return PERMISSION_NAME_PUSH_NOTIFICATIONS; | |
| 23 case blink::WebPermissionTypeMidiSysEx: | |
| 24 return PERMISSION_NAME_MIDI_SYSEX; | |
| 25 default: | |
| 26 // The default statement is only there to prevent compilation failures if | |
|
Bernhard Bauer
2015/03/23 10:20:43
Uh, if WebPermissionType does in fact get extended
| |
| 27 // WebPermissionType enum gets extended. | |
| 28 NOTREACHED(); | |
| 29 return PERMISSION_NAME_GEOLOCATION; | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 blink::WebPermissionStatus GetWebPermissionStatus(PermissionStatus status) { | |
| 34 switch (status) { | |
| 35 case PERMISSION_STATUS_GRANTED: | |
| 36 return blink::WebPermissionStatusGranted; | |
| 37 case PERMISSION_STATUS_DENIED: | |
| 38 return blink::WebPermissionStatusDenied; | |
| 39 case PERMISSION_STATUS_ASK: | |
| 40 return blink::WebPermissionStatusPrompt; | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 const int kNoWorkerThread = 0; | |
| 45 | |
| 46 } // anonymous namespace | |
| 47 | |
| 48 PermissionManager::CallbackInformation::CallbackInformation( | |
| 49 blink::WebPermissionQueryCallback* callback, | |
| 50 int worker_thread_id) | |
| 51 : callback_(callback), | |
| 52 worker_thread_id_(worker_thread_id) { | |
| 53 } | |
| 54 | |
| 55 blink::WebPermissionQueryCallback* | |
| 56 PermissionManager::CallbackInformation::callback() const { | |
| 57 return callback_.get(); | |
| 58 } | |
| 59 | |
| 60 int PermissionManager::CallbackInformation::worker_thread_id() const { | |
| 61 return worker_thread_id_; | |
| 62 } | |
| 63 | |
| 64 blink::WebPermissionQueryCallback* | |
| 65 PermissionManager::CallbackInformation::ReleaseCallback() { | |
| 66 return callback_.release(); | |
| 67 } | |
| 68 | |
| 69 PermissionManager::CallbackInformation::~CallbackInformation() { | |
| 70 } | |
| 71 | |
| 72 PermissionManager::PermissionManager(ServiceRegistry& service_registry) { | |
| 73 service_registry.ConnectToRemoteService(&permission_service_); | |
| 74 } | |
| 75 | |
| 76 PermissionManager::~PermissionManager() { | |
| 77 } | |
| 78 | |
| 79 void PermissionManager::queryPermission( | |
| 80 blink::WebPermissionType type, | |
| 81 const blink::WebURL& origin, | |
| 82 blink::WebPermissionQueryCallback* callback) { | |
| 83 QueryPermissionInternal( | |
| 84 type, origin.string().utf8(), callback, kNoWorkerThread); | |
| 85 } | |
| 86 | |
| 87 void PermissionManager::QueryPermissionForWorker( | |
| 88 blink::WebPermissionType type, | |
| 89 const std::string& origin, | |
| 90 blink::WebPermissionQueryCallback* callback, | |
| 91 int worker_thread_id) { | |
| 92 QueryPermissionInternal(type, origin, callback, worker_thread_id); | |
| 93 } | |
| 94 | |
| 95 void PermissionManager::QueryPermissionInternal( | |
| 96 blink::WebPermissionType type, | |
| 97 const std::string& origin, | |
| 98 blink::WebPermissionQueryCallback* callback, | |
| 99 int worker_thread_id) { | |
| 100 // We need to save the |callback| in an IDMap so if |this| gets deleted, the | |
| 101 // callback will not leak. In the case of |this| gets deleted, the | |
|
Bernhard Bauer
2015/03/23 10:20:43
s/In the case of/In case/ (or "In the case of |thi
| |
| 102 // |permission_service_| pipe will be destroyed too so OnQueryPermission will | |
| 103 // not be called. | |
| 104 int request_id = pending_callbacks_.Add( | |
| 105 new CallbackInformation(callback, worker_thread_id)); | |
| 106 permission_service_->HasPermission( | |
| 107 GetPermissionName(type), | |
| 108 origin, | |
| 109 base::Bind(&PermissionManager::OnQueryPermission, | |
| 110 base::Unretained(this), | |
| 111 request_id)); | |
| 112 } | |
| 113 | |
| 114 void PermissionManager::OnQueryPermission(int request_id, | |
| 115 PermissionStatus result) { | |
| 116 CallbackInformation* callback_information = | |
| 117 pending_callbacks_.Lookup(request_id); | |
| 118 DCHECK(callback_information && callback_information->callback()); | |
| 119 scoped_ptr<blink::WebPermissionStatus> status( | |
| 120 new blink::WebPermissionStatus(GetWebPermissionStatus(result))); | |
| 121 | |
| 122 if (callback_information->worker_thread_id() != kNoWorkerThread) { | |
| 123 blink::WebPermissionQueryCallback* callback = | |
| 124 callback_information->ReleaseCallback(); | |
| 125 int worker_thread_id = callback_information->worker_thread_id(); | |
| 126 pending_callbacks_.Remove(request_id); | |
| 127 | |
| 128 // If the worker is no longer running, ::PostTask() will return false and | |
| 129 // gracefully fail, destroying the callback too. | |
|
Bernhard Bauer
2015/03/23 10:20:43
I don't understand this comment. The return value
| |
| 130 WorkerTaskRunner::Instance()->PostTask( | |
| 131 worker_thread_id, | |
| 132 base::Bind(&PermissionManager::RunCallbackOnWorkerThread, | |
| 133 base::Unretained(callback), | |
|
Bernhard Bauer
2015/03/23 10:20:43
Could you pass this base::Owned or in a scoped_ptr
| |
| 134 base::Passed(&status))); | |
| 135 return; | |
| 136 } | |
| 137 | |
| 138 callback_information->callback()->onSuccess(status.release()); | |
| 139 pending_callbacks_.Remove(request_id); | |
| 140 } | |
| 141 | |
| 142 // static | |
| 143 void PermissionManager::RunCallbackOnWorkerThread( | |
| 144 blink::WebPermissionQueryCallback* callback, | |
| 145 scoped_ptr<blink::WebPermissionStatus> status) { | |
| 146 callback->onSuccess(status.release()); | |
| 147 delete callback; | |
| 148 } | |
| 149 | |
| 150 } // namespace content | |
| OLD | NEW |