OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/services/gcm/push_messaging_service_impl.h" | 5 #include "chrome/browser/services/gcm/push_messaging_service_impl.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "chrome/browser/content_settings/permission_request_id.h" | |
11 #include "chrome/browser/profiles/profile.h" | |
12 #include "chrome/browser/services/gcm/gcm_permission_context.h" | |
10 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 13 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
14 #include "chrome/browser/tab_contents/tab_util.h" | |
11 #include "components/gcm_driver/gcm_driver.h" | 15 #include "components/gcm_driver/gcm_driver.h" |
16 #include "content/public/browser/web_contents.h" | |
12 | 17 |
13 namespace gcm { | 18 namespace gcm { |
14 | 19 |
15 PushMessagingServiceImpl::PushMessagingServiceImpl( | 20 PushMessagingServiceImpl::PushMessagingServiceImpl( |
16 GCMProfileService* gcm_profile_service) | 21 GCMProfileService* gcm_profile_service) |
17 : gcm_profile_service_(gcm_profile_service), | 22 : gcm_profile_service_(gcm_profile_service), |
18 weak_factory_(this) {} | 23 weak_factory_(this) {} |
19 | 24 |
20 PushMessagingServiceImpl::~PushMessagingServiceImpl() {} | 25 PushMessagingServiceImpl::~PushMessagingServiceImpl() {} |
21 | 26 |
22 void PushMessagingServiceImpl::Register( | 27 void PushMessagingServiceImpl::Register( |
23 const std::string& app_id, | 28 const std::string& app_id, |
24 const std::string& sender_id, | 29 const std::string& sender_id, |
30 const int renderer_id, | |
31 const int routing_id, | |
25 const content::PushMessagingService::RegisterCallback& callback) { | 32 const content::PushMessagingService::RegisterCallback& callback) { |
26 // The GCMDriver could be NULL if GCMProfileService has been shut down. | 33 // The GCMDriver could be NULL if GCMProfileService has been shut down. |
27 if (!gcm_profile_service_->driver()) | 34 if (!gcm_profile_service_->driver()) |
28 return; | 35 return; |
29 std::vector<std::string> sender_ids(1, sender_id); | 36 content::WebContents* web_contents = |
30 gcm_profile_service_->driver()->Register( | 37 tab_util::GetWebContentsByID(renderer_id, routing_id); |
31 app_id, | 38 |
32 sender_ids, | 39 // The page doesn't exist any more. |
33 base::Bind(&PushMessagingServiceImpl::DidRegister, | 40 if (!web_contents) |
41 return; | |
Michael van Ouwerkerk
2014/06/19 10:16:11
We can't only return. We must also send a rejectio
Miguel Garcia
2014/06/19 17:49:28
But if web_contents does not exist, it means the r
| |
42 | |
43 // TODO(miguelg) need to send this over IPC when bubble support is | |
44 // implemented. | |
45 int bridge_id = -1; | |
46 | |
47 const PermissionRequestID id(renderer_id, routing_id, bridge_id, GURL()); | |
48 | |
49 GURL embedder = web_contents->GetURL(); | |
50 gcm::GCMPermissionContext* context = | |
Michael van Ouwerkerk
2014/06/19 10:16:11
We have other contexts (ServiceWorkerContextWrappe
Miguel Garcia
2014/06/19 17:49:28
Done.
| |
51 gcm_profile_service_->profile()->GetPushMessagingPermissionContext(); | |
52 | |
53 context->RequestPermission( | |
54 web_contents, | |
55 id, | |
56 embedder, | |
57 false, | |
58 base::Bind(&PushMessagingServiceImpl::ShouldProceed, | |
34 weak_factory_.GetWeakPtr(), | 59 weak_factory_.GetWeakPtr(), |
60 sender_id, | |
61 app_id, | |
35 callback)); | 62 callback)); |
36 } | 63 } |
37 | 64 |
38 void PushMessagingServiceImpl::DidRegister( | 65 void PushMessagingServiceImpl::DidRegister( |
39 const content::PushMessagingService::RegisterCallback& callback, | 66 const content::PushMessagingService::RegisterCallback& callback, |
40 const std::string& registration_id, | 67 const std::string& registration_id, |
41 GCMClient::Result result) { | 68 GCMClient::Result result) { |
42 GURL endpoint = GURL("https://android.googleapis.com/gcm/send"); | 69 GURL endpoint = GURL("https://android.googleapis.com/gcm/send"); |
43 callback.Run(endpoint, registration_id, result == GCMClient::SUCCESS); | 70 callback.Run(endpoint, registration_id, result == GCMClient::SUCCESS); |
44 } | 71 } |
45 | 72 |
73 void PushMessagingServiceImpl::ShouldProceed( | |
Michael van Ouwerkerk
2014/06/19 10:16:11
This method name is not very clear. Let's rename t
Miguel Garcia
2014/06/19 17:49:28
Done.
| |
74 const std::string& sender_id, | |
75 const std::string& app_id, | |
76 const content::PushMessagingService::RegisterCallback& callback, | |
Michael van Ouwerkerk
2014/06/19 10:16:11
The argument and method names are starting to conf
Miguel Garcia
2014/06/19 17:49:28
Done.
| |
77 bool allow) { | |
78 if (!allow) { | |
79 // NOT_SIGNED_IN is the closest error I can find when not allowed. | |
Michael van Ouwerkerk
2014/06/19 10:16:11
We really need to rewire all the error codes so pp
Miguel Garcia
2014/06/19 17:49:28
Agreed
| |
80 DidRegister(callback, "", GCMClient::NOT_SIGNED_IN); | |
Michael van Ouwerkerk
2014/06/19 10:16:11
The signed-in requirement is going away. Maybe jus
Miguel Garcia
2014/06/19 17:49:28
Done.
| |
81 return; | |
82 } | |
83 | |
84 // The GCMDriver could be NULL if GCMProfileService has been shut down. | |
85 if (!gcm_profile_service_->driver()) | |
86 return; | |
87 | |
88 std::vector<std::string> sender_ids(1, sender_id); | |
89 | |
90 gcm_profile_service_->driver()->Register( | |
91 app_id, | |
92 sender_ids, | |
93 base::Bind(&PushMessagingServiceImpl::DidRegister, | |
94 weak_factory_.GetWeakPtr(), | |
95 callback)); | |
96 } | |
97 | |
46 } // namespace gcm | 98 } // namespace gcm |
OLD | NEW |