| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/budget_service/budget_service_impl.h" | 5 #include "chrome/browser/budget_service/budget_service_impl.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "chrome/browser/budget_service/budget_manager.h" | 8 #include "chrome/browser/budget_service/budget_manager.h" |
| 9 #include "chrome/browser/budget_service/budget_manager_factory.h" | 9 #include "chrome/browser/budget_service/budget_manager_factory.h" |
| 10 #include "chrome/browser/permissions/permission_manager.h" |
| 11 #include "chrome/browser/permissions/permission_manager_factory.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "content/public/browser/permission_type.h" |
| 11 #include "content/public/browser/render_process_host.h" | 14 #include "content/public/browser/render_process_host.h" |
| 12 #include "mojo/public/cpp/bindings/strong_binding.h" | 15 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 13 | 16 |
| 14 BudgetServiceImpl::BudgetServiceImpl(int render_process_id) | 17 BudgetServiceImpl::BudgetServiceImpl(int render_process_id) |
| 15 : render_process_id_(render_process_id) {} | 18 : render_process_id_(render_process_id) {} |
| 16 | 19 |
| 17 BudgetServiceImpl::~BudgetServiceImpl() = default; | 20 BudgetServiceImpl::~BudgetServiceImpl() = default; |
| 18 | 21 |
| 19 // static | 22 // static |
| 20 void BudgetServiceImpl::Create( | 23 void BudgetServiceImpl::Create( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 43 | 46 |
| 44 void BudgetServiceImpl::GetBudget(const url::Origin& origin, | 47 void BudgetServiceImpl::GetBudget(const url::Origin& origin, |
| 45 const GetBudgetCallback& callback) { | 48 const GetBudgetCallback& callback) { |
| 46 // The RenderProcessHost should still be alive as long as any connections are | 49 // The RenderProcessHost should still be alive as long as any connections are |
| 47 // alive, and if the BudgetService mojo connection is down, the | 50 // alive, and if the BudgetService mojo connection is down, the |
| 48 // BudgetServiceImpl should have been destroyed. | 51 // BudgetServiceImpl should have been destroyed. |
| 49 content::RenderProcessHost* host = | 52 content::RenderProcessHost* host = |
| 50 content::RenderProcessHost::FromID(render_process_id_); | 53 content::RenderProcessHost::FromID(render_process_id_); |
| 51 DCHECK(host); | 54 DCHECK(host); |
| 52 | 55 |
| 56 Profile* profile = Profile::FromBrowserContext(host->GetBrowserContext()); |
| 57 |
| 58 PermissionManager* permission_manager = |
| 59 PermissionManagerFactory::GetForProfile(profile); |
| 60 DCHECK(permission_manager); |
| 61 |
| 62 // By request of the Privacy Team, we only communicate the budget buckets with |
| 63 // the developer when the notification permission has been granted. This is |
| 64 // something the impact of which has to be reconsidered when the feature is |
| 65 // ready to ship for real. See https://crbug.com/710809 for context. |
| 66 if (permission_manager->GetPermissionStatus( |
| 67 content::PermissionType::NOTIFICATIONS, origin.GetURL(), GURL()) != |
| 68 blink::mojom::PermissionStatus::GRANTED) { |
| 69 blink::mojom::BudgetStatePtr empty_state(blink::mojom::BudgetState::New()); |
| 70 empty_state->budget_at = 0; |
| 71 empty_state->time = base::Time::Now().ToDoubleT(); |
| 72 |
| 73 std::vector<blink::mojom::BudgetStatePtr> predictions; |
| 74 predictions.push_back(std::move(empty_state)); |
| 75 |
| 76 callback.Run(blink::mojom::BudgetServiceErrorType::NONE, |
| 77 std::move(predictions)); |
| 78 return; |
| 79 } |
| 80 |
| 53 // Query the BudgetManager for the budget. | 81 // Query the BudgetManager for the budget. |
| 54 content::BrowserContext* context = host->GetBrowserContext(); | 82 BudgetManagerFactory::GetForProfile(profile)->GetBudget(origin, callback); |
| 55 BudgetManagerFactory::GetForProfile(context)->GetBudget(origin, callback); | |
| 56 } | 83 } |
| 57 | 84 |
| 58 void BudgetServiceImpl::Reserve(const url::Origin& origin, | 85 void BudgetServiceImpl::Reserve(const url::Origin& origin, |
| 59 blink::mojom::BudgetOperationType operation, | 86 blink::mojom::BudgetOperationType operation, |
| 60 const ReserveCallback& callback) { | 87 const ReserveCallback& callback) { |
| 61 // The RenderProcessHost should still be alive as long as any connections are | 88 // The RenderProcessHost should still be alive as long as any connections are |
| 62 // alive, and if the BudgetService mojo connection is down, the | 89 // alive, and if the BudgetService mojo connection is down, the |
| 63 // BudgetServiceImpl should have been destroyed. | 90 // BudgetServiceImpl should have been destroyed. |
| 64 content::RenderProcessHost* host = | 91 content::RenderProcessHost* host = |
| 65 content::RenderProcessHost::FromID(render_process_id_); | 92 content::RenderProcessHost::FromID(render_process_id_); |
| 66 DCHECK(host); | 93 DCHECK(host); |
| 67 | 94 |
| 68 // Request a reservation from the BudgetManager. | 95 // Request a reservation from the BudgetManager. |
| 69 content::BrowserContext* context = host->GetBrowserContext(); | 96 content::BrowserContext* context = host->GetBrowserContext(); |
| 70 BudgetManagerFactory::GetForProfile(context)->Reserve(origin, operation, | 97 BudgetManagerFactory::GetForProfile(context)->Reserve(origin, operation, |
| 71 callback); | 98 callback); |
| 72 } | 99 } |
| OLD | NEW |