Chromium Code Reviews| 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/bind.h" | |
| 7 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 8 #include "chrome/browser/prefs/pref_service.h" | 9 #include "chrome/browser/prefs/pref_service.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/common/chrome_switches.h" | 11 #include "chrome/common/chrome_switches.h" |
| 11 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
| 12 #include "content/browser/browser_thread.h" | 13 #include "content/browser/browser_thread.h" |
| 13 #include "content/public/common/url_fetcher.h" | 14 #include "content/public/common/url_fetcher.h" |
| 14 #include "net/base/escape.h" | 15 #include "net/base/escape.h" |
| 15 #include "net/url_request/url_request_status.h" | 16 #include "net/url_request/url_request_status.h" |
| 16 | 17 |
| 17 AppNotifyChannelSetup::AppNotifyChannelSetup( | 18 AppNotifyChannelSetup::AppNotifyChannelSetup( |
| 18 Profile* profile, | 19 Profile* profile, |
| 19 const std::string& client_id, | 20 const std::string& client_id, |
| 20 const GURL& requestor_url, | 21 const GURL& requestor_url, |
| 21 int return_route_id, | 22 int return_route_id, |
| 22 int callback_id, | 23 int callback_id, |
| 24 AppNotifyChannelUI* ui, | |
| 23 base::WeakPtr<AppNotifyChannelSetup::Delegate> delegate) | 25 base::WeakPtr<AppNotifyChannelSetup::Delegate> delegate) |
| 24 : profile_(profile), | 26 : profile_(profile), |
| 25 client_id_(client_id), | 27 client_id_(client_id), |
| 26 requestor_url_(requestor_url), | 28 requestor_url_(requestor_url), |
| 27 return_route_id_(return_route_id), | 29 return_route_id_(return_route_id), |
| 28 callback_id_(callback_id), | 30 callback_id_(callback_id), |
| 29 delegate_(delegate) {} | 31 delegate_(delegate), |
| 32 ui_(ui) {} | |
| 30 | 33 |
| 31 AppNotifyChannelSetup::~AppNotifyChannelSetup() {} | 34 AppNotifyChannelSetup::~AppNotifyChannelSetup() {} |
| 32 | 35 |
| 33 static GURL GetChannelServerURL() { | 36 static GURL GetChannelServerURL() { |
| 34 // TODO(asargent) - Eventually we'll have a hardcoded url baked in and this | 37 // TODO(asargent) - Eventually we'll have a hardcoded url baked in and this |
| 35 // will just be an override, but for now we just have this flag as the client | 38 // will just be an override, but for now we just have this flag as the client |
| 36 // and server are both works-in-progress. | 39 // and server are both works-in-progress. |
| 37 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 40 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 38 if (command_line->HasSwitch(switches::kAppNotifyChannelServerURL)) { | 41 if (command_line->HasSwitch(switches::kAppNotifyChannelServerURL)) { |
| 39 std::string switch_value = command_line->GetSwitchValueASCII( | 42 std::string switch_value = command_line->GetSwitchValueASCII( |
| 40 switches::kAppNotifyChannelServerURL); | 43 switches::kAppNotifyChannelServerURL); |
| 41 GURL result(switch_value); | 44 GURL result(switch_value); |
| 42 if (result.is_valid()) { | 45 if (result.is_valid()) { |
| 43 return result; | 46 return result; |
| 44 } else { | 47 } else { |
| 45 LOG(ERROR) << "Invalid value for " << | 48 LOG(ERROR) << "Invalid value for " << |
| 46 switches::kAppNotifyChannelServerURL; | 49 switches::kAppNotifyChannelServerURL; |
| 47 } | 50 } |
| 48 } | 51 } |
| 49 return GURL(); | 52 return GURL(); |
| 50 } | 53 } |
| 51 | 54 |
| 52 void AppNotifyChannelSetup::Start() { | 55 void AppNotifyChannelSetup::Start() { |
| 53 AddRef(); // Balanced in ReportResult. | 56 AddRef(); // Balanced in ReportResult. |
| 54 | 57 |
| 55 GURL channel_server_url = GetChannelServerURL(); | |
| 56 | |
| 57 // Check if the user is logged in to the browser. | 58 // Check if the user is logged in to the browser. |
| 58 std::string username = profile_->GetPrefs()->GetString( | 59 std::string username = profile_->GetPrefs()->GetString( |
| 59 prefs::kGoogleServicesUsername); | 60 prefs::kGoogleServicesUsername); |
| 60 | 61 |
| 61 // TODO(asargent) - If the user is not logged in, we'd like to prompt for | 62 if (username.empty()) { |
| 62 // login and if then they sign in, continue as normal. But for now just return | 63 ui_->PromptSyncSetup(this); |
| 63 // an error. We do this via PostTask instead of immediately calling back the | 64 return; // We'll get called back in OnSyncSetupResult |
| 65 } | |
| 66 | |
| 67 BeginFetch(); | |
| 68 } | |
| 69 | |
| 70 void AppNotifyChannelSetup::OnURLFetchComplete( | |
| 71 const content::URLFetcher* source) { | |
| 72 CHECK(source); | |
| 73 net::URLRequestStatus status = source->GetStatus(); | |
| 74 | |
| 75 if (status.status() == net::URLRequestStatus::SUCCESS && | |
| 76 source->GetResponseCode() == 200) { | |
| 77 // TODO(asargent) - we need to parse the response from |source| here. | |
| 78 ReportResult("dummy_do_not_use", ""); | |
| 79 } else { | |
| 80 ReportResult("", "channel_service_error"); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 void AppNotifyChannelSetup::OnSyncSetupResult(bool enabled) { | |
| 85 if (enabled) { | |
| 86 BeginFetch(); | |
| 87 } else { | |
| 88 ReportResult("", "not_available"); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 void AppNotifyChannelSetup::BeginFetch() { | |
| 93 GURL channel_server_url = GetChannelServerURL(); | |
| 94 | |
| 95 // We return the error via PostTask instead of immediately calling back the | |
| 64 // delegate because it simplifies tests. | 96 // delegate because it simplifies tests. |
| 65 if (!channel_server_url.is_valid() || username.empty()) { | 97 if (!channel_server_url.is_valid()) { |
| 98 std::string code; | |
|
Mihai Parparita -not on Chrome
2011/10/27 22:21:05
You don't seem to be using these variables.
asargent_no_longer_on_chrome
2011/10/28 17:09:22
Oops, good catch - I had moved them to be inline i
| |
| 99 std::string error("not_available"); | |
| 66 BrowserThread::PostTask( | 100 BrowserThread::PostTask( |
| 67 BrowserThread::UI, | 101 BrowserThread::UI, |
| 68 FROM_HERE, | 102 FROM_HERE, |
| 69 NewRunnableMethod(this, | 103 base::Bind(&AppNotifyChannelSetup::ReportResult, this, |
| 70 &AppNotifyChannelSetup::ReportResult, | 104 std::string(), std::string("not_available"))); |
| 71 std::string(), | |
| 72 std::string("not_available"))); | |
| 73 return; | 105 return; |
| 74 } | 106 } |
| 75 | 107 |
| 76 url_fetcher_.reset(content::URLFetcher::Create( | 108 url_fetcher_.reset(content::URLFetcher::Create( |
| 77 0, channel_server_url, content::URLFetcher::POST, this)); | 109 0, channel_server_url, content::URLFetcher::POST, this)); |
| 78 | 110 |
| 79 // TODO(asargent) - we eventually want this to use the browser login | 111 // TODO(asargent) - we eventually want this to use the browser login |
| 80 // credentials instead of the regular cookie store, but for now to aid server | 112 // credentials instead of the regular cookie store, but for now to aid server |
| 81 // development, we're just using the regular cookie store. | 113 // development, we're just using the regular cookie store. |
| 82 url_fetcher_->SetRequestContext(profile_->GetRequestContext()); | 114 url_fetcher_->SetRequestContext(profile_->GetRequestContext()); |
| 83 std::string data = "client_id=" + EscapeUrlEncodedData(client_id_, true); | 115 std::string data = "client_id=" + EscapeUrlEncodedData(client_id_, true); |
| 84 url_fetcher_->SetUploadData("application/x-www-form-urlencoded", data); | 116 url_fetcher_->SetUploadData("application/x-www-form-urlencoded", data); |
| 85 url_fetcher_->Start(); | 117 url_fetcher_->Start(); |
| 86 } | 118 } |
| 87 | 119 |
| 88 void AppNotifyChannelSetup::OnURLFetchComplete( | |
| 89 const content::URLFetcher* source) { | |
| 90 CHECK(source); | |
| 91 net::URLRequestStatus status = source->GetStatus(); | |
| 92 | |
| 93 if (status.status() == net::URLRequestStatus::SUCCESS && | |
| 94 source->GetResponseCode() == 200) { | |
| 95 // TODO(asargent) - we need to parse the response from |source| here. | |
| 96 ReportResult("dummy_do_not_use", ""); | |
| 97 } else { | |
| 98 ReportResult("", "channel_service_error"); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 void AppNotifyChannelSetup::ReportResult( | 120 void AppNotifyChannelSetup::ReportResult( |
| 103 const std::string& channel_id, | 121 const std::string& channel_id, |
| 104 const std::string& error) { | 122 const std::string& error) { |
| 105 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 123 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 106 if (delegate_.get()) | 124 if (delegate_.get()) |
| 107 delegate_->AppNotifyChannelSetupComplete( | 125 delegate_->AppNotifyChannelSetupComplete( |
| 108 channel_id, error, return_route_id_, callback_id_); | 126 channel_id, error, return_route_id_, callback_id_); |
| 109 Release(); // Matches AddRef in Start. | 127 Release(); // Matches AddRef in Start. |
| 110 } | 128 } |
| OLD | NEW |