| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/extensions/app_notify_channel_setup.h" | 5 #include "chrome/browser/extensions/app_notify_channel_setup.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "net/base/escape.h" | 24 #include "net/base/escape.h" |
| 25 #include "net/base/load_flags.h" | 25 #include "net/base/load_flags.h" |
| 26 #include "net/http/http_request_headers.h" | 26 #include "net/http/http_request_headers.h" |
| 27 #include "net/url_request/url_request_status.h" | 27 #include "net/url_request/url_request_status.h" |
| 28 | 28 |
| 29 using base::StringPrintf; | 29 using base::StringPrintf; |
| 30 using content::BrowserThread; | 30 using content::BrowserThread; |
| 31 using content::URLFetcher; | 31 using content::URLFetcher; |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 |
| 34 static const char kChannelSetupAuthError[] = "unauthorized"; | 35 static const char kChannelSetupAuthError[] = "unauthorized"; |
| 35 static const char kChannelSetupInternalError[] = "internal_error"; | 36 static const char kChannelSetupInternalError[] = "internal_error"; |
| 36 static const char kChannelSetupCanceledByUser[] = "canceled_by_user"; | 37 static const char kChannelSetupCanceledByUser[] = "canceled_by_user"; |
| 37 // TODO(munjal): Change these to use production URLs when we have them. | 38 // TODO(munjal): Change these to use production URLs when we have them. |
| 38 static const char kAuthorizationHeaderFormat[] = | 39 static const char kAuthorizationHeaderFormat[] = |
| 39 "Authorization: GoogleLogin auth=%s"; | 40 "Authorization: GoogleLogin auth=%s"; |
| 40 static const char kOAuth2IssueTokenURL[] = | 41 static const char kOAuth2IssueTokenURL[] = |
| 41 "https://www.googleapis.com/oauth2/v2/IssueToken"; | 42 "https://www.googleapis.com/oauth2/v2/IssueToken"; |
| 42 static const char kOAuth2IssueTokenBodyFormat[] = | 43 static const char kOAuth2IssueTokenBodyFormat[] = |
| 43 "force=true" | 44 "force=true" |
| 44 "&response_type=token" | 45 "&response_type=token" |
| 45 "&scope=%s" | 46 "&scope=%s" |
| 46 "&client_id=%s" | 47 "&client_id=%s" |
| 47 "&origin=%s"; | 48 "&origin=%s"; |
| 48 static const char kOAuth2IssueTokenScope[] = | 49 static const char kOAuth2IssueTokenScope[] = |
| 49 "https://www.googleapis.com/auth/chromewebstore.notification"; | 50 "https://www.googleapis.com/auth/chromewebstore.notification"; |
| 50 static const char kCWSChannelServiceURL[] = | 51 static const char kCWSChannelServiceURL[] = |
| 51 "https://www.googleapis.com/chromewebstore/" | 52 "https://www.googleapis.com/chromewebstore/" |
| 52 "v1.1/channels/id"; | 53 "v1.1/channels/id"; |
| 53 static const char* kRelevantGaiaServices[] = { | 54 static const char* kRelevantGaiaServices[] = { |
| 54 GaiaConstants::kCWSService, | 55 GaiaConstants::kCWSService, |
| 55 GaiaConstants::kLSOService, | 56 GaiaConstants::kLSOService, |
| 56 }; | 57 }; |
| 57 static const int kRelevantGaiaServicesCount = arraysize(kRelevantGaiaServices); | 58 static const int kRelevantGaiaServicesCount = arraysize(kRelevantGaiaServices); |
| 59 |
| 60 static AppNotifyChannelSetup::InterceptorForTests* g_interceptor_for_tests = |
| 61 NULL; |
| 62 |
| 58 } // namespace. | 63 } // namespace. |
| 59 | 64 |
| 65 // static |
| 66 void AppNotifyChannelSetup::SetInterceptorForTests( |
| 67 AppNotifyChannelSetup::InterceptorForTests* interceptor) { |
| 68 // Only one interceptor at a time, please. |
| 69 CHECK(g_interceptor_for_tests == NULL); |
| 70 g_interceptor_for_tests = interceptor; |
| 71 } |
| 72 |
| 60 AppNotifyChannelSetup::AppNotifyChannelSetup( | 73 AppNotifyChannelSetup::AppNotifyChannelSetup( |
| 61 Profile* profile, | 74 Profile* profile, |
| 62 const std::string& extension_id, | 75 const std::string& extension_id, |
| 63 const std::string& client_id, | 76 const std::string& client_id, |
| 64 const GURL& requestor_url, | 77 const GURL& requestor_url, |
| 65 int return_route_id, | 78 int return_route_id, |
| 66 int callback_id, | 79 int callback_id, |
| 67 AppNotifyChannelUI* ui, | 80 AppNotifyChannelUI* ui, |
| 68 base::WeakPtr<AppNotifyChannelSetup::Delegate> delegate) | 81 base::WeakPtr<AppNotifyChannelSetup::Delegate> delegate) |
| 69 : profile_(profile), | 82 : profile_(profile), |
| 70 extension_id_(extension_id), | 83 extension_id_(extension_id), |
| 71 client_id_(client_id), | 84 client_id_(client_id), |
| 72 requestor_url_(requestor_url), | 85 requestor_url_(requestor_url), |
| 73 return_route_id_(return_route_id), | 86 return_route_id_(return_route_id), |
| 74 callback_id_(callback_id), | 87 callback_id_(callback_id), |
| 75 delegate_(delegate), | 88 delegate_(delegate), |
| 76 ui_(ui), | 89 ui_(ui), |
| 77 state_(INITIAL), | 90 state_(INITIAL), |
| 78 fetch_token_success_count_(0), | 91 fetch_token_success_count_(0), |
| 79 fetch_token_fail_count_(0) {} | 92 fetch_token_fail_count_(0) {} |
| 80 | 93 |
| 81 AppNotifyChannelSetup::~AppNotifyChannelSetup() {} | 94 AppNotifyChannelSetup::~AppNotifyChannelSetup() {} |
| 82 | 95 |
| 83 void AppNotifyChannelSetup::Start() { | 96 void AppNotifyChannelSetup::Start() { |
| 97 if (g_interceptor_for_tests) { |
| 98 std::string channel_id; |
| 99 std::string error; |
| 100 g_interceptor_for_tests->DoIntercept(this, &channel_id, &error); |
| 101 delegate_->AppNotifyChannelSetupComplete(channel_id, error, this); |
| 102 return; |
| 103 } |
| 84 AddRef(); // Balanced in ReportResult. | 104 AddRef(); // Balanced in ReportResult. |
| 85 BeginLogin(); | 105 BeginLogin(); |
| 86 } | 106 } |
| 87 | 107 |
| 88 void AppNotifyChannelSetup::Observe( | 108 void AppNotifyChannelSetup::Observe( |
| 89 int type, | 109 int type, |
| 90 const content::NotificationSource& source, | 110 const content::NotificationSource& source, |
| 91 const content::NotificationDetails& details) { | 111 const content::NotificationDetails& details) { |
| 92 if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) { | 112 if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) { |
| 93 TokenService::TokenAvailableDetails* tok_details = | 113 TokenService::TokenAvailableDetails* tok_details = |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 } | 330 } |
| 311 } | 331 } |
| 312 | 332 |
| 313 void AppNotifyChannelSetup::ReportResult( | 333 void AppNotifyChannelSetup::ReportResult( |
| 314 const std::string& channel_id, | 334 const std::string& channel_id, |
| 315 const std::string& error) { | 335 const std::string& error) { |
| 316 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 336 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 317 CHECK(state_ == CHANNEL_ID_SETUP_DONE || state_ == ERROR_STATE); | 337 CHECK(state_ == CHANNEL_ID_SETUP_DONE || state_ == ERROR_STATE); |
| 318 | 338 |
| 319 if (delegate_.get()) { | 339 if (delegate_.get()) { |
| 320 delegate_->AppNotifyChannelSetupComplete( | 340 delegate_->AppNotifyChannelSetupComplete(channel_id, error, this); |
| 321 channel_id, error, return_route_id_, callback_id_); | |
| 322 } | 341 } |
| 323 Release(); // Matches AddRef in Start. | 342 Release(); // Matches AddRef in Start. |
| 324 } | 343 } |
| 325 | 344 |
| 326 // static | 345 // static |
| 327 GURL AppNotifyChannelSetup::GetCWSChannelServiceURL() { | 346 GURL AppNotifyChannelSetup::GetCWSChannelServiceURL() { |
| 328 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 347 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 329 if (command_line->HasSwitch(switches::kAppNotifyChannelServerURL)) { | 348 if (command_line->HasSwitch(switches::kAppNotifyChannelServerURL)) { |
| 330 std::string switch_value = command_line->GetSwitchValueASCII( | 349 std::string switch_value = command_line->GetSwitchValueASCII( |
| 331 switches::kAppNotifyChannelServerURL); | 350 switches::kAppNotifyChannelServerURL); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 DictionaryValue* dict = static_cast<DictionaryValue*>(value.get()); | 402 DictionaryValue* dict = static_cast<DictionaryValue*>(value.get()); |
| 384 if (!dict->Get("id", &channel_id_value)) | 403 if (!dict->Get("id", &channel_id_value)) |
| 385 return false; | 404 return false; |
| 386 if (channel_id_value->GetType() != base::Value::TYPE_STRING) | 405 if (channel_id_value->GetType() != base::Value::TYPE_STRING) |
| 387 return false; | 406 return false; |
| 388 | 407 |
| 389 StringValue* channel_id = static_cast<StringValue*>(channel_id_value); | 408 StringValue* channel_id = static_cast<StringValue*>(channel_id_value); |
| 390 channel_id->GetAsString(result); | 409 channel_id->GetAsString(result); |
| 391 return true; | 410 return true; |
| 392 } | 411 } |
| OLD | NEW |