Chromium Code Reviews| 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 content::BrowserContext* context = host->GetBrowserContext(); | |
|
johnme
2017/06/02 16:06:35
Nit: just do `Profile* profile = Profile::FromBrow
Peter Beverloo
2017/06/02 17:40:56
Done.
| |
| 57 | |
| 58 PermissionManager* permission_manager = | |
| 59 PermissionManagerFactory::GetForProfile( | |
| 60 Profile::FromBrowserContext(context)); | |
| 61 DCHECK(permission_manager); | |
| 62 | |
| 63 // By request of the Privacy Team, we only communicate the budget buckets with | |
| 64 // the developer when the notification permission has been granted. This is | |
| 65 // something the impact of which has to be reconsidered when the feature is | |
| 66 // ready to ship for real. See https://crbug.com/710809 for context. | |
| 67 if (permission_manager->GetPermissionStatus( | |
|
johnme
2017/06/02 16:06:35
It's encouraged to use PermissionManager::GetPermi
Peter Beverloo
2017/06/02 17:40:56
This is inherent to how Service Workers work, part
| |
| 68 content::PermissionType::NOTIFICATIONS, origin.GetURL(), GURL()) != | |
| 69 blink::mojom::PermissionStatus::GRANTED) { | |
| 70 blink::mojom::BudgetStatePtr empty_state(blink::mojom::BudgetState::New()); | |
| 71 empty_state->budget_at = 0; | |
| 72 empty_state->time = base::Time::Now().ToDoubleT(); | |
| 73 | |
| 74 std::vector<blink::mojom::BudgetStatePtr> predictions; | |
| 75 predictions.push_back(std::move(empty_state)); | |
| 76 | |
| 77 callback.Run(blink::mojom::BudgetServiceErrorType::NONE, | |
| 78 std::move(predictions)); | |
| 79 return; | |
| 80 } | |
| 81 | |
| 53 // Query the BudgetManager for the budget. | 82 // Query the BudgetManager for the budget. |
| 54 content::BrowserContext* context = host->GetBrowserContext(); | |
| 55 BudgetManagerFactory::GetForProfile(context)->GetBudget(origin, callback); | 83 BudgetManagerFactory::GetForProfile(context)->GetBudget(origin, callback); |
| 56 } | 84 } |
| 57 | 85 |
| 58 void BudgetServiceImpl::Reserve(const url::Origin& origin, | 86 void BudgetServiceImpl::Reserve(const url::Origin& origin, |
| 59 blink::mojom::BudgetOperationType operation, | 87 blink::mojom::BudgetOperationType operation, |
| 60 const ReserveCallback& callback) { | 88 const ReserveCallback& callback) { |
| 61 // The RenderProcessHost should still be alive as long as any connections are | 89 // The RenderProcessHost should still be alive as long as any connections are |
| 62 // alive, and if the BudgetService mojo connection is down, the | 90 // alive, and if the BudgetService mojo connection is down, the |
| 63 // BudgetServiceImpl should have been destroyed. | 91 // BudgetServiceImpl should have been destroyed. |
| 64 content::RenderProcessHost* host = | 92 content::RenderProcessHost* host = |
| 65 content::RenderProcessHost::FromID(render_process_id_); | 93 content::RenderProcessHost::FromID(render_process_id_); |
| 66 DCHECK(host); | 94 DCHECK(host); |
| 67 | 95 |
| 68 // Request a reservation from the BudgetManager. | 96 // Request a reservation from the BudgetManager. |
| 69 content::BrowserContext* context = host->GetBrowserContext(); | 97 content::BrowserContext* context = host->GetBrowserContext(); |
| 70 BudgetManagerFactory::GetForProfile(context)->Reserve(origin, operation, | 98 BudgetManagerFactory::GetForProfile(context)->Reserve(origin, operation, |
| 71 callback); | 99 callback); |
| 72 } | 100 } |
| OLD | NEW |