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/webdata/web_intents_table.h" | 5 #include "chrome/browser/webdata/web_intents_table.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "googleurl/src/gurl.h" | 9 #include "googleurl/src/gurl.h" |
10 #include "sql/statement.h" | 10 #include "sql/statement.h" |
11 | 11 |
12 namespace { | 12 namespace { |
13 | |
13 bool ExtractIntents(sql::Statement* s, | 14 bool ExtractIntents(sql::Statement* s, |
14 std::vector<WebIntentServiceData>* services) { | 15 std::vector<WebIntentServiceData>* services) { |
15 DCHECK(s); | 16 DCHECK(s); |
James Hawkins
2011/10/29 21:41:32
FYI this DCHECK is about the pointer |s| being non
Greg Billock
2011/10/31 17:07:09
There's an operator overload on !Statement which i
James Hawkins
2011/11/02 07:23:46
As I said previously, |s| is a pointer so if (!s)
| |
17 if (!s) | |
18 return false; | |
19 | |
16 while (s->Step()) { | 20 while (s->Step()) { |
17 WebIntentServiceData service; | 21 WebIntentServiceData service; |
18 string16 tmp = s->ColumnString16(0); | 22 string16 tmp = s->ColumnString16(0); |
19 service.service_url = GURL(tmp); | 23 service.service_url = GURL(tmp); |
20 | 24 |
21 service.action = s->ColumnString16(1); | 25 service.action = s->ColumnString16(1); |
22 service.type = s->ColumnString16(2); | 26 service.type = s->ColumnString16(2); |
23 service.title = s->ColumnString16(3); | 27 service.title = s->ColumnString16(3); |
24 tmp = s->ColumnString16(4); | 28 tmp = s->ColumnString16(4); |
25 // Default to window disposition. | 29 // Default to window disposition. |
26 service.disposition = WebIntentServiceData::DISPOSITION_WINDOW; | 30 service.disposition = WebIntentServiceData::DISPOSITION_WINDOW; |
27 if (tmp == ASCIIToUTF16("inline")) | 31 if (tmp == ASCIIToUTF16("inline")) |
28 service.disposition = WebIntentServiceData::DISPOSITION_INLINE; | 32 service.disposition = WebIntentServiceData::DISPOSITION_INLINE; |
29 services->push_back(service); | 33 services->push_back(service); |
30 } | 34 } |
31 return true; | 35 return true; |
32 } | 36 } |
33 } | 37 |
38 } // namespace | |
34 | 39 |
35 WebIntentsTable::WebIntentsTable(sql::Connection* db, | 40 WebIntentsTable::WebIntentsTable(sql::Connection* db, |
36 sql::MetaTable* meta_table) | 41 sql::MetaTable* meta_table) |
37 : WebDatabaseTable(db, meta_table) { | 42 : WebDatabaseTable(db, meta_table) { |
38 } | 43 } |
39 | 44 |
40 WebIntentsTable::~WebIntentsTable() { | 45 WebIntentsTable::~WebIntentsTable() { |
41 } | 46 } |
42 | 47 |
43 bool WebIntentsTable::Init() { | 48 bool WebIntentsTable::Init() { |
44 if (db_->DoesTableExist("web_intents")) | 49 if (db_->DoesTableExist("web_intents")) |
45 return true; | 50 return true; |
46 | 51 |
47 if (!db_->Execute("CREATE TABLE web_intents (" | 52 if (!db_->Execute("CREATE TABLE web_intents (" |
48 "service_url LONGVARCHAR," | 53 "service_url LONGVARCHAR," |
49 "action VARCHAR," | 54 "action VARCHAR," |
50 "type VARCHAR," | 55 "type VARCHAR," |
51 "title VARCHAR," | 56 "title VARCHAR," |
52 "disposition VARCHAR," | 57 "disposition VARCHAR," |
53 "UNIQUE (service_url, action, type))")) { | 58 "UNIQUE (service_url, action, type))")) { |
54 NOTREACHED(); | 59 return false; |
55 } | 60 } |
56 | 61 |
57 if (!db_->Execute("CREATE INDEX web_intents_index ON web_intents (action)")) | 62 if (!db_->Execute("CREATE INDEX web_intents_index ON web_intents (action)")) |
58 NOTREACHED(); | 63 return false; |
59 | 64 |
60 return true; | 65 return true; |
61 } | 66 } |
62 | 67 |
63 // TODO(jhawkins): Figure out Sync story. | 68 // TODO(jhawkins): Figure out Sync story. |
64 bool WebIntentsTable::IsSyncable() { | 69 bool WebIntentsTable::IsSyncable() { |
65 return false; | 70 return false; |
66 } | 71 } |
67 | 72 |
68 bool WebIntentsTable::GetWebIntents( | 73 bool WebIntentsTable::GetWebIntents( |
69 const string16& action, | 74 const string16& action, |
70 std::vector<WebIntentServiceData>* intents) { | 75 std::vector<WebIntentServiceData>* intents) { |
71 DCHECK(intents); | 76 DCHECK(intents); |
72 sql::Statement s(db_->GetUniqueStatement( | 77 sql::Statement s(db_->GetUniqueStatement( |
73 "SELECT service_url, action, type, title, disposition FROM web_intents " | 78 "SELECT service_url, action, type, title, disposition FROM web_intents " |
74 "WHERE action=?")); | 79 "WHERE action=?")); |
75 if (!s) | |
76 NOTREACHED() << "Statement prepare failed"; | |
77 | 80 |
78 s.BindString16(0, action); | 81 s.BindString16(0, action); |
79 return ExtractIntents(&s, intents); | 82 return ExtractIntents(&s, intents); |
80 } | 83 } |
81 | 84 |
85 // TODO(gbillock): This currently does a full-table scan. Eventually we will | |
86 // store registrations by domain, and so have an indexed origin. At that time, | |
87 // this should just change to do lookup by origin instead of URL. | |
88 bool WebIntentsTable::GetWebIntentsForURL( | |
89 const string16& service_url, | |
90 std::vector<WebIntentServiceData>* intents) { | |
91 DCHECK(intents); | |
92 sql::Statement s(db_->GetUniqueStatement( | |
93 "SELECT service_url, action, type, title, disposition FROM web_intents " | |
94 "WHERE service_url=?")); | |
95 | |
96 s.BindString16(0, service_url); | |
97 return ExtractIntents(&s, intents); | |
98 } | |
99 | |
82 bool WebIntentsTable::GetAllWebIntents( | 100 bool WebIntentsTable::GetAllWebIntents( |
83 std::vector<WebIntentServiceData>* intents) { | 101 std::vector<WebIntentServiceData>* intents) { |
84 DCHECK(intents); | 102 DCHECK(intents); |
85 sql::Statement s(db_->GetUniqueStatement( | 103 sql::Statement s(db_->GetUniqueStatement( |
86 "SELECT service_url, action, type, title, disposition FROM web_intents")); | 104 "SELECT service_url, action, type, title, disposition FROM web_intents")); |
87 if (!s) | |
88 NOTREACHED() << "Statement prepare failed"; | |
89 | 105 |
90 return ExtractIntents(&s, intents); | 106 return ExtractIntents(&s, intents); |
91 } | 107 } |
92 | 108 |
93 bool WebIntentsTable::SetWebIntent(const WebIntentServiceData& intent) { | 109 bool WebIntentsTable::SetWebIntent(const WebIntentServiceData& intent) { |
94 sql::Statement s(db_->GetUniqueStatement( | 110 sql::Statement s(db_->GetUniqueStatement( |
95 "INSERT OR REPLACE INTO web_intents " | 111 "INSERT OR REPLACE INTO web_intents " |
96 "(service_url, type, action, title, disposition) " | 112 "(service_url, type, action, title, disposition) " |
97 "VALUES (?, ?, ?, ?, ?)")); | 113 "VALUES (?, ?, ?, ?, ?)")); |
98 if (!s) | |
99 NOTREACHED() << "Statement prepare failed"; | |
100 | 114 |
101 // Default to window disposition. | 115 // Default to window disposition. |
102 string16 disposition = ASCIIToUTF16("window"); | 116 string16 disposition = ASCIIToUTF16("window"); |
103 if (intent.disposition == WebIntentServiceData::DISPOSITION_INLINE) | 117 if (intent.disposition == WebIntentServiceData::DISPOSITION_INLINE) |
104 disposition = ASCIIToUTF16("inline"); | 118 disposition = ASCIIToUTF16("inline"); |
105 s.BindString(0, intent.service_url.spec()); | 119 s.BindString(0, intent.service_url.spec()); |
106 s.BindString16(1, intent.type); | 120 s.BindString16(1, intent.type); |
107 s.BindString16(2, intent.action); | 121 s.BindString16(2, intent.action); |
108 s.BindString16(3, intent.title); | 122 s.BindString16(3, intent.title); |
109 s.BindString16(4, disposition); | 123 s.BindString16(4, disposition); |
110 return s.Run(); | 124 return s.Run(); |
111 } | 125 } |
112 | 126 |
113 // TODO(jhawkins): Investigate the need to remove rows matching only | 127 // TODO(jhawkins): Investigate the need to remove rows matching only |
114 // |intent.service_url|. It's unlikely the user will be given the ability to | 128 // |intent.service_url|. It's unlikely the user will be given the ability to |
115 // remove at the granularity of actions or types. | 129 // remove at the granularity of actions or types. |
116 bool WebIntentsTable::RemoveWebIntent(const WebIntentServiceData& intent) { | 130 bool WebIntentsTable::RemoveWebIntent(const WebIntentServiceData& intent) { |
117 sql::Statement s(db_->GetUniqueStatement( | 131 sql::Statement s(db_->GetUniqueStatement( |
118 "DELETE FROM web_intents " | 132 "DELETE FROM web_intents " |
119 "WHERE service_url = ? AND action = ? AND type = ?")); | 133 "WHERE service_url = ? AND action = ? AND type = ?")); |
120 if (!s) | |
121 NOTREACHED() << "Statement prepare failed"; | |
122 | 134 |
123 s.BindString(0, intent.service_url.spec()); | 135 s.BindString(0, intent.service_url.spec()); |
124 s.BindString16(1, intent.action); | 136 s.BindString16(1, intent.action); |
125 s.BindString16(2, intent.type); | 137 s.BindString16(2, intent.type); |
126 return s.Run(); | 138 return s.Run(); |
127 } | 139 } |
OLD | NEW |